• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Log } from '@ohos/common';
17import { EventConstants } from '@ohos/common';
18import { localEventManager } from '@ohos/common';
19import { SettingsModel } from '@ohos/common';
20import { SettingItemInfo } from '@ohos/common';
21import { SettingItemsConfig } from '@ohos/common';
22import { SettingItemsManager } from '@ohos/common';
23import { SettingItemOptionsChecker } from '@ohos/common';
24
25const TAG = 'SettingsPresenter';
26
27/**
28 * Class SettingsPresenter.
29 */
30export default class SettingsPresenter {
31  /**
32   * style: list or grid
33   */
34  static SETTINGS_INDEX_STYLE = 0;
35
36  /**
37   * grid layout: row x column
38   */
39  static SETTINGS_INDEX_GRID_LAYOUT = 1;
40
41  private readonly mSettingsModel: SettingsModel = null;
42
43  private readonly mCallbackList = [];
44
45  private readonly mSettingItemsManager: SettingItemsManager;
46
47  private readonly mLayoutOptionsChecker: SettingItemOptionsChecker = ()=> {
48    const layout = this.mSettingsModel.getAppPageStartConfig();
49    Log.showDebug(TAG, `mLayoutOptionsChecker layout: ${layout}`);
50    return layout;
51  };
52
53  private readonly mGridLayOutOptionsChecker: SettingItemOptionsChecker = ()=> {
54    const gridLayout = this.mSettingsModel.getGridConfig().layout;
55    Log.showDebug(TAG, `mGridLayOutOptionsChecker layout: ${gridLayout}`);
56    return gridLayout;
57  };
58
59  /**
60   * Constructor.
61   *
62   * @param {object} settingsModel - model of setting.
63   */
64  constructor() {
65    this.mSettingsModel = SettingsModel.getInstance();
66    this.mSettingItemsManager = (new SettingItemsManager()).
67      withChecker(SettingItemsConfig.SETTING_ITEM_LAYOUT_OPTIONS, this.mLayoutOptionsChecker).
68      withChecker(SettingItemsConfig.SETTING_ITEM_PHONE_GRID_LAYOUT_OPTIONS, this.mGridLayOutOptionsChecker).
69      withChecker(SettingItemsConfig.SETTING_ITEM_PAD_GRID_LAYOUT_OPTIONS, this.mGridLayOutOptionsChecker);
70  }
71
72  /**
73   * Get settingsPresenter instance.
74   *
75   * @return {settingPresenter} - settingPresenter.
76   */
77  static getInstance(): SettingsPresenter{
78    if (globalThis.SettingsPresenter == null) {
79      globalThis.SettingsPresenter = new SettingsPresenter();
80    }
81    return globalThis.SettingsPresenter;
82  }
83
84  /**
85   * Get setting list.
86   *
87   * @return [settingList] - setting list.
88   */
89  getSettingList(): SettingItemInfo[] {
90    const deviceType = this.mSettingsModel.getDevice() == 'phone' ?
91      SettingItemsConfig.DEVICE_TYPE_PHONE : SettingItemsConfig.DEVICE_TYPE_PAD;
92
93    const condition = this.mSettingsModel.getAppPageStartConfig() == 'Grid' ?
94      SettingItemsConfig.CONDITION_GRID_LAYOUT_ENABLE : SettingItemsConfig.CONDITION_LIST_LAYOUT_ENABLE;
95
96    Log.showDebug(TAG, 'getSettingList, deviceType is '+ deviceType + ', condition is ' + condition);
97    return this.mSettingItemsManager.get(deviceType, condition);
98  }
99
100  /**
101   * Set system setting value.
102   *
103   * @param {string} settingsName - setting name.
104   * @param {string} settingValue - setting value.
105   */
106  setSettingsValue(ida, settingValue) {
107    Log.showDebug(TAG, 'setSettingsValue, ida is '+ ida + ', settingValue is ' + settingValue);
108
109    if (ida == SettingsPresenter.SETTINGS_INDEX_STYLE) {
110      this.setAppPageStartConfig(settingValue);
111    } else if (ida == SettingsPresenter.SETTINGS_INDEX_GRID_LAYOUT) {
112      const idx = this.mSettingItemsManager.gridLayoutValue2Idx(settingValue);
113      Log.showDebug(TAG, 'setSettingsValue, idx is '+ idx);
114      this.setGridConfig(idx);
115    } else {
116      this.setRecentMissionsLimit(settingValue);
117    }
118    this.settingUpdate();
119  }
120
121  /**
122   * Set app start config.
123   *
124   * @param {string} type - the type of config.
125   */
126  setAppPageStartConfig(type) {
127    this.mSettingsModel.setAppPageStartConfig(type);
128  }
129
130  /**
131   * Update setting.
132   *
133   */
134  settingUpdate() {
135    Log.showDebug(TAG, 'settingUpdate start');
136    globalThis.settingsContext.terminateSelf()
137      .then(data => Log.showDebug(TAG, 'terminateSelf promise::then : ' + data))
138      .catch(error => Log.showError(TAG, 'terminateSelf promise::catch : ' + error));
139    Log.showDebug(TAG, 'terminateSelf end');
140  }
141
142  /**
143   * Set grid config.
144   *
145   * @param {string} id - the id of grid config.
146   */
147  setGridConfig(id) {
148    this.mSettingsModel.setGridConfig(id);
149  }
150
151  /**
152   * Set recent missions limit.
153   *
154   * @param {number} num - the num of recent missions.
155   */
156  setRecentMissionsLimit(num) {
157    this.mSettingsModel.setRecentMissionsLimit(num);
158  }
159
160  /**
161   * Back to the desktop interface.
162   *
163   */
164  backToTheDesktop() {
165    Log.showDebug(TAG, 'backToTheDesktop!');
166    this.settingUpdate();
167  }
168
169  /**
170   * Register value callback.
171   *
172   * @param {string} settingsName - setting name.
173   * @param {function()} settingValue - setting value.
174   */
175  registerValueCallback(ida, settingValue) {
176    this.mCallbackList.push({
177      id: ida,
178      fun: settingValue
179    });
180  }
181
182  /**
183   * Change page setting value.
184   *
185   * @param {string} settingsName - setting name.
186   * @param {string} settingValue - setting value.
187   */
188  changeSettingValue(ida, settingValue) {
189    for (let i = 0;i < this.mCallbackList.length; i++) {
190      if (this.mCallbackList[i].id == ida) {
191        this.mCallbackList[i].fun(settingValue);
192        break;
193      }
194    }
195  }
196
197  /**
198   * get the device type.
199   *
200   * @return {string} device type
201   */
202  getDevice(): string {
203    return this.mSettingsModel.getDevice();
204  }
205
206  sendLocalEvent(value: string) {
207    Log.showDebug(TAG, `setValue value: ${value}`);
208    if (value != '1' && value != '0') {
209      Log.showDebug(TAG, 'setValue error');
210      return;
211    }
212    if (value == '0') {
213      this.mSettingsModel.setValue(value);
214    } else {
215      localEventManager.sendLocalEventSticky(EventConstants.EVENT_NAVIGATOR_BAR_STATUS_CHANGE, value);
216    }
217  }
218
219  initNavigationBarStatusValue() {
220    try {
221      const initValue = this.mSettingsModel.getValue();
222      const navigationBarStatusValue = initValue == '0' ? true : false;
223      Log.showDebug(TAG, `initNavigationBarStatusValue initValue:${initValue}, navigationBarStatusValue:${navigationBarStatusValue}`);
224      AppStorage.SetOrCreate('NavigationBarStatusValue', navigationBarStatusValue);
225    } catch (e) {
226      Log.showError(TAG, `initNavigationBarStatusValue error:  ${e.toString()}`);
227    }
228  }
229}