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 '../../../../../../../../common/src/main/ets/default/Log'; 17import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 18import { ControlComponentData, ControlCenterConfig } from '../common/Constants'; 19import ControlCenterService from '../model/ControlCenterService'; 20import { PluginType, PluginComponentData 21} from '../../../../../../../../common/src/main/ets/plugindatasource/common/Constants'; 22 23export const CONTROL_CENTER_COMPLEX_TOGGLE_LAYOUT_KEY = 'ControlCenterComplexToggleLayout'; 24 25export const CONTROL_CENTER_SIMPLE_TOGGLE_LAYOUT_KEY = 'ControlCenterSimpleToggleLayout'; 26 27const TAG = 'ControlCenterVM'; 28 29export class ControlCenterVM { 30 mIsStart = false; 31 mComplexToggleLayout: SubscribedAbstractProperty<string[]>; 32 mSimpleToggleLayout: SubscribedAbstractProperty<string[]>; 33 34 constructor() { 35 Log.showDebug(TAG, 'constructor'); 36 this.mComplexToggleLayout = AppStorage.SetAndLink(CONTROL_CENTER_COMPLEX_TOGGLE_LAYOUT_KEY, new Array<string>()); 37 this.mSimpleToggleLayout = AppStorage.SetAndLink(CONTROL_CENTER_SIMPLE_TOGGLE_LAYOUT_KEY, new Array<string>()); 38 ControlCenterService.registerListener(this); 39 } 40 41 initViewModel(config: ControlCenterConfig, moduleName: string): void { 42 if (this.mIsStart) { 43 return; 44 } 45 Log.showInfo(TAG, `initViewModel, config: ${JSON.stringify(config)} `); 46 this.mIsStart = true; 47 48 ControlCenterService.startService(config, moduleName); 49 } 50 51 setComplexToggleLayout(layout: string[]): void{ 52 Log.showDebug(TAG, `setComplexToggleLayout, layout: ${JSON.stringify(layout)}`); 53 if (JSON.stringify(layout) != JSON.stringify(this.mComplexToggleLayout.get())) { 54 this.mComplexToggleLayout.set(layout); 55 } 56 Log.showDebug(TAG, `setComplexToggleLayout, mComplexToggleLayout: ${JSON.stringify(this.mComplexToggleLayout.get())}`); 57 } 58 59 setSimpleToggleLayout(layout: string[]): void{ 60 Log.showDebug(TAG, `setSimpleToggleLayout, layout: ${JSON.stringify(layout)}`); 61 if (JSON.stringify(layout) != JSON.stringify(this.mSimpleToggleLayout.get())) { 62 this.mSimpleToggleLayout.set(layout); 63 } 64 Log.showDebug(TAG, `setSimpleToggleLayout, mSimpleToggleLayout: ${JSON.stringify(this.mSimpleToggleLayout.get())}`); 65 } 66 67 setItemData(id: string, itemData: ControlComponentData): void{ 68 Log.showDebug(TAG, `setItemData, id: ${id} itemData: ${JSON.stringify(itemData)}`); 69 let storageKey: string = 'ControlCenter_' + id; 70 if (itemData) { 71 AppStorage.SetOrCreate(storageKey, itemData); 72 } else { 73 let deleteRs: boolean = AppStorage.Delete(storageKey); 74 Log.showDebug(TAG, `setItemData, AppStorage.Delete rs: ${deleteRs} `); 75 } 76 this.setPluginData(id, itemData); 77 } 78 79 80 setPluginData(id: string, itemData: ControlComponentData): void{ 81 Log.showInfo(TAG, `setPluginData, itemData: ${JSON.stringify(itemData)}`); 82 if (itemData && itemData.pluginType == PluginType.PLUGIN_COMPONENT) { 83 let data = undefined; 84 if (itemData.actionData.pluginData && itemData.actionData.pluginData.data) { 85 data = JSON.parse(JSON.stringify(itemData.actionData.pluginData.data)); 86 } 87 let pluginData = this.getPluginData(id); 88 pluginData.template = itemData.actionData.pluginData.template; 89 pluginData.data = data; 90 Log.showInfo(TAG, `setPluginData, pluginData: ${JSON.stringify(pluginData)} `); 91 } else { 92 let storageKey = 'ControlCenter_PluginIcon_' + id; 93 if (AppStorage.Has(storageKey)) { 94 let deleteRs: boolean = AppStorage.Delete(storageKey); 95 Log.showInfo(TAG, `setPluginData, AppStorage.Delete rs: ${String(deleteRs)} `); 96 } 97 } 98 } 99 100 getPluginData(id: string): PluginComponentData { 101 Log.showInfo(TAG, `getPluginData, id: ${id}`); 102 let storageKey = 'ControlCenter_PluginIcon_' + id; 103 if (!AppStorage.Has(storageKey)) { 104 AppStorage.SetOrCreate(storageKey, new PluginComponentData()); 105 } 106 return AppStorage.Get(storageKey); 107 } 108 109 getDisplayingSimpleToggles(): string[]{ 110 let simpleToggles = this.mSimpleToggleLayout.get(); 111 Log.showDebug(TAG, `getDisplayingSimpleToggles, simpleToggles: ${JSON.stringify(simpleToggles)}`); 112 return simpleToggles; 113 } 114 115 getHidingSimpleToggles(): string[] { 116 Log.showDebug(TAG, 'getHidingSimpleToggles'); 117 return ControlCenterService.getHidingSimpleToggles(); 118 } 119 120 getDefaultSimpleToggleLayout(): string[] { 121 Log.showDebug(TAG, 'getDefaultSimpleToggleLayout'); 122 return ControlCenterService.getDefaultSimpleToggleLayout(); 123 } 124 125 saveSimpleToggleLayout(layout: string[]): void{ 126 Log.showDebug(TAG, `saveSimpleToggleLayout, layout: ${JSON.stringify(layout)}`); 127 ControlCenterService.saveSimpleToggleLayout(layout); 128 } 129} 130 131let sControlCenterVM = createOrGet(ControlCenterVM, TAG); 132 133export default sControlCenterVM;