16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ciimport { Log } from '../utils/Log'; 176e80583aSopenharmony_ciimport { ILayoutConfig } from './ILayoutConfig'; 186e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants'; 196e80583aSopenharmony_ciimport FileUtils from '../utils/FileUtils'; 206e80583aSopenharmony_ciimport { GridLayoutInfo } from '../interface'; 216e80583aSopenharmony_ci 226e80583aSopenharmony_ciconst TAG = 'PageDesktopLayoutConfig'; 236e80583aSopenharmony_ci 246e80583aSopenharmony_ci/** 256e80583aSopenharmony_ci * Desktop workspace function layout configuration. 266e80583aSopenharmony_ci */ 276e80583aSopenharmony_ciexport class PageDesktopLayoutConfig extends ILayoutConfig { 286e80583aSopenharmony_ci /** 296e80583aSopenharmony_ci * Workspace Feature Layout Configuration Index. 306e80583aSopenharmony_ci */ 316e80583aSopenharmony_ci static GRID_LAYOUT_INFO = 'GridLayoutInfo'; 326e80583aSopenharmony_ci 336e80583aSopenharmony_ci private static readonly DEFAULT_PAGE_COUNT = 1; 346e80583aSopenharmony_ci 356e80583aSopenharmony_ci private static readonly DEFAULT_ROW_COUNT = 5; 366e80583aSopenharmony_ci 376e80583aSopenharmony_ci private static readonly DEFAULT_COLUMN_COUNT = 4; 386e80583aSopenharmony_ci 396e80583aSopenharmony_ci private static readonly DEFAULT_LAYOUT_INFO: GridLayoutInfo = { 406e80583aSopenharmony_ci layoutDescription: { 416e80583aSopenharmony_ci pageCount: PageDesktopLayoutConfig.DEFAULT_PAGE_COUNT, 426e80583aSopenharmony_ci row: PageDesktopLayoutConfig.DEFAULT_ROW_COUNT, 436e80583aSopenharmony_ci column: PageDesktopLayoutConfig.DEFAULT_COLUMN_COUNT 446e80583aSopenharmony_ci }, 456e80583aSopenharmony_ci layoutInfo: [] 466e80583aSopenharmony_ci }; 476e80583aSopenharmony_ci 486e80583aSopenharmony_ci private mGridLayoutInfo: GridLayoutInfo = PageDesktopLayoutConfig.DEFAULT_LAYOUT_INFO; 496e80583aSopenharmony_ci 506e80583aSopenharmony_ci locked: boolean = false; 516e80583aSopenharmony_ci 526e80583aSopenharmony_ci protected constructor() { 536e80583aSopenharmony_ci super(); 546e80583aSopenharmony_ci } 556e80583aSopenharmony_ci 566e80583aSopenharmony_ci /** 576e80583aSopenharmony_ci * Get an instance of the workspace function layout configuration 586e80583aSopenharmony_ci */ 596e80583aSopenharmony_ci static getInstance(): PageDesktopLayoutConfig { 606e80583aSopenharmony_ci if (globalThis.PageDesktopLayoutConfigInstance == null) { 616e80583aSopenharmony_ci globalThis.PageDesktopLayoutConfigInstance = new PageDesktopLayoutConfig(); 626e80583aSopenharmony_ci globalThis.PageDesktopLayoutConfigInstance.initConfig(); 636e80583aSopenharmony_ci } 646e80583aSopenharmony_ci return globalThis.PageDesktopLayoutConfigInstance; 656e80583aSopenharmony_ci } 666e80583aSopenharmony_ci 676e80583aSopenharmony_ci initConfig(): void { 686e80583aSopenharmony_ci this.loadPersistConfig(); 696e80583aSopenharmony_ci } 706e80583aSopenharmony_ci 716e80583aSopenharmony_ci getConfigLevel(): string { 726e80583aSopenharmony_ci return CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON; 736e80583aSopenharmony_ci } 746e80583aSopenharmony_ci 756e80583aSopenharmony_ci getConfigType(): number { 766e80583aSopenharmony_ci return CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION; 776e80583aSopenharmony_ci } 786e80583aSopenharmony_ci 796e80583aSopenharmony_ci getConfigName(): string { 806e80583aSopenharmony_ci return PageDesktopLayoutConfig.GRID_LAYOUT_INFO; 816e80583aSopenharmony_ci } 826e80583aSopenharmony_ci 836e80583aSopenharmony_ci protected getPersistConfigJson(): string { 846e80583aSopenharmony_ci return JSON.stringify(this.mGridLayoutInfo); 856e80583aSopenharmony_ci } 866e80583aSopenharmony_ci 876e80583aSopenharmony_ci /** 886e80583aSopenharmony_ci * Update workspace layout data. 896e80583aSopenharmony_ci * 906e80583aSopenharmony_ci * @params gridLayoutInfo 916e80583aSopenharmony_ci */ 926e80583aSopenharmony_ci updateGridLayoutInfo(gridLayoutInfo: GridLayoutInfo): void { 936e80583aSopenharmony_ci const temp = gridLayoutInfo; 946e80583aSopenharmony_ci FileUtils.writeStringToFile(JSON.stringify(temp), this.getConfigFileAbsPath()); 956e80583aSopenharmony_ci this.mGridLayoutInfo = gridLayoutInfo; 966e80583aSopenharmony_ci Log.showInfo(TAG, 'srj updateGridLayoutInfo...'); 976e80583aSopenharmony_ci 986e80583aSopenharmony_ci const UPDATE_RDB_GRID_LAYOUT_INFO_INTERVAL = 30; 996e80583aSopenharmony_ci const timer = setInterval(() => { 1006e80583aSopenharmony_ci if (!this.locked) { 1016e80583aSopenharmony_ci this.locked = true; 1026e80583aSopenharmony_ci globalThis.RdbStoreManagerInstance.insertGridLayoutInfo(gridLayoutInfo).then((result) => { 1036e80583aSopenharmony_ci this.locked = false; 1046e80583aSopenharmony_ci clearInterval(timer); 1056e80583aSopenharmony_ci Log.showInfo(TAG, 'srj updateGridLayoutInfo success.'); 1066e80583aSopenharmony_ci }).catch((err) => { 1076e80583aSopenharmony_ci this.locked = false; 1086e80583aSopenharmony_ci clearInterval(timer); 1096e80583aSopenharmony_ci Log.showError(TAG, `srj updateGridLayoutInfo error: ${err.toString()}`); 1106e80583aSopenharmony_ci }); 1116e80583aSopenharmony_ci } 1126e80583aSopenharmony_ci }, UPDATE_RDB_GRID_LAYOUT_INFO_INTERVAL); 1136e80583aSopenharmony_ci } 1146e80583aSopenharmony_ci 1156e80583aSopenharmony_ci /** 1166e80583aSopenharmony_ci * Get workspace layout data 1176e80583aSopenharmony_ci * 1186e80583aSopenharmony_ci * @return Workspace layout data 1196e80583aSopenharmony_ci */ 1206e80583aSopenharmony_ci getGridLayoutInfo(): GridLayoutInfo { 1216e80583aSopenharmony_ci return this.mGridLayoutInfo; 1226e80583aSopenharmony_ci } 1236e80583aSopenharmony_ci 1246e80583aSopenharmony_ci /** 1256e80583aSopenharmony_ci * load configuration 1266e80583aSopenharmony_ci */ 1276e80583aSopenharmony_ci async loadPersistConfig(): Promise<void> { 1286e80583aSopenharmony_ci Log.showDebug(TAG, 'loadPersistConfig start'); 1296e80583aSopenharmony_ci let defaultConfig = super.loadPersistConfig(); 1306e80583aSopenharmony_ci const configFromFile = FileUtils.readStringFromFile(this.getConfigFileAbsPath()); 1316e80583aSopenharmony_ci if (configFromFile) { 1326e80583aSopenharmony_ci defaultConfig = JSON.parse(configFromFile); 1336e80583aSopenharmony_ci } 1346e80583aSopenharmony_ci const configFromRdb = await globalThis.RdbStoreManagerInstance.queryGridLayoutInfo(); 1356e80583aSopenharmony_ci if (configFromRdb) { 1366e80583aSopenharmony_ci defaultConfig.layoutInfo = configFromRdb; 1376e80583aSopenharmony_ci } 1386e80583aSopenharmony_ci this.mGridLayoutInfo = defaultConfig; 1396e80583aSopenharmony_ci } 1406e80583aSopenharmony_ci} 141