• 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 '../utils/Log';
17import { ILayoutConfig } from './ILayoutConfig';
18import { CommonConstants } from '../constants/CommonConstants';
19import FileUtils from '../utils/FileUtils';
20import { GridLayoutInfo } from '../interface';
21
22const TAG = 'PageDesktopLayoutConfig';
23
24/**
25 * Desktop workspace function layout configuration.
26 */
27export class PageDesktopLayoutConfig extends ILayoutConfig {
28  /**
29   * Workspace Feature Layout Configuration Index.
30   */
31  static GRID_LAYOUT_INFO = 'GridLayoutInfo';
32
33  private static readonly DEFAULT_PAGE_COUNT = 1;
34
35  private static readonly DEFAULT_ROW_COUNT = 5;
36
37  private static readonly DEFAULT_COLUMN_COUNT = 4;
38
39  private static readonly DEFAULT_LAYOUT_INFO: GridLayoutInfo = {
40    layoutDescription: {
41      pageCount: PageDesktopLayoutConfig.DEFAULT_PAGE_COUNT,
42      row: PageDesktopLayoutConfig.DEFAULT_ROW_COUNT,
43      column: PageDesktopLayoutConfig.DEFAULT_COLUMN_COUNT
44    },
45    layoutInfo: []
46  };
47
48  private mGridLayoutInfo: GridLayoutInfo = PageDesktopLayoutConfig.DEFAULT_LAYOUT_INFO;
49
50  locked: boolean = false;
51
52  protected constructor() {
53    super();
54  }
55
56  /**
57   * Get an instance of the workspace function layout configuration
58   */
59  static getInstance(): PageDesktopLayoutConfig {
60    if (globalThis.PageDesktopLayoutConfigInstance == null) {
61      globalThis.PageDesktopLayoutConfigInstance = new PageDesktopLayoutConfig();
62      globalThis.PageDesktopLayoutConfigInstance.initConfig();
63    }
64    return globalThis.PageDesktopLayoutConfigInstance;
65  }
66
67  initConfig(): void {
68    this.loadPersistConfig();
69  }
70
71  getConfigLevel(): string {
72    return CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON;
73  }
74
75  getConfigType(): number {
76    return CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION;
77  }
78
79  getConfigName(): string {
80    return PageDesktopLayoutConfig.GRID_LAYOUT_INFO;
81  }
82
83  protected getPersistConfigJson(): string {
84    return JSON.stringify(this.mGridLayoutInfo);
85  }
86
87  /**
88   * Update workspace layout data.
89   *
90   * @params gridLayoutInfo
91   */
92  updateGridLayoutInfo(gridLayoutInfo: GridLayoutInfo): void {
93    const temp = gridLayoutInfo;
94    FileUtils.writeStringToFile(JSON.stringify(temp), this.getConfigFileAbsPath());
95    this.mGridLayoutInfo = gridLayoutInfo;
96    Log.showInfo(TAG, 'srj updateGridLayoutInfo...');
97
98    const UPDATE_RDB_GRID_LAYOUT_INFO_INTERVAL = 30;
99    const timer = setInterval(() => {
100      if (!this.locked) {
101        this.locked = true;
102        globalThis.RdbStoreManagerInstance.insertGridLayoutInfo(gridLayoutInfo).then((result) => {
103          this.locked = false;
104          clearInterval(timer);
105          Log.showInfo(TAG, 'srj updateGridLayoutInfo success.');
106        }).catch((err) => {
107          this.locked = false;
108          clearInterval(timer);
109          Log.showError(TAG, `srj updateGridLayoutInfo error: ${err.toString()}`);
110        });
111      }
112    }, UPDATE_RDB_GRID_LAYOUT_INFO_INTERVAL);
113  }
114
115  /**
116   * Get workspace layout data
117   *
118   * @return Workspace layout data
119   */
120  getGridLayoutInfo(): GridLayoutInfo {
121    return this.mGridLayoutInfo;
122  }
123
124  /**
125   * load configuration
126   */
127  async loadPersistConfig(): Promise<void> {
128    Log.showDebug(TAG, 'loadPersistConfig start');
129    let defaultConfig = super.loadPersistConfig();
130    const configFromFile = FileUtils.readStringFromFile(this.getConfigFileAbsPath());
131    if (configFromFile) {
132      defaultConfig = JSON.parse(configFromFile);
133    }
134    const configFromRdb = await globalThis.RdbStoreManagerInstance.queryGridLayoutInfo();
135    if (configFromRdb) {
136      defaultConfig.layoutInfo = configFromRdb;
137    }
138    this.mGridLayoutInfo = defaultConfig;
139  }
140}
141