|
@@ -0,0 +1,255 @@
|
|
|
+package com.kdanmobile.android.common.utils.string
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import com.google.gson.ExclusionStrategy
|
|
|
+import com.google.gson.FieldAttributes
|
|
|
+import com.google.gson.Gson
|
|
|
+import com.google.gson.GsonBuilder
|
|
|
+import com.kdanmobile.android.common.extension.*
|
|
|
+import java.io.IOException
|
|
|
+import java.io.StreamCorruptedException
|
|
|
+import java.util.concurrent.locks.Lock
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author : hubowen
|
|
|
+ * @e-mail : hubowen@kdanmobile.com
|
|
|
+ * @date : 2020/10/19 1:53 PM
|
|
|
+ * @description:Sp数据保存相关
|
|
|
+ */
|
|
|
+class SharedPreferencesSave {
|
|
|
+
|
|
|
+ companion object {
|
|
|
+
|
|
|
+ const val DEFAULT_SP_NAME = "app.pref"
|
|
|
+
|
|
|
+ val instance by lazy {
|
|
|
+ SharedPreferencesSave()
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param ob
|
|
|
+ * @方法说明:保存对象
|
|
|
+ * @方法名称:savaObject
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveObject(context: Context, spName: String = DEFAULT_SP_NAME, key: String, ob: Any?) {
|
|
|
+ if (ob == null) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putString(key, ob.objSerializable()).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取存储的对象
|
|
|
+ * @方法名称:getObject
|
|
|
+ * @返回值:Object
|
|
|
+ */
|
|
|
+ fun <T> getObject(context: Context, spName: String = DEFAULT_SP_NAME, key: String): T? {
|
|
|
+
|
|
|
+ return try {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ val productBase64 = preferences.getString(key, "")
|
|
|
+ productBase64?.serializableToBean<T>()
|
|
|
+ } catch (e1: Exception) {
|
|
|
+ e1.printStackTrace()
|
|
|
+ null
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移除对应的key
|
|
|
+ */
|
|
|
+ fun removeKey(context: Context, spName: String = DEFAULT_SP_NAME, key: String) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().remove(key)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ * @方法说明:存储int数据
|
|
|
+ * @方法名称:savaIntValue
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveIntValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, value: Int) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putInt(key, value).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param defaultValue
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取int数据
|
|
|
+ * @方法名称:getIntValue
|
|
|
+ * @返回int
|
|
|
+ */
|
|
|
+ fun getIntValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, defaultValue: Int = 0): Int {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.getInt(key, defaultValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ * @方法说明:存储float数据
|
|
|
+ * @方法名称:savaFloatValue
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveFloatValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String?, value: Float) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putFloat(key, value).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取float数据
|
|
|
+ * @方法名称:getFloatValue
|
|
|
+ * @返回float
|
|
|
+ */
|
|
|
+ fun getFloatValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String?, defaultValue: Float = 0F): Float {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.getFloat(key, defaultValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ * @方法说明:存储boolean数据
|
|
|
+ * @方法名称:savaBooleanValue
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveBooleanValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, value: Boolean) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putBoolean(key, value).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param defaultValue 默认值
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取boolean数据
|
|
|
+ * @方法名称:getBooleanValue
|
|
|
+ * @返回boolean
|
|
|
+ */
|
|
|
+ fun getBooleanValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, defaultValue: Boolean = false): Boolean {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.getBoolean(key, defaultValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ * @方法说明:存储long数据
|
|
|
+ * @方法名称:savaLongValue
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveLongValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, value: Long) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putLong(key, value).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取long数据
|
|
|
+ * @方法名称:getLongValue
|
|
|
+ * @返回long
|
|
|
+ */
|
|
|
+ fun getLongValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, defaultValue : Long = 0L) : Long {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.getLong(key, defaultValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @param value
|
|
|
+ * @方法说明:存储String数据
|
|
|
+ * @方法名称:savaStringValue
|
|
|
+ * @返回void
|
|
|
+ */
|
|
|
+ fun saveStringValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String, value: String?) {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ preferences.edit().putString(key, value).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param context
|
|
|
+ * @param spName
|
|
|
+ * @param key
|
|
|
+ * @return
|
|
|
+ * @方法说明:获取String数据
|
|
|
+ * @方法名称:getStringValue
|
|
|
+ * @返回String
|
|
|
+ */
|
|
|
+ fun getStringValue(context: Context, spName: String = DEFAULT_SP_NAME, key: String?, defaultValue: String = ""): String {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.getString(key, defaultValue)?:defaultValue
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun saveMap(context: Context, spName: String = DEFAULT_SP_NAME, key: String, map: Map<*, *>?): Boolean {
|
|
|
+ if (map == null) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.edit().putString(key, map.toJson()).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ inline fun <reified K, reified V> getMap(context: Context, spName: String = DEFAULT_SP_NAME, key: String?): Map<K, V> {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ val mapStr = preferences.getString(key, "")
|
|
|
+ return mapStr?.jsonToMap<K,V>()?: emptyMap()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun <E> saveList(context: Context, spName: String = DEFAULT_SP_NAME, key: String, list: List<E>?): Boolean {
|
|
|
+ if (list.isNullOrEmpty()) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ return preferences.edit().putString(key, list.toJson()).commit()
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ inline fun <reified E> getList(context: Context, spName: String = DEFAULT_SP_NAME, key: String): List<E> {
|
|
|
+ val preferences = context.getSharedPreferences(spName, Context.MODE_PRIVATE)
|
|
|
+ val productBase64 = preferences.getString(key, "")
|
|
|
+ return productBase64?.jsonToList<E>()?: emptyList()
|
|
|
+ }
|
|
|
+}
|