1/*
2 * Copyright (c) 2023-2023 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 '@ohos/common';
17import { AppStorageKeyName } from '@ohos/common'
18
19const TAG = '[AppStorageHelper]:'
20
21/**
22 * AppStorageHelper
23 *
24 * @since 2022-08-02
25 */
26export default class AppStorageHelper {
27  private static registerKeys = [
28    AppStorageKeyName.JOB_QUEUE_NAME,
29    AppStorageKeyName.PRINTER_QUEUE_NAME,
30    AppStorageKeyName.PRINT_EXTENSION_LIST_NAME,
31    AppStorageKeyName.CONFIG_LANGUAGE,
32    AppStorageKeyName.START_PRINT_TIME,
33    AppStorageKeyName.INGRESS_PACKAGE,
34    AppStorageKeyName.APP_VERSION,
35    AppStorageKeyName.DOCUMENT_NAME,
36    AppStorageKeyName.PREVIEW_PAGE_INSTANCE,
37    AppStorageKeyName.imageSourcesName,
38  ];
39
40  /**
41   * 将变量value保存到AppStorage对象中,key需要先注册到registerKeys,否则挂载失败
42   * 注意本接口在AppStorage对象中不包含key时,会在AppStorage对象中新增该key值
43   *
44   * @returns 挂载成功返回value,失败返回undefined
45   */
46  public static createValue<T>(value: T, storageKey: string): T {
47    Log.info(TAG, 'createValue' + JSON.stringify(storageKey) + ' : ' + JSON.stringify(value))
48    const element = AppStorageHelper.registerKeys.find((ele) => ele === storageKey);
49    if (element === undefined) {
50      Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey));
51      return undefined;
52    }
53    if (!AppStorage.has(storageKey)) {
54      AppStorage.setOrCreate<T>(storageKey, value)
55      Log.error(TAG, 'AppStorageHelper Create key of ' + JSON.stringify(storageKey));
56    } else {
57      Log.info(TAG, 'setValue' + JSON.stringify(storageKey) + ' : ' + JSON.stringify(value))
58      AppStorage.set<T>(storageKey, value)
59    }
60    return AppStorageHelper.getValue<T>(storageKey)
61  }
62
63  /**
64   * 将变量value保存到AppStorage对象中,key需要先注册到registerKeys,否则挂载失败
65   * 注意本接口在AppStorage对象中不包含key时,不会在AppStorage对象中新增该key值,需新增key值不要使用本接口
66   *
67   * @returns 挂载成功返回value,失败返回undefined
68   */
69  public static setValue<T>(value: T, storageKey: string): boolean {
70    const element = AppStorageHelper.registerKeys.find((ele) => ele === storageKey);
71    if (element === undefined) {
72      Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey));
73      return false
74    }
75    if (AppStorage.has(storageKey)) {
76      AppStorage.set<T>(storageKey, value)
77      return true
78    }
79    return false
80  }
81
82  /**
83   * 清除AppStorage对象的值,key需要先注册到registerKeys,否则挂载失败
84   * 如果AppStorage中有对应的属性,且该属性已经没有订阅者,则删除成功,返回true。如果属性不存在,或者该属性还存在订阅者,则返回false
85   *
86   * @returns 删除成功返回true,失败返回false
87   */
88  public static deleteValue<T>(storageKey: string): boolean {
89    const element = AppStorageHelper.registerKeys.find((ele) => ele === storageKey);
90    if (element === undefined) {
91      Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey));
92      return false;
93    }
94    if (AppStorage.has(storageKey)) {
95      AppStorage.delete(storageKey);
96      return true;
97    }
98    return false;
99  }
100
101  /**
102   * 获取挂载到AppStorage上的全局变量
103   *
104   * @returns 成功返回value,若之前未挂载则返回undefined
105   */
106  public static getValue<T>(storageKey: string): T {
107    if (!AppStorage.has(storageKey)) {
108      Log.error(TAG, 'The storageKey is not exist, key =  ' + JSON.stringify(storageKey));
109      return undefined
110    }
111    return (AppStorage.get<T>(storageKey) as T)
112  }
113}