aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/kotlin/ca/allanwang
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main/kotlin/ca/allanwang')
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt5
-rw-r--r--library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt3
2 files changed, 6 insertions, 2 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
index add79b9..7fd8955 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
@@ -21,7 +21,8 @@ open class KPref {
internal val sp: SharedPreferences by lazy {
if (!initialized) throw KPrefException("KPref object has not yet been initialized; please initialize it with a context and preference name")
- c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE) }
+ c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
+ }
internal val prefMap: MutableMap<String, KPrefDelegate<*>> = mutableMapOf()
@@ -29,4 +30,6 @@ open class KPref {
prefMap.values.forEach { it.invalidate() }
}
+ operator fun get(key: String): KPrefDelegate<*>? = prefMap[key]
+
} \ No newline at end of file
diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
index 5c6caa9..fe67ecc 100644
--- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
+++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
@@ -15,7 +15,7 @@ fun KPref.kpref(key: String, fallback: String): KPrefDelegate<String> = KPrefDel
class StringSet(set: Collection<String>) : LinkedHashSet<String>(set)
-class KPrefDelegate<T : Any> internal constructor(private val key: String, private val fallback: T, private val pref: KPref, lock: Any? = null) : Lazy<T>, java.io.Serializable {
+class KPrefDelegate<T : Any> internal constructor(private val key: String, private val fallback: T, private val pref: KPref, lock: Any? = null, var postSetter: (value: T) -> Unit = {}) : Lazy<T>, java.io.Serializable {
@Volatile private var _value: Any = UNINITIALIZED
private val lock = lock ?: this
@@ -75,6 +75,7 @@ class KPrefDelegate<T : Any> internal constructor(private val key: String, priva
else -> throw KPrefException(t)
}
editor.apply()
+ postSetter.invoke(t)
}
}