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