aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-09-26 09:49:46 -0400
committerAllan Wang <me@allanwang.ca>2018-09-26 09:49:46 -0400
commitf37d7782b7d0269755348b41bfa63dbd50593a6b (patch)
treed763168e5e1e064985059253534c4aa32bfa9917 /core/src/main/kotlin
parent4b38bf156c67e2d6b3a649624bbcb12edbb52232 (diff)
downloadkau-f37d7782b7d0269755348b41bfa63dbd50593a6b.tar.gz
kau-f37d7782b7d0269755348b41bfa63dbd50593a6b.tar.bz2
kau-f37d7782b7d0269755348b41bfa63dbd50593a6b.zip
Update versions and begin kpref update
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt2
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt66
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt64
3 files changed, 97 insertions, 35 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
index 804eacb..c94b06d 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
@@ -71,7 +71,7 @@ class EmailBuilder(val email: String, val subject: String) {
val appInfo = context.packageManager.getPackageInfo(context.packageName, 0)
emailBuilder.append("\nApp: ").append(context.packageName)
.append("\nApp Version Name: ").append(appInfo.versionName)
- .append("\nApp Version Code: ").append(appInfo.versionCode).append("\n")
+ .append("\nApp Version Code: ").append(appInfo.longVersionCode).append("\n")
} catch (e: PackageManager.NameNotFoundException) {
KL.e { "EmailBuilder packageInfo not found" }
}
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 33ba807..9dbca49 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt
@@ -3,15 +3,26 @@ package ca.allanwang.kau.kpref
import ca.allanwang.kau.kotlin.ILazyResettable
-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)
-fun KPref.kpref(key: String, fallback: Float, postSetter: (value: Float) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
-fun KPref.kpref(key: String, fallback: Int, postSetter: (value: Int) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
-fun KPref.kpref(key: String, fallback: Long, postSetter: (value: Long) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
-fun KPref.kpref(key: String, fallback: Set<String>, postSetter: (value: Set<String>) -> Unit = {}) = KPrefDelegate(key, StringSet(fallback), this, postSetter)
-fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter)
+fun KPref.kpref(key: String, fallback: Boolean, postSetter: (value: Boolean) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefBooleanTransaction, postSetter)
-class StringSet(set: Collection<String>) : LinkedHashSet<String>(set)
+fun KPref.kpref(key: String, fallback: Float, postSetter: (value: Float) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefFloatTransaction, postSetter)
+
+fun KPref.kpref(key: String, fallback: Double, postSetter: (value: Float) -> Unit = {}) =
+ kpref(key, fallback.toFloat(), postSetter)
+
+fun KPref.kpref(key: String, fallback: Int, postSetter: (value: Int) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefIntTransaction, postSetter)
+
+fun KPref.kpref(key: String, fallback: Long, postSetter: (value: Long) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefLongTransaction, postSetter)
+
+fun KPref.kpref(key: String, fallback: Set<String>?, postSetter: (value: Set<String>) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefSetTransaction) { postSetter(it ?: emptySet()) }
+
+fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Unit = {}) =
+ KPrefDelegate(key, fallback, this, KPrefStringTransaction, postSetter)
/**
* Created by Allan Wang on 2017-06-07.
@@ -20,19 +31,24 @@ class StringSet(set: Collection<String>) : LinkedHashSet<String>(set)
* Contains a unique key for the shared preference as well as a nonnull fallback item
* Also contains an optional mutable postSetter that will be called every time a new value is given
*/
-class KPrefDelegate<T : Any> internal constructor(
- private val key: String, private val fallback: T, private val pref: KPref, private var postSetter: (value: T) -> Unit = {}, lock: Any? = null
+class KPrefDelegate<T> internal constructor(
+ private val key: String,
+ private val fallback: T,
+ private val pref: KPref,
+ private val transaction: KPrefTransaction<T>,
+ private var postSetter: (value: T) -> Unit = {}
) : ILazyResettable<T> {
private object UNINITIALIZED
- @Volatile private var _value: Any = UNINITIALIZED
- private val lock = lock ?: this
+ @Volatile
+ private var _value: Any? = UNINITIALIZED
+ private val lock = this
init {
if (pref.prefMap.containsKey(key))
throw KPrefException("$key is already used elsewhere in preference ${pref.PREFERENCE_NAME}")
- pref.prefMap.put(key, this@KPrefDelegate)
+ pref.prefMap[key] = this@KPrefDelegate
}
override fun invalidate() {
@@ -52,15 +68,7 @@ class KPrefDelegate<T : Any> internal constructor(
@Suppress("UNCHECKED_CAST")
_v2 as T
} else {
- _value = when (fallback) {
- is Boolean -> pref.sp.getBoolean(key, fallback)
- is Float -> pref.sp.getFloat(key, fallback)
- is Int -> pref.sp.getInt(key, fallback)
- is Long -> pref.sp.getLong(key, fallback)
- is StringSet -> StringSet(pref.sp.getStringSet(key, fallback))
- is String -> pref.sp.getString(key, fallback)
- else -> throw KPrefException(fallback)
- }
+ _value = transaction.get(pref.sp, key, fallback)
@Suppress("UNCHECKED_CAST")
_value as T
}
@@ -74,20 +82,10 @@ class KPrefDelegate<T : Any> internal constructor(
operator fun setValue(any: Any, property: kotlin.reflect.KProperty<*>, t: T) {
_value = t
val editor = pref.sp.edit()
- when (t) {
- is Boolean -> editor.putBoolean(key, t)
- is Float -> editor.putFloat(key, t)
- is Int -> editor.putInt(key, t)
- is Long -> editor.putLong(key, t)
- is StringSet -> editor.putStringSet(key, t)
- is String -> editor.putString(key, t)
- else -> throw KPrefException(t)
- }
+ transaction.set(editor, key, t)
editor.apply()
postSetter(t)
}
}
-class KPrefException(message: String) : IllegalAccessException(message) {
- constructor(element: Any?) : this("Invalid type in pref cache: ${element?.javaClass?.simpleName ?: "null"}")
-} \ No newline at end of file
+class KPrefException(message: String) : IllegalAccessException(message) \ No newline at end of file
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt
new file mode 100644
index 0000000..d02cf06
--- /dev/null
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt
@@ -0,0 +1,64 @@
+package ca.allanwang.kau.kpref
+
+import android.content.SharedPreferences
+
+internal interface KPrefTransaction<T> {
+ fun get(prefs: SharedPreferences, key: String, fallback: T): T
+ fun set(editor: SharedPreferences.Editor, key: String, data: T)
+}
+
+internal object KPrefBooleanTransaction : KPrefTransaction<Boolean> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: Boolean) =
+ prefs.getBoolean(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: Boolean) {
+ editor.putBoolean(key, data)
+ }
+}
+
+internal object KPrefIntTransaction : KPrefTransaction<Int> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: Int) =
+ prefs.getInt(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: Int) {
+ editor.putInt(key, data)
+ }
+}
+
+internal object KPrefLongTransaction : KPrefTransaction<Long> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: Long) =
+ prefs.getLong(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: Long) {
+ editor.putLong(key, data)
+ }
+}
+
+internal object KPrefFloatTransaction : KPrefTransaction<Float> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: Float) =
+ prefs.getFloat(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: Float) {
+ editor.putFloat(key, data)
+ }
+}
+
+internal object KPrefStringTransaction : KPrefTransaction<String> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: String) =
+ prefs.getString(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: String) {
+ editor.putString(key, data)
+ }
+}
+
+internal object KPrefSetTransaction : KPrefTransaction<Set<String>?> {
+ override fun get(prefs: SharedPreferences, key: String, fallback: Set<String>?) =
+ prefs.getStringSet(key, fallback)
+
+ override fun set(editor: SharedPreferences.Editor, key: String, data: Set<String>?) {
+ editor.putStringSet(key, data)
+ }
+}
+
+