• 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 */
15
16import Logger from '../model/Logger'
17import common from '@ohos.app.ability.common'
18import preferences from '@ohos.data.preferences'
19
20
21const TAG: string = '[AccountData]'
22
23export class AccountData {
24  static instance: AccountData = null
25  private storage: preferences.Preferences = null
26
27  public static getInstance() {
28    if (this.instance === null) {
29      this.instance = new AccountData()
30    }
31    return this.instance
32  }
33
34  async getFromStorage(context: common.Context, url: string) {
35    let name = url
36    Logger.info(TAG, `Name is ${name}`)
37    try {
38      this.storage = await preferences.getPreferences(context, `${name}`)
39    } catch (err) {
40      Logger.error(`getStorage failed, code is ${err.code}, message is ${err.message}`)
41    }
42    if (this.storage === null) {
43      Logger.info(TAG, `Create stroage is fail.`)
44    }
45  }
46
47  async getStorage(context: common.Context, url: string) {
48    this.storage = null
49    await this.getFromStorage(context, url)
50    return this.storage
51  }
52
53  async putStorageValue(context: common.Context, key: string, value: string, url: string) {
54    this.storage = await this.getStorage(context, url)
55    try {
56      await this.storage.put(key, value)
57      await this.storage.flush()
58      Logger.info(TAG, `put key && value success`)
59    } catch (err) {
60      Logger.info(TAG, `aaaaaa put failed`)
61    }
62    return
63  }
64
65  async hasStorageValue(context: common.Context, key: string, url: string) {
66    this.storage = await this.getStorage(context, url)
67    let result
68    try {
69      result = await this.storage.has(key)
70    } catch (err) {
71      Logger.error(`hasStorageValue failed, code is ${err.code}, message is ${err.message}`)
72    }
73    Logger.info(TAG, `hasStorageValue success result is ${result}`)
74    return result
75  }
76
77  async getStorageValue(context: common.Context, key: string, url: string) {
78    this.storage = await this.getStorage(context, url)
79    let getValue
80    try {
81      getValue = await this.storage.get(key, 'null')
82    } catch (err) {
83      Logger.error(`getStorageValue failed, code is ${err.code}, message is ${err.message}`)
84    }
85    Logger.info(TAG, `getStorageValue success`)
86    return getValue
87  }
88
89  async deleteStorageValue(context: common.Context, key: string, url: string) {
90    this.storage = await this.getStorage(context, url)
91    try {
92      await this.storage.delete(key)
93      await this.storage.flush()
94    } catch (err) {
95      Logger.error(`deleteStorageValue failed, code is ${err.code}, message is ${err.message}`)
96    }
97    Logger.info(TAG, `delete success`)
98    return
99  }
100}