diff options
author | Allan Wang <me@allanwang.ca> | 2020-02-23 13:40:29 -0800 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2020-02-23 13:40:29 -0800 |
commit | 0e7e43f7f778a206b0b30c6997c7c36494e6c142 (patch) | |
tree | 4e3f13ac9eafdfd02f4f0f78e0d7575f8bea0bf5 | |
parent | b35098bafd923d44bf751df2fce5f24cdad6f54b (diff) | |
download | kau-0e7e43f7f778a206b0b30c6997c7c36494e6c142.tar.gz kau-0e7e43f7f778a206b0b30c6997c7c36494e6c142.tar.bz2 kau-0e7e43f7f778a206b0b30c6997c7c36494e6c142.zip |
Add delete keys function
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 10 | ||||
-rw-r--r-- | 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<String, ILazyResettable<*>> = 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<String>) + fun KPref.deleteKeys(keys: Array<out String>) } 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<String>) { + override fun KPref.deleteKeys(keys: Array<out String>) { // 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<String>) { + override fun KPref.deleteKeys(keys: Array<out String>) { // Clear cached values keys.forEach { prefMap[it]?.invalidate() } } |