Share preference util
This commit is contained in:
parent
9e12f98bb7
commit
086bb93212
|
|
@ -5,7 +5,7 @@
|
|||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
|
||||
<application
|
||||
android:name=".App"
|
||||
android:name=".VidiDinApp"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
|
|
|||
|
|
@ -5,4 +5,6 @@ import dagger.hilt.android.HiltAndroidApp
|
|||
|
||||
|
||||
@HiltAndroidApp
|
||||
class App : BaseApp()
|
||||
class VidiDinApp : BaseApp() {
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ import androidx.core.view.updatePadding
|
|||
import androidx.fragment.app.viewModels
|
||||
import com.ama.core.architecture.appBase.AppViewsFragment
|
||||
import com.ama.core.architecture.appBase.OnFragmentBackgroundListener
|
||||
import com.ama.core.architecture.util.SpUtil
|
||||
import com.ama.core.architecture.util.setStatusBarDarkFont
|
||||
import com.gamedog.vididin.main.interfaces.OnTabClickAgainListener
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
|
|
|
|||
|
|
@ -1,11 +1,191 @@
|
|||
package com.ama.core.architecture.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.util.Base64
|
||||
import com.ama.core.architecture.BaseApp
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import java.lang.reflect.Type
|
||||
|
||||
|
||||
|
||||
class SpUtil private constructor() {
|
||||
|
||||
|
||||
|
||||
class SpUtil private constructor(spFileName: String) {
|
||||
companion object {
|
||||
const val KEY_X1 = "11111"
|
||||
const val KEY_X2 = "22222"
|
||||
|
||||
private const val DEFAULT_SP_NAME = "enhanced_prefs"
|
||||
@Volatile
|
||||
private var instance: SpUtil? = null
|
||||
fun instance(prefName: String = DEFAULT_SP_NAME): SpUtil {
|
||||
return instance ?: synchronized(this) {
|
||||
instance ?: SpUtil(prefName).also {
|
||||
instance = it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
val gson: Gson = Gson()
|
||||
private val sharedPreferences: SharedPreferences
|
||||
|
||||
init {
|
||||
sharedPreferences = BaseApp.appContext().getSharedPreferences(spFileName, Context.MODE_PRIVATE)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ----------- 基本数据类型操作 -----------
|
||||
fun putString(key: String, value: String) {
|
||||
sharedPreferences.edit().putString(key, value).apply()
|
||||
}
|
||||
|
||||
fun getString(key: String, defaultValue: String = ""): String {
|
||||
return sharedPreferences.getString(key, defaultValue) ?: defaultValue
|
||||
}
|
||||
|
||||
fun putInt(key: String, value: Int) {
|
||||
sharedPreferences.edit().putInt(key, value).apply()
|
||||
}
|
||||
|
||||
fun getInt(key: String, defaultValue: Int = 0): Int {
|
||||
return sharedPreferences.getInt(key, defaultValue)
|
||||
}
|
||||
|
||||
fun putBoolean(key: String, value: Boolean) {
|
||||
sharedPreferences.edit().putBoolean(key, value).apply()
|
||||
}
|
||||
|
||||
fun getBoolean(key: String, defaultValue: Boolean = false): Boolean {
|
||||
return sharedPreferences.getBoolean(key, defaultValue)
|
||||
}
|
||||
|
||||
fun putLong(key: String, value: Long) {
|
||||
sharedPreferences.edit().putLong(key, value).apply()
|
||||
}
|
||||
|
||||
fun getLong(key: String, defaultValue: Long = 0L): Long {
|
||||
return sharedPreferences.getLong(key, defaultValue)
|
||||
}
|
||||
|
||||
fun putFloat(key: String, value: Float) {
|
||||
sharedPreferences.edit().putFloat(key, value).apply()
|
||||
}
|
||||
|
||||
fun getFloat(key: String, defaultValue: Float = 0f): Float {
|
||||
return sharedPreferences.getFloat(key, defaultValue)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ----------- 复杂对象 -----------
|
||||
fun <T> putObject(key: String, obj: T) {
|
||||
try {
|
||||
val jsonString = gson.toJson(obj)
|
||||
val encryptedData = Base64.encodeToString(jsonString.toByteArray(), Base64.DEFAULT)
|
||||
putString(key, encryptedData)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> getObject(key: String, defaultObj: T? = null): T? {
|
||||
return try {
|
||||
val encryptedData = getString(key)
|
||||
if (encryptedData.isBlank()) return defaultObj
|
||||
|
||||
val jsonString = String(Base64.decode(encryptedData, Base64.DEFAULT))
|
||||
gson.fromJson(jsonString, T::class.java) ?: defaultObj
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
defaultObj
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <T> putList(key: String, list: List<T>) {
|
||||
try {
|
||||
val jsonString = gson.toJson(list)
|
||||
val encryptedData = Base64.encodeToString(jsonString.toByteArray(), Base64.DEFAULT)
|
||||
putString(key, encryptedData)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T> getList(key: String): List<T> {
|
||||
return try {
|
||||
val encryptedData = getString(key)
|
||||
if (encryptedData.isBlank()) return emptyList()
|
||||
|
||||
val jsonString = String(Base64.decode(encryptedData, Base64.DEFAULT))
|
||||
val type: Type = object : TypeToken<List<T>>() {}.type
|
||||
gson.fromJson<List<T>>(jsonString, type) ?: emptyList()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun <K, V> putMap(key: String, map: Map<K, V>) {
|
||||
try {
|
||||
val jsonString = gson.toJson(map)
|
||||
val encryptedData = Base64.encodeToString(jsonString.toByteArray(), Base64.DEFAULT)
|
||||
putString(key, encryptedData)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline fun <reified K, reified V> getMap(key: String): Map<K, V> {
|
||||
return try {
|
||||
val encryptedData = getString(key)
|
||||
if (encryptedData.isBlank()) return emptyMap()
|
||||
|
||||
val jsonString = String(Base64.decode(encryptedData, Base64.DEFAULT))
|
||||
val type: Type = object : TypeToken<Map<K, V>>() {}.type
|
||||
gson.fromJson<Map<K, V>>(jsonString, type) ?: emptyMap()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
emptyMap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// ----------- 工具方法 -----------
|
||||
fun contains(key: String): Boolean {
|
||||
return sharedPreferences.contains(key)
|
||||
}
|
||||
|
||||
fun remove(key: String) {
|
||||
sharedPreferences.edit().remove(key).apply()
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
sharedPreferences.edit().clear().apply()
|
||||
}
|
||||
|
||||
fun getAllKeys(): Set<String> {
|
||||
return sharedPreferences.all.keys
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue