1/**
2 * Copyright (c) 2021 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 */
15
16import ConfigData from './ConfigData';
17import LogUtil from './LogUtil';
18import { GlobalContext } from './GlobalContext';
19
20//import Storage from '@ohos.data.storage';
21
22//let preference = Storage.getStorageSync(ConfigData.PREFERENCES_PATH);
23this.storage = await dataStorage.getPreferences(
24  GlobalContext.getContext().getObject(GlobalContext.globalKeySettingsAbilityContext), ConfigData.PREFERENCES_PATH);
25
26export class PreferenceUtil {
27  /**
28   * Set up storage data
29   *
30   * @param key - key
31   * @param value - value
32   */
33  setStorageValue(key, value): void {
34    LogUtil.info(`Set preference, key: ${key}, value: ${value}")`);
35    preference.putSync(key, value);
36    preference.flushSync();
37  }
38
39  /**
40   * Get stored data
41   *
42   * @param key - key
43   * @param defaultValue - defaultValue
44   */
45  getStorageValue(key: string, defaultValue) {
46    let value = preference.getSync(key, defaultValue);
47    LogUtil.info(`Get storage value, key: ${key}, value: ${value}`);
48    return value;
49  }
50}
51
52let preferenceUtil = new PreferenceUtil();
53
54export default preferenceUtil as PreferenceUtil