aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt47
1 files changed, 21 insertions, 26 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 fc7a76a..7f75370 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
@@ -15,10 +15,7 @@
*/
package ca.allanwang.kau.kpref
-import android.content.Context
-import android.content.SharedPreferences
import ca.allanwang.kau.kotlin.ILazyResettable
-import ca.allanwang.kau.logging.KL
/**
* Created by Allan Wang on 2017-06-07.
@@ -32,38 +29,36 @@ import ca.allanwang.kau.logging.KL
* Furthermore, all kprefs are held in the [prefMap],
* so if you wish to reset a preference, you must also invalidate the kpref
* from that map
- *
- * You may optionally override [deleteKeys]. This will be called on initialization
- * And delete all keys returned from that method
*/
-open class KPref(builder: KPrefBuilder = KPrefBuilderAndroid) : KPrefBuilder by builder {
-
- lateinit var PREFERENCE_NAME: String
- lateinit var sp: SharedPreferences
+open class KPref private constructor(
+ val preferenceName: String,
+ val builder: KPrefBuilder
+) : KPrefBuilder by builder {
- fun initialize(
- c: Context,
- preferenceName: String,
- sharedPrefs: SharedPreferences = c.applicationContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
- ) {
- PREFERENCE_NAME = preferenceName
- sp = sharedPrefs
- KL.d { "Shared Preference $preferenceName has been initialized" }
- val toDelete = deleteKeys()
- if (toDelete.isNotEmpty()) {
- val edit = sp.edit()
- toDelete.forEach { edit.remove(it) }
- edit.apply()
- }
- }
+ constructor(preferenceName: String, factory: KPrefFactory) : this(
+ preferenceName,
+ factory.createBuilder(preferenceName)
+ )
internal val prefMap: MutableMap<String, ILazyResettable<*>> = mutableMapOf()
+ fun add(entry: KPrefDelegate<*>) {
+ if (prefMap.containsKey(entry.key))
+ throw KPrefException("${entry.key} is already used elsewhere in preference $preferenceName")
+ prefMap[entry.key] = entry
+ }
+
fun reset() {
prefMap.values.forEach { it.invalidate() }
}
operator fun get(key: String): ILazyResettable<*>? = prefMap[key]
- open fun deleteKeys(): Array<String> = arrayOf()
+ /**
+ * Exposed key deletion function from builder.
+ * To avoid recursion, this type uses vararg
+ */
+ fun deleteKeys(vararg keys: String) {
+ deleteKeys(keys)
+ }
}