liukai пре 2 година
родитељ
комит
358fed411f

+ 2 - 1
app/src/main/java/com/hongyegroup/zhurijob/app/MyApplication.kt

@@ -7,8 +7,9 @@ import android.os.Process
 import android.webkit.WebView
 import com.guadou.cs_cptservices.YYConstants
 import com.guadou.lib_baselib.base.BaseApplication
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
 import com.guadou.lib_baselib.ext.checkEmpty
+import com.guadou.lib_baselib.engine.getString
 import dagger.hilt.android.HiltAndroidApp
 
 @HiltAndroidApp

+ 2 - 1
app/src/main/java/com/hongyegroup/zhurijob/ui/SplashActivity.kt

@@ -6,8 +6,9 @@ import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_cptservices.base.activity.YYBaseVDBActivity
 import com.guadou.lib_baselib.base.vm.EmptyViewModel
 import com.guadou.lib_baselib.bean.DataBindingConfig
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
 import com.guadou.lib_baselib.ext.checkEmpty
+import com.guadou.lib_baselib.engine.getString
 import com.guadou.lib_baselib.utils.BitmapUtils
 import com.guadou.lib_baselib.utils.CommUtils
 import com.guadou.lib_baselib.utils.log.YYLogUtils

+ 2 - 1
cpt_auth/src/main/java/com/hongyegroup/cpt_auth/mvvm/vm/UserLoginViewModel.kt

@@ -8,8 +8,9 @@ import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_cptservices.popup.FullWidthTextPopup
 import com.guadou.cs_router.YYRouterService
 import com.guadou.lib_baselib.base.vm.BaseViewModel
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
 import com.guadou.lib_baselib.ext.dp2px
+import com.guadou.lib_baselib.engine.putString
 import com.guadou.lib_baselib.utils.log.YYLogUtils
 import com.hongyegroup.cpt_auth.mvvm.repository.DemoRepository
 import com.hongyegroup.cpt_auth.mvvm.repository.SchoolRepository

+ 3 - 2
cs_baselib/build.gradle

