111 lines
4.0 KiB
Kotlin
111 lines
4.0 KiB
Kotlin
package com.sw.inbound.utils
|
|
|
|
import android.content.Context
|
|
import androidx.core.content.edit
|
|
import com.sw.plate.App
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.MutableStateFlow
|
|
import kotlinx.coroutines.flow.map
|
|
import kotlin.properties.ReadWriteProperty
|
|
import kotlin.reflect.KProperty
|
|
|
|
class SPUtil private constructor(context: Context, private val spName: String) {
|
|
|
|
companion object {
|
|
@Volatile
|
|
private var instance: SPUtil? = null
|
|
|
|
fun getInstance(
|
|
context: Context = App.getContext(),
|
|
spName: String = "default_sp"
|
|
): SPUtil {
|
|
return instance ?: synchronized(this) {
|
|
instance ?: SPUtil(context.applicationContext, spName).also { instance = it }
|
|
}
|
|
}
|
|
}
|
|
|
|
private val sharedPreferences by lazy {
|
|
context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
}
|
|
|
|
// 基础存储方法
|
|
fun put(key: String, value: Any?) {
|
|
when (value) {
|
|
null -> remove(key) // 存入null视为删除
|
|
is String -> sharedPreferences.edit { putString(key, value) }
|
|
is Int -> sharedPreferences.edit { putInt(key, value) }
|
|
is Long -> sharedPreferences.edit { putLong(key, value) }
|
|
is Float -> sharedPreferences.edit { putFloat(key, value) }
|
|
is Boolean -> sharedPreferences.edit { putBoolean(key, value) }
|
|
is Set<*> -> sharedPreferences.edit { putStringSet(key, value as Set<String>) }
|
|
else -> throw IllegalArgumentException("Unsupported type: ${value.javaClass.name}")
|
|
}
|
|
notifyDataChanged(key)
|
|
}
|
|
|
|
@Suppress("UNCHECKED_CAST")
|
|
fun <T> get(key: String, defaultValue: T? = null): T? {
|
|
return when (defaultValue) {
|
|
is String -> sharedPreferences.getString(key, defaultValue) as T
|
|
is Int -> sharedPreferences.getInt(key, defaultValue) as T
|
|
is Long -> sharedPreferences.getLong(key, defaultValue) as T
|
|
is Float -> sharedPreferences.getFloat(key, defaultValue) as T
|
|
is Boolean -> sharedPreferences.getBoolean(key, defaultValue) as T
|
|
is Set<*> -> sharedPreferences.getStringSet(key, defaultValue as Set<String>) as T
|
|
null -> when {
|
|
sharedPreferences.contains(key) -> get(key, "") as? T // 尝试作为String获取
|
|
else -> null
|
|
}
|
|
|
|
else -> throw IllegalArgumentException("Unsupported type: ${defaultValue.javaClass.name}")
|
|
}
|
|
}
|
|
|
|
fun remove(key: String) {
|
|
if (sharedPreferences.contains(key)) {
|
|
sharedPreferences.edit { remove(key) }
|
|
notifyDataChanged(key)
|
|
}
|
|
}
|
|
|
|
fun clear() {
|
|
sharedPreferences.edit { clear() }
|
|
notifyDataChanged(null)
|
|
}
|
|
|
|
fun contains(key: String): Boolean {
|
|
return sharedPreferences.contains(key)
|
|
}
|
|
|
|
// 监听变化
|
|
private val dataChangeFlow = MutableStateFlow(0)
|
|
|
|
private fun notifyDataChanged(key: String?) {
|
|
dataChangeFlow.value++
|
|
}
|
|
|
|
fun observeKey(key: String): Flow<Any?> {
|
|
return dataChangeFlow.map { get(key) }
|
|
}
|
|
|
|
// 属性委托支持
|
|
fun int(key: String, default: Int = 0) = SpProperty(key, default)
|
|
fun long(key: String, default: Long = 0L) = SpProperty(key, default)
|
|
fun float(key: String, default: Float = 0f) = SpProperty(key, default)
|
|
fun boolean(key: String, default: Boolean = false) = SpProperty(key, default)
|
|
fun string(key: String, default: String = "") = SpProperty(key, default)
|
|
fun stringSet(key: String, default: Set<String> = emptySet()) = SpProperty(key, default)
|
|
|
|
inner class SpProperty<T>(private val key: String, private val defaultValue: T) :
|
|
ReadWriteProperty<Any?, T> {
|
|
|
|
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
|
|
return get(key, defaultValue) ?: defaultValue
|
|
}
|
|
|
|
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
|
|
put(key, value)
|
|
}
|
|
}
|
|
} |