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 ]; 36 37 /** 38 * 将变量value保存到AppStorage对象中,key需要先注册到registerKeys,否则挂载失败 39 * 注意本接口在AppStorage对象中不包含key时,会在AppStorage对象中新增该key值 40 * 41 * @returns 挂载成功返回value,失败返回undefined 42 */ 43 public static createValue<T>(value: T, storageKey: string): T { 44 Log.info(TAG, 'createValue' + JSON.stringify(storageKey) + ' : ' + JSON.stringify(value)) 45 const element = AppStorageHelper.registerKeys.find((ele) => ele === storageKey); 46 if (element === undefined) { 47 Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey)); 48 return undefined; 49 } 50 if (!AppStorage.has(storageKey)) { 51 AppStorage.setOrCreate<T>(storageKey, value) 52 Log.error(TAG, 'AppStorageHelper Create key of ' + JSON.stringify(storageKey)); 53 } else { 54 Log.info(TAG, 'setValue' + JSON.stringify(storageKey) + ' : ' + JSON.stringify(value)) 55 AppStorage.set<T>(storageKey, value) 56 } 57 return AppStorageHelper.getValue<T>(storageKey) 58 } 59 60 /** 61 * 将变量value保存到AppStorage对象中,key需要先注册到registerKeys,否则挂载失败 62 * 注意本接口在AppStorage对象中不包含key时,不会在AppStorage对象中新增该key值,需新增key值不要使用本接口 63 * 64 * @returns 挂载成功返回value,失败返回undefined 65 */ 66 public static setValue<T>(value: T, storageKey: string): boolean { 67 const element = AppStorageHelper.registerKeys.find((ele) => ele === storageKey); 68 if (element === undefined) { 69 Log.error(TAG, 'Can not find register storageKey: ' + JSON.stringify(storageKey)); 70 return false 71 } 72 if (AppStorage.has(storageKey)) { 73 AppStorage.set<T>(storageKey, value) 74 return true 75 } 76 return false 77 } 78 79 /** 80 * 获取挂载到AppStorage上的全局变量 81 * 82 * @returns 成功返回value,若之前未挂载则返回undefined 83 */ 84 public static getValue<T>(storageKey: string): T { 85 if (!AppStorage.has(storageKey)) { 86 Log.error(TAG, 'The storageKey is not exist, key = ' + JSON.stringify(storageKey)); 87 return undefined 88 } 89 return (AppStorage.get<T>(storageKey) as T) 90 } 91}