From d663bd4430a27b1cdd8d067c9eb64520efdbb64e Mon Sep 17 00:00:00 2001 From: calintat Date: Fri, 14 Apr 2017 22:01:24 +0100 Subject: [PATCH] Update 1.2 --- README.md | 6 + .../java/com/github/calintat/Delegates.kt | 108 ++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 library/src/main/java/com/github/calintat/Delegates.kt diff --git a/README.md b/README.md index 3fef876..a662ec4 100644 --- a/README.md +++ b/README.md @@ -39,4 +39,10 @@ You can also use the following to populate a container view with a preference fr ```java populateWithPreferences(int containerViewId, int preferencesResId) +``` + +Since version 1.2, you can also use property delegates (in Kotlin only): + +```kotlin +val str by stringPref("pref_string", "default") ``` \ No newline at end of file diff --git a/library/src/main/java/com/github/calintat/Delegates.kt b/library/src/main/java/com/github/calintat/Delegates.kt new file mode 100644 index 0000000..25266e9 --- /dev/null +++ b/library/src/main/java/com/github/calintat/Delegates.kt @@ -0,0 +1,108 @@ +package com.github.calintat + +import android.content.Context +import kotlin.reflect.KProperty + +/** + * A preference getter such as [getBoolean] or [getString]. + */ +private typealias Get = Context.(String, T) -> T + +/** + * A preference putter such as [putBoolean] or [putString]. + */ +private typealias Put = Context.(String, T) -> Unit + +/** + * Returns a property delegate for a read/write property corresponding to a boolean preference. + * Note that this uses the default shared preferences of the receiver context. + * Changing the value of the property will be reflected in the shared preferences. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + * + * @throws ClassCastException if there is a preference with this name that is not a boolean. + */ +fun Context.booleanPref(key: String, defValue: Boolean = false): PreferenceProperty { + + return PreferenceProperty(key, defValue, Context::getBoolean, Context::putBoolean) +} + +/** + * Returns a property delegate for a read/write property corresponding to a float preference. + * Note that this uses the default shared preferences of the receiver context. + * Changing the value of the property will be reflected in the shared preferences. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + * + * @throws ClassCastException if there is a preference with this name that is not a float. + */ +fun Context.floatPref(key: String, defValue: Float = 0f): PreferenceProperty { + + return PreferenceProperty(key, defValue, Context::getFloat, Context::putFloat) +} + +/** + * Returns a property delegate for a read/write property corresponding to an int preference. + * Note that this uses the default shared preferences of the receiver context. + * Changing the value of the property will be reflected in the shared preferences. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + * + * @throws ClassCastException if there is a preference with this name that is not an int. + */ +fun Context.intPref(key: String, defValue: Int = 0): PreferenceProperty { + + return PreferenceProperty(key, defValue, Context::getInt, Context::putInt) +} + +/** + * Returns a property delegate for a read/write property corresponding to a long preference. + * Note that this uses the default shared preferences of the receiver context. + * Changing the value of the property will be reflected in the shared preferences. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + * + * @throws ClassCastException if there is a preference with this name that is not a long. + */ +fun Context.longPref(key: String, defValue: Long = 0): PreferenceProperty { + + return PreferenceProperty(key, defValue, Context::getLong, Context::putLong) +} + +/** + * Returns a property delegate for a read/write property corresponding to a string preference. + * Note that this uses the default shared preferences of the receiver context. + * Changing the value of the property will be reflected in the shared preferences. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + * + * @throws ClassCastException if there is a preference with this name that is not a string. + */ +fun Context.stringPref(key: String, defValue: String = ""): PreferenceProperty { + + return PreferenceProperty(key, defValue, Context::getString, Context::putString) +} + +/** + * A property delegate of properties that use a preference as a sort of backing field. + * + * @param key The name of the preference used as a backing field for the property. + * @param defValue The default value of the property if the preference does not exist. + */ +class PreferenceProperty(val key: String, val defValue: T, val get: Get, val put: Put) { + + operator fun getValue(thisRef: Context, property: KProperty<*>): T { + + return thisRef.get(key, defValue) + } + + operator fun setValue(thisRef: Context, property: KProperty<*>, value: T) { + + thisRef.put(key, value) + } +} \ No newline at end of file