aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/kpref
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-07-31 23:02:01 -0700
committerGitHub <noreply@github.com>2017-07-31 23:02:01 -0700
commit48213d0b427c478865c75fee912ff1ae8bbaffb5 (patch)
tree7aef1d8400fc3403ee5a40aba945f33a95319359 /core/src/main/kotlin/ca/allanwang/kau/kpref
parent8a4e9fd44dfbcf58aa7ab63167dcbdf8752db7d0 (diff)
downloadkau-48213d0b427c478865c75fee912ff1ae8bbaffb5.tar.gz
kau-48213d0b427c478865c75fee912ff1ae8bbaffb5.tar.bz2
kau-48213d0b427c478865c75fee912ff1ae8bbaffb5.zip
Major update to core and kotterknife; create mediapicker (#15)
* Readme * Fix kau direction bits * Truly support transparent ripples * Update changelog * Test rect as base * Replace fab transition with generic fade scale transition * Add scalexy func * Add scaleXY * Add arguments to fadeScaleTransition * Clean up ink indicator * Create setOnSingleTapListener * Fix lint and add rndColor * Create kotterknife resettables * Add readme and missing object * Create lazy resettable registered * Update core docs * Opt for separate class for resettable registry * Clean up resettable registry * Rename functions * Add ripple callback listener * Adjust kprefactivity desc color * Add more transitions * Add delete keys option * Add instrumentation tests * switch id * Revert automatic instrumental tests * Generify imagepickercore and prepare video alternative * Create working video picker * Address possible null issue * Update searchview * Make layouts public * Add changelog test * Update logo link * Add custom color gif
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/kpref')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt21
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt3
2 files changed, 23 insertions, 1 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 fa6c5a9..c1ce282 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
@@ -6,6 +6,19 @@ import ca.allanwang.kau.kotlin.ILazyResettable
/**
* Created by Allan Wang on 2017-06-07.
+ *
+ * Base class for shared preferences
+ * All objects extending this class must be called in
+ * the app's [android.app.Application] class
+ *
+ * See the [KPref.kpref] extensions for more details
+ *
+ * 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 {
@@ -18,6 +31,12 @@ open class KPref {
initialized = true
this.c = c.applicationContext
PREFERENCE_NAME = preferenceName
+ val toDelete = deleteKeys()
+ if (toDelete.isNotEmpty()) {
+ val edit = sp.edit()
+ toDelete.forEach { edit.remove(it) }
+ edit.apply()
+ }
}
internal val sp: SharedPreferences by lazy {
@@ -33,4 +52,6 @@ open class KPref {
operator fun get(key: String): ILazyResettable<*>? = prefMap[key]
+ open fun deleteKeys(): Array<String> = arrayOf()
+
} \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
index f742078..8a582d8 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
@@ -2,7 +2,6 @@ package ca.allanwang.kau.kpref
import ca.allanwang.kau.kotlin.ILazyResettable
-object UNINITIALIZED
fun KPref.kpref(key: String, fallback: Boolean, postSetter: (value: Boolean) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
fun KPref.kpref(key: String, fallback: Double, postSetter: (value: Float) -> Unit = {}) = KPrefDelegate(key, fallback.toFloat(), this, postSetter)
@@ -25,6 +24,8 @@ class KPrefDelegate<T : Any> internal constructor(
private val key: String, private val fallback: T, private val pref: KPref, var postSetter: (value: T) -> Unit = {}, lock: Any? = null
) : ILazyResettable<T>, java.io.Serializable {
+ private object UNINITIALIZED
+
@Volatile private var _value: Any = UNINITIALIZED
private val lock = lock ?: this