From da6ea537adb2cb1abf7fe63187398615871670c7 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 14 Jun 2017 16:31:34 -0700 Subject: Add postgetter and setter --- library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 5 ++++- library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'library/src/main') 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> = 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 = KPrefDel class StringSet(set: Collection) : LinkedHashSet(set) -class KPrefDelegate internal constructor(private val key: String, private val fallback: T, private val pref: KPref, lock: Any? = null) : Lazy, java.io.Serializable { +class KPrefDelegate internal constructor(private val key: String, private val fallback: T, private val pref: KPref, lock: Any? = null, var postSetter: (value: T) -> Unit = {}) : Lazy, java.io.Serializable { @Volatile private var _value: Any = UNINITIALIZED private val lock = lock ?: this @@ -75,6 +75,7 @@ class KPrefDelegate internal constructor(private val key: String, priva else -> throw KPrefException(t) } editor.apply() + postSetter.invoke(t) } } -- cgit v1.2.3