1/* 2 * Copyright (c) 2022-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 */ 15import { Log } from './Log'; 16import { Constants } from '../model/common/Constants'; 17import data_preferences from '@ohos.data.preferences'; 18import contextConstant from '@ohos.app.ability.contextConstant'; 19import common from '@ohos.app.ability.common'; 20 21const TAG: string = 'common_DataStoreUtil'; 22 23export class DataStoreUtil { 24 private static readonly PREFERENCES_KEY_MY_FORM_STORE = 'myformstore'; 25 private preferences: data_preferences.Preferences = undefined; 26 27 private constructor() { 28 Log.info(TAG, 'new DataStoreUtil'); 29 } 30 31 public static getInstance(): DataStoreUtil { 32 if (AppStorage.get(Constants.FROM_DATA_STORE_UTIL) == null) { 33 AppStorage.SetOrCreate(Constants.FROM_DATA_STORE_UTIL, new DataStoreUtil()); 34 } 35 return AppStorage.get(Constants.FROM_DATA_STORE_UTIL); 36 } 37 38 public async init(): Promise<void> { 39 Log.debug(TAG, 'init start!'); 40 if (this.preferences) { 41 Log.debug(TAG, 'init already!'); 42 return; 43 } 44 let context: common.AbilityStageContext = AppStorage.get<common.AbilityStageContext>('photosGlobalContext'); 45 let area: contextConstant.AreaMode = context.area; 46 context.area = contextConstant.AreaMode.EL1; 47 try { 48 this.preferences = await data_preferences.getPreferences(context, DataStoreUtil.PREFERENCES_KEY_MY_FORM_STORE); 49 Log.info(TAG, `init preferences ${this.preferences}`); 50 } catch (err) { 51 Log.error(TAG, `init err ${err}`); 52 } 53 context.area = area; 54 Log.debug(TAG, 'init end!'); 55 return; 56 } 57 58 public async getData(key: string, defValue) { 59 Log.debug(TAG, 'getData start!'); 60 if (!this.preferences) { 61 Log.error(TAG, `getData preferences is undefined`); 62 await this.init(); 63 } 64 let temValue = defValue; 65 await this.preferences.get(key, defValue).then((value) => { 66 Log.debug(TAG, 'The value of startup is ' + value) 67 temValue = value; 68 }).catch((err) => { 69 Log.error(TAG, `Get the value failed with err: ${err}`) 70 }) 71 return temValue; 72 } 73 74 public async putData(key: string, value) { 75 Log.debug(TAG, 'putData start!'); 76 if (value === null || value === undefined) { 77 return; 78 } 79 80 if (!this.preferences) { 81 Log.error(TAG, 'putData preferences is undefined'); 82 await this.init(); 83 } 84 85 await this.preferences.put(key, value).then(() => { 86 Log.debug(TAG, 'Put the value successfully.'); 87 }).catch((err) => { 88 Log.error(TAG, `Put the value failed with err: ${err}`); 89 }) 90 } 91 92 public async delData(key: string) { 93 Log.debug(TAG, 'delData start!'); 94 if (!this.preferences) { 95 Log.error(TAG, `delData preferences is undefined`); 96 await this.init(); 97 } 98 await this.preferences.delete(key).then(() => { 99 Log.debug(TAG, 'Delete the value successfully.'); 100 }).catch((err) => { 101 Log.error(TAG, `Delete the value failed with err: ${err}`); 102 }) 103 } 104 105 public async flush() { 106 Log.debug(TAG, 'flush start!'); 107 if (!this.preferences) { 108 Log.error(TAG, `flush preferences is undefined`); 109 await this.init(); 110 } 111 await this.preferences.flush(); 112 } 113 114 public async hasData(key: string) { 115 Log.debug(TAG, `hasData start! preferences ${this.preferences}`); 116 let ret = false; 117 if (!this.preferences) { 118 Log.error(TAG, `hasData preferences is undefined`); 119 await this.init(); 120 } 121 122 await this.preferences.has(key).then((value) => { 123 Log.debug(TAG, `hasData the value successfully. ${value}}`); 124 ret = value; 125 }).catch((err) => { 126 Log.error(TAG, `hasData the value failed with err: ${err}`); 127 ret = false; 128 }) 129 return ret; 130 } 131 132 public async removeCache() { 133 Log.debug(TAG, 'removeCache start!'); 134 let context: common.AbilityStageContext = AppStorage.get<common.AbilityStageContext>('photosGlobalContext'); 135 let area: contextConstant.AreaMode = context.area; 136 context.area = contextConstant.AreaMode.EL1; 137 try { 138 await data_preferences.removePreferencesFromCache(context, DataStoreUtil.PREFERENCES_KEY_MY_FORM_STORE); 139 this.preferences = undefined; 140 Log.info(TAG, 'removeCache successfully.') 141 } catch (err) { 142 Log.info(TAG, 'removeCache failed with err: ' + err) 143 } 144 context.area = area; 145 } 146} 147