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';
19
20const TAG = 'PageDesktopAppModeConfig';
21
22/**
23 * Desktop Workspace App Configuration
24 */
25export class PageDesktopAppModeConfig extends ILayoutConfig {
26  /**
27   * Workspace Feature Layout Configuration Index
28   */
29  static DESKTOP_APPLICATION_INFO = 'DesktopApplicationInfo';
30
31  private static readonly DEFAULT_LAYOUT_INFO: any = [];
32
33  private mAppListInfo: any = PageDesktopAppModeConfig.DEFAULT_LAYOUT_INFO;
34
35  protected constructor() {
36    super();
37  }
38
39  /**
40   * Get an instance of the workspace function layout configuration
41   */
42  static getInstance(): PageDesktopAppModeConfig {
43    if (globalThis.PageDesktopAppModeConfig == null) {
44      globalThis.PageDesktopAppModeConfig = new PageDesktopAppModeConfig();
45      globalThis.PageDesktopAppModeConfig.initConfig();
46    }
47    return globalThis.PageDesktopAppModeConfig;
48  }
49
50  initConfig(): void {
51    this.loadPersistConfig();
52  }
53
54  getConfigLevel(): string {
55    return CommonConstants.LAYOUT_CONFIG_LEVEL_COMMON;
56  }
57
58  getConfigType(): number {
59    return CommonConstants.LAYOUT_CONFIG_TYPE_MODE;
60  }
61
62  getConfigName(): string {
63    return PageDesktopAppModeConfig.DESKTOP_APPLICATION_INFO;
64  }
65
66  protected getPersistConfigJson(): string {
67    return JSON.stringify(this.mAppListInfo);
68  }
69
70  /**
71   * update appList in desktop
72   *
73   * @params appListInfo
74   */
75  updateAppListInfo(appListInfo: object): void {
76    this.mAppListInfo = appListInfo;
77    super.persistConfig();
78    globalThis.RdbStoreManagerInstance.insertDesktopApplication(appListInfo).then((result) => {
79      Log.showDebug(TAG, 'updateAppListInfo success.');
80    }).catch((err) => {
81      Log.showError(TAG, `updateAppListInfo error: ${err.toString()}`);
82    });
83  }
84
85  /**
86   * Get workspace shortcuts
87   *
88   * @return Workspace shortcuts
89   */
90  getAppListInfo(): any {
91    return this.mAppListInfo;
92  }
93
94  /**
95   * load configuration
96   */
97  loadPersistConfig(): void {
98    let defaultConfig = super.loadPersistConfig();
99    globalThis.RdbStoreManagerInstance.queryDesktopApplication()
100      .then((config) => {
101        Log.showDebug(TAG, 'loadPersistConfig configFromRdb success.');
102        this.mAppListInfo = config;
103    }).catch((err) => {
104      Log.showError(TAG, `loadPersistConfig configFromRdb err: ${err.toString()}`);
105      this.mAppListInfo = defaultConfig;
106    });
107  }
108}
109