diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-14 16:31:34 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-14 16:31:34 -0700 |
commit | da6ea537adb2cb1abf7fe63187398615871670c7 (patch) | |
tree | 6e2ea50b5594dcf6f9d15987471cdee4fd855bd2 /library | |
parent | 90e61e0fef26b7134cdda1b97f1a358d7496c4ec (diff) | |
download | kau-da6ea537adb2cb1abf7fe63187398615871670c7.tar.gz kau-da6ea537adb2cb1abf7fe63187398615871670c7.tar.bz2 kau-da6ea537adb2cb1abf7fe63187398615871670c7.zip |
Add postgetter and setter
Diffstat (limited to 'library')
-rw-r--r-- | library/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt | 5 | ||||
-rw-r--r-- | library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 3 |
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) } } |