@@ -47,8 +47,9 @@ dependencies {
     //Gson转换容错处理
     api deps.support.gson_factory
 
-    //SP 通过扩展方法SP()调用存取
-    api deps.support.fastkv
+    //SP(Key Value Cache)
+    api deps.support.data_store_core
+    api deps.support.data_store
     //权限管理
     implementation deps.support.permission
     //吐司

+ 2 - 0
cs_baselib/src/main/java/com/guadou/lib_baselib/base/BaseApplication.kt

@@ -19,6 +19,7 @@ open class BaseApplication : Application() {
 
     //全局的静态Gson对象
     companion object {
+        lateinit var instance : BaseApplication
         val mGson = Gson()
         lateinit var networkType: NetWorkUtil.NetworkType   //此变量会在网络监听中被动态赋值
 
@@ -33,6 +34,7 @@ open class BaseApplication : Application() {
 
     override fun onCreate() {
         super.onCreate()
+        instance = this
 
         //获取到全局的网络状态
         networkType = NetWorkUtil.getNetworkType(this@BaseApplication.applicationContext)

+ 0 - 2
cs_baselib/src/main/java/com/guadou/lib_baselib/cache/BaseLockCaches.java

@@ -92,8 +92,6 @@ public abstract class BaseLockCaches {
 
     /**
      * 获取缓存大小
-     *
-     * @return
      */
     public long size() {
         return getSize();

+ 79 - 0
cs_baselib/src/main/java/com/guadou/lib_baselib/engine/SPExt.kt

@@ -0,0 +1,79 @@
+package com.guadou.lib_baselib.engine
+
+import androidx.datastore.core.DataStore
+import androidx.datastore.preferences.core.Preferences
+import com.guadou.lib_baselib.utils.DataStoreUtils
+
+
+/**
+ * SharedPreferences实现引擎类
+ * 内部使用DataStore工具类替换实现
+ *
+ * sp().getString("a", "default")
+ * sp().getBoolean("b", false)
+ *
+   sp().putString("abc","test demo")
+
+ * sp().clearAll()
+}
+ */
+
+fun Any.SP(): DataStore<Preferences> {
+    return DataStoreUtils.dataStore
+}
+
+fun DataStore<Preferences>.putString(key: String, value: String) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getString(key: String, default: String = ""): String {
+    return DataStoreUtils.getData(key, default)
+}
+
+fun DataStore<Preferences>.putBoolean(key: String, value: Boolean) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getBoolean(key: String, default: Boolean = false): Boolean {
+    return DataStoreUtils.getData(key, default)
+}
+
+fun DataStore<Preferences>.putInt(key: String, value: Int) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getInt(key: String): Int {
+    return DataStoreUtils.getData(key, 0)
+}
+
+fun DataStore<Preferences>.putFloat(key: String, value: Float) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getFloat(key: String): Float {
+    return DataStoreUtils.getData(key, 0f)
+}
+
+fun DataStore<Preferences>.putDouble(key: String, value: Double) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getDouble(key: String): Double {
+    return DataStoreUtils.getData(key, 0.0)
+}
+
+fun DataStore<Preferences>.putLong(key: String, value: Long) {
+    DataStoreUtils.putData(key, value)
+}
+
+fun DataStore<Preferences>.getLong(key: String): Long {
+    return DataStoreUtils.getData(key, 0L)
+}
+
+fun DataStore<Preferences>.clearAll() {
+    DataStoreUtils.clearData()
+}
+
+
+
+

+ 0 - 27
cs_baselib/src/main/java/com/guadou/lib_baselib/ext/SharedPrefExt.kt

@@ -1,27 +0,0 @@
-package com.guadou.lib_baselib.ext
-
-import io.fastkv.FastKV
-
-
-/**
- * SharedPreferences相关
- *
- * sp().getString("a", "default")
- * sp().getBoolean("b", false)
- *
-sp().putString("abc","test demo")
-
-sp().edit {
-putString("a", "1")
-putBoolean("b", true)
-}
-
- * sp().clear()
-}
- */
-
-fun Any.SP(): FastKV {
-    return FastKV.Builder(commContext().filesDir.absolutePath + "/fastkv", "app_config").build()
-}
-
-

+ 158 - 0
cs_baselib/src/main/java/com/guadou/lib_baselib/utils/DataStoreUtils.kt

@@ -0,0 +1,158 @@
+package com.guadou.lib_baselib.utils
+
+import androidx.datastore.core.DataStore
+import androidx.datastore.preferences.core.*
+import androidx.datastore.preferences.preferencesDataStore
+import com.guadou.lib_baselib.base.BaseApplication
+import kotlinx.coroutines.flow.first
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.runBlocking
+
+/**
+ * DataStore的存取工具类
+ */
+object DataStoreUtils {
+
+    private val BaseApplication.dataStore: DataStore<Preferences> by preferencesDataStore(name = "app_config")
+
+    val dataStore: DataStore<Preferences> = BaseApplication.instance.dataStore
+
+    /**
+     * 存数据
+     */
+    fun <T> putData(key: String, value: T) {
+
+        runBlocking {
+            when (value) {
+                is Int -> putIntData(key, value)
+                is Long -> putLongData(key, value)
+                is String -> putStringData(key, value)
+                is Boolean -> putBooleanData(key, value)
+                is Float -> putFloatData(key, value)
+                is Double -> putDoubleData(key, value)
+                else -> throw IllegalArgumentException("This type cannot be saved to the Data Store")
+            }
+        }
+    }
+
+    /**
+     * 取数据
+     */
+    fun <T> getData(key: String, defaultValue: T): T {
+        val data = when (defaultValue) {
+            is Int -> getIntData(key, defaultValue)
+            is Long -> getLongData(key, defaultValue)
+            is String -> getStringData(key, defaultValue)
+            is Boolean -> getBooleanData(key, defaultValue)
+            is Float -> getFloatData(key, defaultValue)
+            is Double -> getDoubleData(key, defaultValue)
+            else -> throw IllegalArgumentException("This type cannot be saved to the Data Store")
+        }
+        return data as T
+    }
+
+    /**
+     * 清空数据
+     */
+    fun clearData() = runBlocking { dataStore.edit {
+        it.clear()
+    } }
+
+    /**
+     * 存放Int数据
+     */
+    private suspend fun putIntData(key: String, value: Int) = dataStore.edit {
+        it[intPreferencesKey(key)] = value
+    }
+
+    /**
+     * 存放Long数据
+     */
+    private suspend fun putLongData(key: String, value: Long) = dataStore.edit {
+        it[longPreferencesKey(key)] = value
+    }
+
+    /**
+     * 存放String数据
+     */
+    private suspend fun putStringData(key: String, value: String) = dataStore.edit {
+        it[stringPreferencesKey(key)] = value
+    }
+
+    /**
+     * 存放Boolean数据
+     */
+    private suspend fun putBooleanData(key: String, value: Boolean) = dataStore.edit {
+        it[booleanPreferencesKey(key)] = value
+    }
+
+    /**
+     * 存放Float数据
+     */
+    private suspend fun putFloatData(key: String, value: Float) = dataStore.edit {
+        it[floatPreferencesKey(key)] = value
+    }
+
+    /**
+     * 存放Double数据
+     */
+    private suspend fun putDoubleData(key: String, value: Double) = dataStore.edit {
+        it[doublePreferencesKey(key)] = value
+    }
+
+    /**
+     * 取出Int数据
+     */
+    private fun getIntData(key: String, default: Int = 0): Int = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[intPreferencesKey(key)] ?: default
+        }.first()
+    }
+
+    /**
+     * 取出Long数据
+     */
+    private fun getLongData(key: String, default: Long = 0): Long = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[longPreferencesKey(key)] ?: default
+        }.first()
+    }
+
+    /**
+     * 取出String数据
+     */
+    private fun getStringData(key: String, default: String? = null): String = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[stringPreferencesKey(key)] ?: default
+        }.first()!!
+    }
+
+    /**
+     * 取出Boolean数据
+     */
+    private fun getBooleanData(key: String, default: Boolean = false): Boolean = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[booleanPreferencesKey(key)] ?: default
+        }.first()
+    }
+
+    /**
+     * 取出Float数据
+     */
+    private fun getFloatData(key: String, default: Float = 0.0f): Float = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[floatPreferencesKey(key)] ?: default
+        }.first()
+    }
+
+    /**
+     * 取出Double数据
+     */
+    private fun getDoubleData(key: String, default: Double = 0.00): Double = runBlocking {
+        return@runBlocking dataStore.data.map {
+            it[doublePreferencesKey(key)] ?: default
+        }.first()
+    }
+
+
+}

