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