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: preferences.Preferences = undefined; 23 private static readonly PREFERENCES_KEY_MY_FORM_STORE = 'myFormStore'; 24 25 constructor() { 26 } 27 28 public async init(): Promise<void> { 29 Log.debug(TAG, 'init start!'); 30 if (this.preferences != undefined) { 31 Log.info(TAG, `init preferences before`); 32 return; 33 } 34 try { 35 let context = globalThis.applicationContext; 36 this.preferences = await preferences.getPreferences(context, DataStoreUtil.PREFERENCES_KEY_MY_FORM_STORE); 37 Log.info(TAG, `init preferences ${preferences}`); 38 } catch (err) { 39 Log.error(TAG, `init err ${err}`); 40 } 41 Log.debug(TAG, 'init end!'); 42 } 43 44 public async getData(key: string, defValue) { 45 Log.debug(TAG, 'getData start!'); 46 if (this.preferences == undefined) { 47 Log.warn(TAG, `getData preferences is undefined`); 48 return defValue; 49 } 50 let temValue = defValue; 51 try { 52 temValue = await this.preferences.get(key, defValue); 53 Log.debug(TAG, "The value of startup is " + temValue); 54 } catch (err) { 55 Log.error(TAG, `Get the value failed with err: ${err}`); 56 } 57 return temValue; 58 } 59 60 public async putData(key: string, value) { 61 Log.debug(TAG, 'putData start!'); 62 if (this.preferences == undefined) { 63 Log.warn(TAG, 'putData preferences is undefined'); 64 return; 65 } 66 67 try { 68 await this.preferences.put(key, value); 69 Log.debug(TAG, 'Put the value successfully.'); 70 } catch (err) { 71 Log.error(TAG, `Put the value failed with err: ${err}`); 72 } 73 } 74 75 public async delData(key: string) { 76 Log.debug(TAG, 'delData start!'); 77 if (this.preferences == undefined) { 78 Log.warn(TAG, `delData preferences is undefined`); 79 } 80 try { 81 await this.preferences.delete(key); 82 Log.debug(TAG, "Delete the value successfully."); 83 } catch (err) { 84 Log.error(TAG, `Delete the value failed with err: ${err}`); 85 } 86 } 87 88 public async flush() { 89 Log.debug(TAG, 'flush start!'); 90 if (this.preferences == undefined) { 91 Log.warn(TAG, `flush preferences is undefined`); 92 } 93 await this.preferences.flush(); 94 } 95 96 public async hasData(key: string) { 97 Log.debug(TAG, `hasData start! preferences ${this.preferences}`); 98 let ret = false; 99 if (this.preferences == undefined) { 100 Log.warn(TAG, `hasData preferences is undefined`); 101 return ret; 102 } 103 try { 104 ret = await this.preferences.has(key); 105 Log.debug(TAG, "hasData the value successfully."); 106 } catch (err) { 107 ret = false; 108 Log.error(TAG, `hasData the value failed with err: ${err}`); 109 } 110 return ret; 111 } 112} 113 114let dataStore = createOrGet(DataStoreUtil, TAG); 115 116export default dataStore as DataStoreUtil; 117 118