SpUtil 类实现
This commit is contained in:
parent
0cb65eeaed
commit
c829fce8a7
|
|
@ -0,0 +1,149 @@
|
||||||
|
package com.remax.visualnovel.utils
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import com.remax.visualnovel.configs.NovelApplication
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class SpUtil private constructor(private val mUserId: Int) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val Global = -999
|
||||||
|
private const val SP_NAME_PREFIX = "sp_data_"
|
||||||
|
private var mAppContext: Context = NovelApplication.appContext()
|
||||||
|
|
||||||
|
|
||||||
|
private val mInstanceMap = mutableMapOf<Int, SpUtil>()
|
||||||
|
|
||||||
|
|
||||||
|
fun init() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specified user sp
|
||||||
|
*/
|
||||||
|
fun getInstance(userId: Int = Global): SpUtil {
|
||||||
|
return mInstanceMap.getOrPut(userId) {
|
||||||
|
SpUtil(userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear specified user all sp info
|
||||||
|
*/
|
||||||
|
fun clearInstance(userId: Int? = Global) {
|
||||||
|
val key = userId ?: Global
|
||||||
|
val instance = mInstanceMap.remove(key)
|
||||||
|
instance?.deleteSpFile()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private val mGson = Gson()
|
||||||
|
private val mSharePre: SharedPreferences
|
||||||
|
init {
|
||||||
|
val spName = "${SP_NAME_PREFIX}$mUserId"
|
||||||
|
mSharePre = mAppContext.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- clear delete --------------------
|
||||||
|
private fun getUserSpFile() = "shared_prefs/${SP_NAME_PREFIX}$mUserId.xml"
|
||||||
|
|
||||||
|
private fun deleteSpFile(): Boolean {
|
||||||
|
return try {
|
||||||
|
val file = File(mAppContext.filesDir.parent, getUserSpFile())
|
||||||
|
if (file.exists()) {
|
||||||
|
clear()
|
||||||
|
file.delete()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- basic data type --------------------
|
||||||
|
fun putString(key: String, value: String) = withEditor { putString(key, value) }
|
||||||
|
fun getString(key: String, default: String = ""): String = mSharePre.getString(key, default) ?: default
|
||||||
|
fun putInt(key: String, value: Int) = withEditor { putInt(key, value) }
|
||||||
|
fun getInt(key: String, default: Int = 0): Int = mSharePre.getInt(key, default)
|
||||||
|
fun putLong(key: String, value: Long) = withEditor { putLong(key, value) }
|
||||||
|
fun getLong(key: String, default: Long = 0L): Long = mSharePre.getLong(key, default)
|
||||||
|
fun putFloat(key: String, value: Float) = withEditor { putFloat(key, value) }
|
||||||
|
fun getFloat(key: String, default: Float = 0f): Float = mSharePre.getFloat(key, default)
|
||||||
|
fun putBoolean(key: String, value: Boolean) = withEditor { putBoolean(key, value) }
|
||||||
|
fun getBoolean(key: String, default: Boolean = false): Boolean = mSharePre.getBoolean(key, default)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------- complicate object --------------------
|
||||||
|
fun <T> putObject(key: String, value: T?) {
|
||||||
|
if (value == null) {
|
||||||
|
remove(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val json = mGson.toJson(value)
|
||||||
|
putString(key, json)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T> getObject(key: String, default: T? = null): T? {
|
||||||
|
val json = getString(key)
|
||||||
|
if (json.isEmpty()) return default
|
||||||
|
|
||||||
|
return try {
|
||||||
|
Gson().fromJson(json, T::class.java)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun <T> putList(key: String, list: List<T>?) {
|
||||||
|
if (list == null) {
|
||||||
|
remove(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
val json = mGson.toJson(list)
|
||||||
|
putString(key, json)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline fun <reified T> getList(key: String, default: List<T> = emptyList()): List<T> {
|
||||||
|
val json = getString(key)
|
||||||
|
if (json.isEmpty()) return default
|
||||||
|
|
||||||
|
return try {
|
||||||
|
val type = object : TypeToken<List<T>>() {}.type
|
||||||
|
Gson().fromJson(json, type) ?: default
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
default
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fun remove(key: String) = withEditor { remove(key) }
|
||||||
|
fun clear() = withEditor { clear() }
|
||||||
|
fun contains(key: String): Boolean = mSharePre.contains(key)
|
||||||
|
|
||||||
|
private inline fun withEditor(action: SharedPreferences.Editor.() -> Unit) {
|
||||||
|
mSharePre.edit().apply {
|
||||||
|
action()
|
||||||
|
apply()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue