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 { ILayoutConfig } from '@ohos/common';
17import { CommonConstants } from '@ohos/common';
18import { Log } from '@ohos/common';
19/**
20 * Desktop Dock function layout configuration
21 */
22export class SmartDockLayoutConfig extends ILayoutConfig {
23  /**
24   * Dock Feature Layout Configuration Index
25   */
26  static SMART_DOCK_LAYOUT_INFO = 'SmartDockLayoutInfo';
27
28  /**
29   * Dock function layout data
30   */
31  protected mDockLayoutInfo: any = [];
32
33  protected constructor() {
34    super();
35  }
36
37  /**
38   * Get an instance of the workspace function layout configuration
39   */
40  static getInstance(): SmartDockLayoutConfig {
41    if (globalThis.SmartDockLayoutConfig == null) {
42      globalThis.SmartDockLayoutConfig = new SmartDockLayoutConfig();
43      globalThis.SmartDockLayoutConfig.initConfig();
44    }
45    return globalThis.SmartDockLayoutConfig;
46  }
47
48  initConfig(): void {
49    const config = this.loadPersistConfig();
50    this.mDockLayoutInfo = config;
51  }
52
53  getConfigLevel(): string {
54    return CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON;
55  }
56
57  getConfigType(): number {
58    return CommonConstants.LAYOUT_CONFIG_TYPE_FUNCTION;
59  }
60
61  getConfigName(): string {
62    return SmartDockLayoutConfig.SMART_DOCK_LAYOUT_INFO;
63  }
64
65  protected getPersistConfigJson(): string {
66    return JSON.stringify(this.mDockLayoutInfo);
67  }
68
69  /**
70   * Update dock layout data
71   *
72   * @params gridLayoutInfo:dock layout data
73   */
74  updateDockLayoutInfo(dockLayoutInfo: object): void {
75    this.mDockLayoutInfo = dockLayoutInfo;
76    super.persistConfig();
77  }
78
79  /**
80   * Get dock layout data
81   *
82   * @return dock layout data
83   */
84  getDockLayoutInfo(): any {
85    return this.mDockLayoutInfo;
86  }
87
88  /**
89   * load configuration
90   */
91  loadPersistConfig(): any {
92    let defaultConfig = super.loadPersistConfig();
93    const configFromFile = this.loadPersistConfigFromFile();
94    if (configFromFile) {
95      try {
96        defaultConfig = JSON.parse(configFromFile);
97      } catch (err) {
98        Log.showInfo('SmartDockLayoutConfig', `configFromFile cannot use json: ${defaultConfig}`);
99      }
100    }
101    return defaultConfig;
102  }
103}
104