From f37d7782b7d0269755348b41bfa63dbd50593a6b Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Wed, 26 Sep 2018 09:49:46 -0400 Subject: Update versions and begin kpref update --- .../main/groovy/ca/allanwang/kau/Versions.groovy | 8 +-- .../kotlin/ca/allanwang/kau/ui/views/CutoutView.kt | 2 +- .../kotlin/ca/allanwang/kau/email/EmailBuilder.kt | 2 +- .../kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 66 +++++++++++----------- .../ca/allanwang/kau/kpref/KPrefTransaction.kt | 64 +++++++++++++++++++++ 5 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt diff --git a/buildSrc/src/main/groovy/ca/allanwang/kau/Versions.groovy b/buildSrc/src/main/groovy/ca/allanwang/kau/Versions.groovy index 7e1ff2c..33da690 100644 --- a/buildSrc/src/main/groovy/ca/allanwang/kau/Versions.groovy +++ b/buildSrc/src/main/groovy/ca/allanwang/kau/Versions.groovy @@ -9,7 +9,7 @@ class Versions { static def buildTools = '28.0.2' // https://developer.android.com/topic/libraries/support-library/revisions - static def supportLibs = '28.0.0-rc02' + static def supportLibs = '28.0.0' // https://kotlinlang.org/docs/reference/using-gradle.html static def kotlin = '1.2.71' @@ -27,14 +27,14 @@ class Versions { static def constraintLayout = '1.1.3' // https://github.com/mikepenz/FastAdapter#using-maven - static def fastAdapter = '3.2.5' + static def fastAdapter = '3.2.9' static def fastAdapterCommons = fastAdapter // https://github.com/bumptech/glide/releases static def glide = '4.7.1' // https://github.com/mikepenz/Android-Iconics#1-provide-the-gradle-dependency - static def iconics = '3.0.3' + static def iconics = '3.0.4' static def iconicsGoogle = '3.0.1.2' static def iconicsMaterial = '2.2.0.4' static def iconicsCommunity = '2.0.46.1' @@ -45,7 +45,7 @@ class Versions { static def junit = '4.12' static def testRunner = '1.0.1' - static def gradlePlugin = '3.2.0-rc03' + static def gradlePlugin = '3.2.0' static def mavenPlugin = '2.1' static def playPublishPlugin = '1.2.2' diff --git a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt index eeaac6e..f6f7864 100644 --- a/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt +++ b/core-ui/src/main/kotlin/ca/allanwang/kau/ui/views/CutoutView.kt @@ -82,7 +82,7 @@ class CutoutView @JvmOverloads constructor( if (attrs != null) { val a = context.obtainStyledAttributes(attrs, R.styleable.CutoutView, 0, 0) if (a.hasValue(R.styleable.CutoutView_font)) - paint.typeface = context.getFont(a.getString(R.styleable.CutoutView_font)) + paint.typeface = context.getFont(a.getString(R.styleable.CutoutView_font)!!) foregroundColor = a.getColor(R.styleable.CutoutView_foregroundColor, foregroundColor) text = a.getString(R.styleable.CutoutView_android_text) ?: text minHeight = a.getDimension(R.styleable.CutoutView_android_minHeight, minHeight) 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, postSetter: (value: Set) -> 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) : LinkedHashSet(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?, postSetter: (value: Set) -> 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) : LinkedHashSet(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 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 internal constructor( + private val key: String, + private val fallback: T, + private val pref: KPref, + private val transaction: KPrefTransaction, + private var postSetter: (value: T) -> Unit = {} ) : ILazyResettable { 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 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 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 { + fun get(prefs: SharedPreferences, key: String, fallback: T): T + fun set(editor: SharedPreferences.Editor, key: String, data: T) +} + +internal object KPrefBooleanTransaction : KPrefTransaction { + 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 { + 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 { + 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 { + 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 { + 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?> { + override fun get(prefs: SharedPreferences, key: String, fallback: Set?) = + prefs.getStringSet(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: Set?) { + editor.putStringSet(key, data) + } +} + + -- cgit v1.2.3