From 0e7e43f7f778a206b0b30c6997c7c36494e6c142 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 23 Feb 2020 13:40:29 -0800 Subject: Add delete keys function --- core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 10 +++++++++- core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt index a45d66d..7f75370 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt @@ -42,7 +42,7 @@ open class KPref private constructor( internal val prefMap: MutableMap> = mutableMapOf() - fun add(entry : KPrefDelegate<*>) { + fun add(entry: KPrefDelegate<*>) { if (prefMap.containsKey(entry.key)) throw KPrefException("${entry.key} is already used elsewhere in preference $preferenceName") prefMap[entry.key] = entry @@ -53,4 +53,12 @@ open class KPref private constructor( } operator fun get(key: String): ILazyResettable<*>? = prefMap[key] + + /** + * Exposed key deletion function from builder. + * To avoid recursion, this type uses vararg + */ + fun deleteKeys(vararg keys: String) { + deleteKeys(keys) + } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt index b8fb8ea..7a35ac2 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt @@ -71,7 +71,7 @@ interface KPrefBuilder { /** * Remove keys from pref so they revert to the default */ - fun KPref.deleteKeys(keys: Array) + fun KPref.deleteKeys(keys: Array) } class KPrefBuilderAndroid(val sp: SharedPreferences) : KPrefBuilder { @@ -143,7 +143,7 @@ class KPrefBuilderAndroid(val sp: SharedPreferences) : KPrefBuilder { override fun KPref.kprefSingle(key: String) = KPrefSingleDelegateAndroid(key, this, this@KPrefBuilderAndroid) - override fun KPref.deleteKeys(keys: Array) { + override fun KPref.deleteKeys(keys: Array) { // Remove pref listing sp.edit().apply { keys.forEach { remove(it) } @@ -179,7 +179,7 @@ object KPrefBuilderInMemory : KPrefBuilder { override fun KPref.kprefSingle(key: String) = KPrefSingleDelegateInMemory(key, this) - override fun KPref.deleteKeys(keys: Array) { + override fun KPref.deleteKeys(keys: Array) { // Clear cached values keys.forEach { prefMap[it]?.invalidate() } } -- cgit v1.2.3