18034727885
2026-05-23 293183266e941692005262d76f0bcff05d6384d6
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.xw.common.utils
 
import android.annotation.SuppressLint
import android.content.Context
import com.blankj.utilcode.util.GsonUtils
import com.xw.common.utils.LogUtils
import com.tencent.mmkv.MMKV
 
class MMKVManager {
 
    companion object {
        val TAG = "MMKVManager"
 
        @SuppressLint("StaticFieldLeak")
        @Volatile
        private var instance: MMKVManager? = null
 
        fun getInstance(): MMKVManager {
            return instance ?: synchronized(this) {
                instance ?: MMKVManager().also {
                    instance = it
                }
            }
        }
    }
 
    val isInit: Boolean get() = MMKV.getRootDir() != null
 
    val defaultMMKV: MMKV? by lazy { MMKV.defaultMMKV() }
 
    /**
     * 初始化方法
     *
     * 必须先调用一次该方法,才能正常使用其他方法、属性
     */
    fun init(context: Context) {
        if (isInit.not()) MMKV.initialize(context)
    }
 
    fun saveString(key: KeyPreferences, value: String) {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        defaultMMKV?.encode(key.value, value)
    }
 
    fun getString(key: KeyPreferences, default: String = ""): String {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        return defaultMMKV?.decodeString(key.value) ?: default
    }
 
    fun saveBoolean(key: KeyPreferences, value: Boolean) {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        defaultMMKV?.encode(key.value, value)
    }
 
    fun getBoolean(key: KeyPreferences, default: Boolean = false): Boolean {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        return defaultMMKV?.decodeBool(key.value, default) ?: default
    }
 
    fun <T> saveModel(key: KeyPreferences, value: T?) {
        value?.let {
            if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
            defaultMMKV?.encode(key.value, GsonUtils.toJson(it))
        }
    }
 
    fun deleteModel(key: KeyPreferences) {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        defaultMMKV?.encode(key.value, "")
    }
 
    inline fun <reified T> getModel(key: KeyPreferences): T? {
        if (isInit.not()) MMKV.initialize(AppGlobals.getApplication())
        val value = defaultMMKV?.decodeString(key.value)
        return try {
            GsonUtils.fromJson(value, T::class.java)
        } catch (e: Exception) {
            LogUtils.e(TAG, "model e = ${e.message}")
            null
        }
    }
 
    fun clear() {
        defaultMMKV?.clearAll()
    }
}
 
enum class KeyPreferences(val value: String) {
    TOKEN("token"),
    USER("user"),
    THIRD("third"),
    ACCIDENT("accident"),
    NEED_READ("needRead"), // 是否需要阅读协议
    NEED_UPDATE("needUpdate") // 是否升级
}