From 0c486e01cf28885c4e88853cf2f5c46403e383ea Mon Sep 17 00:00:00 2001 From: Carlos Rafael Giani Date: Sat, 18 Dec 2021 13:06:38 +0100 Subject: [PATCH] sharedPreferences: Add utility delegates Signed-off-by: Carlos Rafael Giani --- .../shared/sharedPreferences/Delegates.kt | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 shared/src/main/java/info/nightscout/shared/sharedPreferences/Delegates.kt diff --git a/shared/src/main/java/info/nightscout/shared/sharedPreferences/Delegates.kt b/shared/src/main/java/info/nightscout/shared/sharedPreferences/Delegates.kt new file mode 100644 index 0000000000..a7f80f7128 --- /dev/null +++ b/shared/src/main/java/info/nightscout/shared/sharedPreferences/Delegates.kt @@ -0,0 +1,83 @@ +package info.nightscout.shared.sharedPreferences + +import kotlin.properties.ReadWriteProperty +import kotlin.reflect.KProperty + +/* + +These classes allow to combine SP values with Kotlin delegates like this: + + private var myValue: String + by SPDelegateString(sp, "myValueKey", "default-value") + +Then, accessing myValue works by simply using the Kotlin setters & getters: + + val value = myValue // reading from sp + myValue = "newvalue" // writing to sp + +*/ + +class SPDelegateBoolean( + private val sp: SP, + private val key: String, + private val defaultValue: Boolean = false, + private val commit: Boolean = false +) : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>) = + sp.getBoolean(key, defaultValue) + + override fun setValue(thisRef: Any, property: KProperty<*>, value: Boolean) = + sp.edit(commit = commit) { putBoolean(key, value) } +} + +class SPDelegateDouble( + private val sp: SP, + private val key: String, + private val defaultValue: Double = 0.0, + private val commit: Boolean = false +) : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>) = + sp.getDouble(key, defaultValue) + + override fun setValue(thisRef: Any, property: KProperty<*>, value: Double) = + sp.edit(commit = commit) { putDouble(key, value) } +} + +class SPDelegateLong( + private val sp: SP, + private val key: String, + private val defaultValue: Long = 0, + private val commit: Boolean = false +) : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>) = + sp.getLong(key, defaultValue) + + override fun setValue(thisRef: Any, property: KProperty<*>, value: Long) = + sp.edit(commit = commit) { putLong(key, value) } +} + +class SPDelegateInt( + private val sp: SP, + private val key: String, + private val defaultValue: Int = 0, + private val commit: Boolean = false +) : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>) = + sp.getInt(key, defaultValue) + + override fun setValue(thisRef: Any, property: KProperty<*>, value: Int) = + sp.edit(commit = commit) { putInt(key, value) } +} + +class SPDelegateString( + private val sp: SP, + private val key: String, + private val defaultValue: String = "", + private val commit: Boolean = false +) : ReadWriteProperty { + override fun getValue(thisRef: Any, property: KProperty<*>) = + sp.getString(key, defaultValue) + + override fun setValue(thisRef: Any, property: KProperty<*>, value: String) = + sp.edit(commit = commit) { putString(key, value) } +}