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