From 0c469b3572ebf1d7fcb7f5e63ffed3dd5a48e64b Mon Sep 17 00:00:00 2001 From: Fabian Norlin Date: Mon, 8 Sep 2025 18:51:31 +0200 Subject: [PATCH] feat(shared): add KvPrefs expect/actual with change listeners (Android SharedPreferences) --- .../kotlin/app/klottr/prefs/KvPrefs.kt | 79 +++++++++++++++++++ .../kotlin/app/klottr/prefs/KvPrefs.kt | 25 ++++++ 2 files changed, 104 insertions(+) create mode 100644 shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt create mode 100644 shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt diff --git a/shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt b/shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt new file mode 100644 index 0000000..064ff63 --- /dev/null +++ b/shared/src/androidMain/kotlin/app/klottr/prefs/KvPrefs.kt @@ -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 = + p().getStringSet(key, emptySet())?.toSet() ?: emptySet() + + actual fun putStringSet(key: String, value: Set) { + 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) + } +} diff --git a/shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt b/shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt new file mode 100644 index 0000000..e8489c8 --- /dev/null +++ b/shared/src/commonMain/kotlin/app/klottr/prefs/KvPrefs.kt @@ -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 + fun putStringSet(key: String, value: Set) + + 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) +}