+ 2 - 1
cs_cptServices/src/main/java/com/guadou/cs_cptservices/router/ServiceComponentServiceImpl.kt

@@ -6,8 +6,9 @@ import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_router.ARouterPath
 import com.guadou.cs_router.service.IServiceComponentServer
 import com.guadou.lib_baselib.base.BaseApplication
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
 import com.guadou.lib_baselib.ext.checkEmpty
+import com.guadou.lib_baselib.engine.getString
 
 @Route(path = ARouterPath.PATH_SERVICE_SERVER, name = "Service模块路由服务")
 class ServiceComponentServiceImpl : IServiceComponentServer {

+ 2 - 1
dependent.gradle

@@ -42,7 +42,8 @@ ext {
                     'okio'              : 'com.squareup.okio:okio:3.0.0',
                     'gson_factory'      : 'com.github.getActivity:GsonFactory:5.2',  //Gson转换容错处理
 
-                    "fastkv"            : 'io.github.billywei01:fastkv:1.1.2',
+                    "data_store"        : 'androidx.datastore:datastore-preferences:1.0.0',
+                    "data_store_core"   : 'androidx.datastore:datastore-preferences-core:1.0.0',
                     "permission"        : 'com.github.getActivity:XXPermissions:13.2',
                     "live_bus"          : 'io.github.jeremyliao:live-event-bus-x:1.8.0',
                     "brvah"             : 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.6',

+ 2 - 1
standalone/authrunalone/src/main/java/com/guadou/authrunalone/RunAloneViewModel.kt

@@ -5,7 +5,8 @@ import androidx.lifecycle.SavedStateHandle
 import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_router.YYRouterService
 import com.guadou.lib_baselib.base.vm.BaseViewModel
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
+import com.guadou.lib_baselib.engine.putString
 import dagger.hilt.android.lifecycle.HiltViewModel
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.delay

+ 2 - 1
standalone/ewalletrunalone/src/main/java/com/guadou/ewalletrunalone/RunAloneViewModel.kt

@@ -5,7 +5,8 @@ import androidx.lifecycle.SavedStateHandle
 import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_router.YYRouterService
 import com.guadou.lib_baselib.base.vm.BaseViewModel
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
+import com.guadou.lib_baselib.engine.putString
 import dagger.hilt.android.lifecycle.HiltViewModel
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.delay

+ 2 - 1
standalone/parttimerunalone/src/main/java/com/guadou/parttimerunalone/RunAloneViewModel.kt

@@ -5,7 +5,8 @@ import androidx.lifecycle.SavedStateHandle
 import com.guadou.cs_cptservices.YYConstants
 import com.guadou.cs_router.YYRouterService
 import com.guadou.lib_baselib.base.vm.BaseViewModel
-import com.guadou.lib_baselib.ext.SP
+import com.guadou.lib_baselib.engine.SP
+import com.guadou.lib_baselib.engine.putString
 import dagger.hilt.android.lifecycle.HiltViewModel
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.delay