基于mmkv的Constant封装

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)
mmkv 版本:"@tencent/mmkv": "1.3.5"

基于 mmkv 的 Constant 封装

全局变量,支持 KV 直接保存到手机物理存储,使用超方便。

关键代码 Constant.ets封装如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { MMKV } from "@tencent/mmkv";

export default class Constant {
private static TAG = "Constant";
/**
* time millis distance between server time and local time.
*
* offsetTime = ${local.getTime() - server.getTime()}
*/
public static offsetTime: number = 0;
/**
* session id
*
* record local from server response session id.
*/
private static SESSION_ID = "SESSION_ID";

public static get st(): string {
return MMKV.defaultMMKV().decodeString(Constant.SESSION_ID) ?? "";
}

public static set st(value: string) {
if (value != null)
MMKV.defaultMMKV().encodeString(Constant.SESSION_ID, value);
}
}
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×