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 { SettingsModel } from '@ohos/common'; 18import { SettingItemInfo } from '@ohos/common'; 19import { SettingItemsConfig } from '@ohos/common'; 20import { SettingItemsManager } from '@ohos/common'; 21import { SettingItemOptionsChecker } from '@ohos/common'; 22 23const TAG = 'SettingsPresenter'; 24 25/** 26 * Class SettingsPresenter. 27 */ 28export default class SettingsPresenter { 29 /** 30 * style: list or grid 31 */ 32 static SETTINGS_INDEX_STYLE = 0; 33 34 /** 35 * grid layout: row x column 36 */ 37 static SETTINGS_INDEX_GRID_LAYOUT = 1; 38 39 private readonly mSettingsModel: SettingsModel = null; 40 41 private readonly mCallbackList = []; 42 43 private readonly mSettingItemsManager: SettingItemsManager; 44 45 private readonly mLayoutOptionsChecker: SettingItemOptionsChecker = ()=> { 46 const layout = this.mSettingsModel.getAppPageStartConfig(); 47 Log.showDebug(TAG, `mLayoutOptionsChecker layout: ${layout}`); 48 return layout; 49 }; 50 51 private readonly mGridLayOutOptionsChecker: SettingItemOptionsChecker = ()=> { 52 const gridLayout = this.mSettingsModel.getGridConfig().layout; 53 Log.showDebug(TAG, `mGridLayOutOptionsChecker layout: ${gridLayout}`); 54 return gridLayout; 55 }; 56 57 /** 58 * Constructor. 59 * 60 * @param {object} settingsModel - model of setting. 61 */ 62 constructor() { 63 this.mSettingsModel = SettingsModel.getInstance(); 64 this.mSettingItemsManager = (new SettingItemsManager()). 65 withChecker(SettingItemsConfig.SETTING_ITEM_LAYOUT_OPTIONS, this.mLayoutOptionsChecker). 66 withChecker(SettingItemsConfig.SETTING_ITEM_PHONE_GRID_LAYOUT_OPTIONS, this.mGridLayOutOptionsChecker). 67 withChecker(SettingItemsConfig.SETTING_ITEM_PAD_GRID_LAYOUT_OPTIONS, this.mGridLayOutOptionsChecker); 68 } 69 70 /** 71 * Get settingsPresenter instance. 72 * 73 * @return {settingPresenter} - settingPresenter. 74 */ 75 static getInstance(): SettingsPresenter{ 76 if (globalThis.SettingsPresenter == null) { 77 globalThis.SettingsPresenter = new SettingsPresenter(); 78 } 79 return globalThis.SettingsPresenter; 80 } 81 82 /** 83 * Get setting list. 84 * 85 * @return [settingList] - setting list. 86 */ 87 getSettingList(): SettingItemInfo[] { 88 const deviceType = this.mSettingsModel.getDevice() == 'phone' ? 89 SettingItemsConfig.DEVICE_TYPE_PHONE : SettingItemsConfig.DEVICE_TYPE_PAD; 90 91 const condition = this.mSettingsModel.getAppPageStartConfig() == 'Grid' ? 92 SettingItemsConfig.CONDITION_GRID_LAYOUT_ENABLE : SettingItemsConfig.CONDITION_LIST_LAYOUT_ENABLE; 93 94 Log.showDebug(TAG, 'getSettingList, deviceType is '+ deviceType + ', condition is ' + condition); 95 return this.mSettingItemsManager.get(deviceType, condition); 96 } 97 98 /** 99 * Set system setting value. 100 * 101 * @param {string} settingsName - setting name. 102 * @param {string} settingValue - setting value. 103 */ 104 setSettingsValue(ida, settingValue) { 105 Log.showDebug(TAG, 'setSettingsValue, ida is '+ ida + ', settingValue is ' + settingValue); 106 107 if (ida == SettingsPresenter.SETTINGS_INDEX_STYLE) { 108 this.setAppPageStartConfig(settingValue); 109 } else if (ida == SettingsPresenter.SETTINGS_INDEX_GRID_LAYOUT) { 110 const idx = this.mSettingItemsManager.gridLayoutValue2Idx(settingValue); 111 Log.showDebug(TAG, 'setSettingsValue, idx is '+ idx); 112 this.setGridConfig(idx); 113 } else { 114 this.setRecentMissionsLimit(settingValue); 115 } 116 this.settingUpdate(); 117 } 118 119 /** 120 * Set app start config. 121 * 122 * @param {string} type - the type of config. 123 */ 124 setAppPageStartConfig(type) { 125 this.mSettingsModel.setAppPageStartConfig(type); 126 } 127 128 /** 129 * Update setting. 130 * 131 */ 132 settingUpdate() { 133 Log.showDebug(TAG, 'settingUpdate start'); 134 globalThis.settingsContext.terminateSelf() 135 .then(data => Log.showDebug(TAG, 'terminateSelf promise::then : ' + data)) 136 .catch(error => Log.showError(TAG, 'terminateSelf promise::catch : ' + error)); 137 Log.showDebug(TAG, 'terminateSelf end'); 138 } 139 140 /** 141 * Set grid config. 142 * 143 * @param {string} id - the id of grid config. 144 */ 145 setGridConfig(id) { 146 this.mSettingsModel.setGridConfig(id); 147 } 148 149 /** 150 * Set recent missions limit. 151 * 152 * @param {number} num - the num of recent missions. 153 */ 154 setRecentMissionsLimit(num) { 155 this.mSettingsModel.setRecentMissionsLimit(num); 156 } 157 158 /** 159 * Back to the desktop interface. 160 * 161 */ 162 backToTheDesktop() { 163 Log.showDebug(TAG, 'backToTheDesktop!'); 164 this.settingUpdate(); 165 } 166 167 /** 168 * Register value callback. 169 * 170 * @param {string} settingsName - setting name. 171 * @param {function()} settingValue - setting value. 172 */ 173 registerValueCallback(ida, settingValue) { 174 this.mCallbackList.push({ 175 id: ida, 176 fun: settingValue 177 }); 178 } 179 180 /** 181 * Change page setting value. 182 * 183 * @param {string} settingsName - setting name. 184 * @param {string} settingValue - setting value. 185 */ 186 changeSettingValue(ida, settingValue) { 187 for (let i = 0;i < this.mCallbackList.length; i++) { 188 if (this.mCallbackList[i].id == ida) { 189 this.mCallbackList[i].fun(settingValue); 190 break; 191 } 192 } 193 } 194 195 /** 196 * get the device type. 197 * 198 * @return {string} device type 199 */ 200 getDevice(): string { 201 return this.mSettingsModel.getDevice(); 202 } 203 204 sendLocalEvent(value: string) { 205 Log.showDebug(TAG, `setValue value: ${value}`); 206 if (value !== '1' && value !== '0') { 207 Log.showDebug(TAG, 'setValue error'); 208 return; 209 } 210 this.mSettingsModel.setValue(value); 211 } 212 213 initNavigationBarStatusValue() { 214 try { 215 const initValue = this.mSettingsModel.getValue(); 216 const navigationBarStatusValue = initValue === '0' ? true : false; 217 Log.showDebug(TAG, `initNavigationBarStatusValue initValue:${initValue}, navigationBarStatusValue:${navigationBarStatusValue}`); 218 AppStorage.setOrCreate('NavigationBarStatusValue', navigationBarStatusValue); 219 } catch (e) { 220 Log.showError(TAG, `initNavigationBarStatusValue error: ${e.toString()}`); 221 } 222 } 223}