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