feat(shared): add KvPrefs expect/actual with change listeners (Android SharedPreferences)
This commit is contained in:
79
shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt
Normal file
79
shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt
Normal file
@@ -0,0 +1,79 @@
|
||||
package app.klottr.prefs
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
|
||||
actual object KvPrefs {
|
||||
private const val PREFS = "klottr_prefs"
|
||||
private var prefs: SharedPreferences? = null
|
||||
|
||||
private val listeners = CopyOnWriteArraySet<(String?) -> Unit>()
|
||||
|
||||
actual fun init(platformArg: Any?) {
|
||||
val ctx = platformArg as? Context
|
||||
?: throw IllegalStateException("KvPrefs.init requires Android Context")
|
||||
if (prefs == null) {
|
||||
prefs = ctx.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
||||
}
|
||||
}
|
||||
|
||||
private fun p(): SharedPreferences =
|
||||
prefs ?: throw IllegalStateException("KvPrefs not initialized. Call KvPrefs.init(context).")
|
||||
|
||||
actual fun getString(key: String, def: String?): String? =
|
||||
p().getString(key, def)
|
||||
|
||||
actual fun putString(key: String, value: String?) {
|
||||
p().edit().apply {
|
||||
if (value == null) remove(key) else putString(key, value)
|
||||
}.apply()
|
||||
notifyChanged(key)
|
||||
}
|
||||
|
||||
actual fun getInt(key: String, def: Int): Int =
|
||||
p().getInt(key, def)
|
||||
|
||||
actual fun putInt(key: String, value: Int) {
|
||||
p().edit().putInt(key, value).apply()
|
||||
notifyChanged(key)
|
||||
}
|
||||
|
||||
actual fun getBoolean(key: String, def: Boolean): Boolean =
|
||||
p().getBoolean(key, def)
|
||||
|
||||
actual fun putBoolean(key: String, value: Boolean) {
|
||||
p().edit().putBoolean(key, value).apply()
|
||||
notifyChanged(key)
|
||||
}
|
||||
|
||||
actual fun getStringSet(key: String): Set<String> =
|
||||
p().getStringSet(key, emptySet())?.toSet() ?: emptySet()
|
||||
|
||||
actual fun putStringSet(key: String, value: Set<String>) {
|
||||
p().edit().putStringSet(key, value.toMutableSet()).apply()
|
||||
notifyChanged(key)
|
||||
}
|
||||
|
||||
actual fun remove(key: String) {
|
||||
p().edit().remove(key).apply()
|
||||
notifyChanged(key)
|
||||
}
|
||||
|
||||
actual fun clearAll() {
|
||||
p().edit().clear().apply()
|
||||
notifyChanged(null)
|
||||
}
|
||||
|
||||
private fun notifyChanged(key: String?) {
|
||||
for (l in listeners) l.invoke(key)
|
||||
}
|
||||
|
||||
actual fun addListener(listener: (key: String?) -> Unit) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
actual fun removeListener(listener: (key: String?) -> Unit) {
|
||||
listeners.remove(listener)
|
||||
}
|
||||
}
|
||||
25
shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt
Normal file
25
shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package app.klottr.prefs
|
||||
|
||||
expect object KvPrefs {
|
||||
/** Körs tidigt på varje plattform (Android: applicationContext). */
|
||||
fun init(platformArg: Any? = null)
|
||||
|
||||
fun getString(key: String, def: String? = null): String?
|
||||
fun putString(key: String, value: String?)
|
||||
|
||||
fun getInt(key: String, def: Int = 0): Int
|
||||
fun putInt(key: String, value: Int)
|
||||
|
||||
fun getBoolean(key: String, def: Boolean = false): Boolean
|
||||
fun putBoolean(key: String, value: Boolean)
|
||||
|
||||
fun getStringSet(key: String): Set<String>
|
||||
fun putStringSet(key: String, value: Set<String>)
|
||||
|
||||
fun remove(key: String)
|
||||
fun clearAll()
|
||||
|
||||
/** Enkelt changes-API som funkar i common kod. */
|
||||
fun addListener(listener: (key: String?) -> Unit)
|
||||
fun removeListener(listener: (key: String?) -> Unit)
|
||||
}
|
||||
Reference in New Issue
Block a user