1/*
2 * Copyright (c) 2021-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 { Logger } from '../util/HiLogger'
16
17const logger: Logger = new Logger("AppStorageHelper")
18
19/**
20 * [系统全局storage管理类,支持界面绑定]
21 */
22export class AppStorageHelper {
23  active: boolean = true
24  unhandledMap: Map<string, any> = new Map()
25  disable: () => void = () => {
26    this.active = false
27  }
28  enable: () => void = () => {
29    this.active = true
30    if (!this.unhandledMap.size) {
31      return
32    }
33    this.unhandledMap.forEach((value, key) => {
34      setOrCreateAppStorage(key, value)
35    })
36    this.unhandledMap.clear()
37  }
38}
39
40// 定义了AppStorage下支持挂载的对象
41export const appStorageKeys = {
42  // 屏幕宽度 px
43  screenWidth: 'screenWidth',
44  // 屏幕高度 px
45  screenHeight: 'screenHeight',
46  // 窗口宽度 px
47  windowWidth: 'windowWidth',
48  // 窗口高度 px
49  windowHeight: 'windowHeight',
50  // 顶部状态栏高度 px
51  statusBarHeight: 'statusBarHeight',
52  // 底部导航栏高度 px
53  navigatorBarHeight: 'navigatorBarHeight',
54  // 样式单位管理对象
55  styleConfig: 'styleConfig',
56  // 主题管理对象
57  themeConfig: 'themeConfig',
58  // 窗口密度模式
59  densityType: 'densityType',
60  // 设置
61  setting: 'setting',
62  // 是否可以访问网络
63  isInternetConnected: 'isInternetConnected',
64  // 当前系统语言
65  lang: "lang",
66  // 是否是深色主题
67  isDarkTheme: "isDarkTheme",
68  // 横竖屏切换宽高对象
69  windowArea: "windowArea",
70  // 全屏模式比例
71  appSplitRatio: "appSplitRatio",
72  serverEnvironment: "serverEnvironment",
73  // 铃声播放状态
74  ringTonePlayStatus: "ringTonePlayStatus"
75}
76
77/**
78 * 创建或者重新设置appstorage
79 *
80 * @param storageKey key
81 * @param object 塞入的对象
82 */
83export function setOrCreateAppStorage<T>(storageKey: string, object: T): void {
84  let globalStore = globalThis.appStorageHelper as AppStorageHelper
85  if (globalStore && !globalStore.active) {
86    globalStore.unhandledMap.set(storageKey, object)
87    return
88  }
89  if (!appStorageKeys[storageKey]) {
90    logger.error(`Cannot Create storage key of ${storageKey}`)
91    return
92  }
93  AppStorage.SetOrCreate<T>(storageKey, object)
94}
95
96/**
97 * 获取设置过的appstorage
98 *
99 * @param storageKey key
100 * @return storageValue
101 */
102export function getAppStorage<T>(storageKey: string): T {
103  logger.info('getAppStorage' + storageKey)
104  if (!appStorageKeys[storageKey]) {
105    logger.error(`Cannot getAppStorage storage key of ${storageKey}`)
106    return undefined
107  }
108  return AppStorage.Get<T>(storageKey)
109}