kotlin : SharedPreferences 与 委托

it2022-05-09  34

方式一:

import android.content.Context import android.content.SharedPreferences import kotlin.properties.ReadWriteProperty import kotlin.reflect.KProperty class MyPreference1<T>(private val context: Context, val name: String, private val default: T) : ReadWriteProperty<Any?, T> { private val prefs: SharedPreferences by lazy { context.getSharedPreferences("default", Context.MODE_PRIVATE) } override fun getValue(thisRef: Any?, property: KProperty<*>): T { return getPreference(name, default) } override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { putPreference(name, value) } @Suppress("UNCHECKED_CAST") private fun <T> getPreference(name: String, default: T): T = with(prefs) { val res: Any = when (default) { is Long -> getLong(name, default) is String -> getString(name, default) is Int -> getInt(name, default) is Boolean -> getBoolean(name, default) is Float -> getFloat(name, default) else -> throw IllegalArgumentException("This type can not be saved into Preferences") } res as T } private fun <U> putPreference(name: String, value: U) = with(prefs.edit()) { when (value) { is Long -> putLong(name, value) is String -> putString(name, value) is Int -> putInt(name, value) is Boolean -> putBoolean(name, value) is Float -> putFloat(name, value) else -> throw IllegalArgumentException("This type can be saved into Preferences") }.apply() } }

方式二:

import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences import kotlin.reflect.KProperty class MyPreference2<T>(val name: String, private val default: T) { private val prefs: SharedPreferences by lazy { App.instance.applicationContext.getSharedPreferences(name, Context.MODE_PRIVATE) } operator fun getValue(thisRef: Any?, property: KProperty<*>): T { return getSharePreferences(name, default) } operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) { putSharePreferences(name, value) } @Suppress("UNCHECKED_CAST") private fun getSharePreferences(name: String, default: T): T = with(prefs) { val res: Any = when (default) { is Long -> getLong(name, default) is String -> getString(name, default) is Int -> getInt(name, default) is Boolean -> getBoolean(name, default) is Float -> getFloat(name, default) else -> throw IllegalArgumentException("This type of data cannot be saved!") } return res as T } @SuppressLint("CommitPrefEdits") private fun putSharePreferences(name: String, value: T) = with(prefs.edit()) { when (value) { is Long -> putLong(name, value) is String -> putString(name, value) is Int -> putInt(name, value) is Boolean -> putBoolean(name, value) is Float -> putFloat(name, value) else -> throw IllegalArgumentException("This type of data cannot be saved!") }.apply() } } class App : Application() { companion object { // 伴生对象 lateinit var instance: App private set } override fun onCreate() { super.onCreate() instance = this } }

测试

class MainActivity : AppCompatActivity() { var a by MyPreference1(this@MainActivity, "a", 1) var b by MyPreference2("b", 100) @SuppressLint("SetTextI18n") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) tv_result.text = "" + a } @SuppressLint("SetTextI18n") @Suppress("UNUSED_EXPRESSION") fun onclick(view: View) { when (view.id) { R.id.btn1 -> a++ R.id.btn2 -> tv_result.text = "" + a R.id.btn3 -> b++ R.id.btn4 -> tv_result.text = "" + b else -> "default" } } }

最新回复(0)