From 1967bc180018b7c8f056617f9c23e7fceda267d1 Mon Sep 17 00:00:00 2001 From: Quyunshuo Date: Wed, 2 Jun 2021 15:30:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=96=B0SpUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/utils/SpUtils.kt | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/utils/SpUtils.kt b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/utils/SpUtils.kt index 76fdb35..8ff1c02 100644 --- a/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/utils/SpUtils.kt +++ b/lib_base/src/main/java/com/quyunshuo/androidbaseframemvvm/base/utils/SpUtils.kt @@ -4,10 +4,10 @@ import android.content.Context import com.tencent.mmkv.MMKV /** - * @Author: QuYunShuo - * @Time: 2020/8/28 - * @Class: SpUtils - * @Remark: MMKV使用封装 + * MMKV使用封装 + * + * @author Qu Yunshuo + * @since 8/28/20 */ object SpUtils { @@ -21,39 +21,43 @@ object SpUtils { * 根据value类型自动匹配需要执行的方法 */ fun put(key: String, value: Any) = - when (value) { - is Int -> putInt(key, value) - is Long -> putLong(key, value) - is Float -> putFloat(key, value) - is Double -> putDouble(key, value) - is String -> putString(key, value) - is Boolean -> putBoolean(key, value) - else -> false - } + when (value) { + is Int -> putInt(key, value) + is Long -> putLong(key, value) + is Float -> putFloat(key, value) + is Double -> putDouble(key, value) + is String -> putString(key, value) + is Boolean -> putBoolean(key, value) + else -> false + } - fun putString(key: String, value: String): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putString(key: String, value: String): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getString(key: String, defValue: String): String = MMKV.defaultMMKV().decodeString(key, defValue) + fun getString(key: String, defValue: String): String? = + MMKV.defaultMMKV()?.decodeString(key, defValue) - fun putInt(key: String, value: Int): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putInt(key: String, value: Int): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getInt(key: String, defValue: Int): Int = MMKV.defaultMMKV().decodeInt(key, defValue) + fun getInt(key: String, defValue: Int): Int? = MMKV.defaultMMKV()?.decodeInt(key, defValue) - fun putLong(key: String, value: Long): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putLong(key: String, value: Long): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getLong(key: String, defValue: Long): Long = MMKV.defaultMMKV().decodeLong(key, defValue) + fun getLong(key: String, defValue: Long): Long? = MMKV.defaultMMKV()?.decodeLong(key, defValue) - fun putDouble(key: String, value: Double): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putDouble(key: String, value: Double): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getDouble(key: String, defValue: Double): Double = MMKV.defaultMMKV().decodeDouble(key, defValue) + fun getDouble(key: String, defValue: Double): Double? = + MMKV.defaultMMKV()?.decodeDouble(key, defValue) - fun putFloat(key: String, value: Float): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putFloat(key: String, value: Float): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getFloat(key: String, defValue: Float): Float = MMKV.defaultMMKV().decodeFloat(key, defValue) + fun getFloat(key: String, defValue: Float): Float? = + MMKV.defaultMMKV()?.decodeFloat(key, defValue) - fun putBoolean(key: String, value: Boolean): Boolean = MMKV.defaultMMKV().encode(key, value) + fun putBoolean(key: String, value: Boolean): Boolean? = MMKV.defaultMMKV()?.encode(key, value) - fun getBoolean(key: String, defValue: Boolean): Boolean = MMKV.defaultMMKV().decodeBool(key, defValue) + fun getBoolean(key: String, defValue: Boolean): Boolean? = + MMKV.defaultMMKV()?.decodeBool(key, defValue) - fun contains(key: String): Boolean = MMKV.defaultMMKV().contains(key) + fun contains(key: String): Boolean? = MMKV.defaultMMKV()?.contains(key) } \ No newline at end of file