Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Update 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
calintat committed Apr 14, 2017
1 parent 3066ff9 commit d663bd4
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```
108 changes: 108 additions & 0 deletions library/src/main/java/com/github/calintat/Delegates.kt
Original file line number Diff line number Diff line change
@@ -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<T> = Context.(String, T) -> T

/**
* A preference putter such as [putBoolean] or [putString].
*/
private typealias Put<T> = 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<Boolean> {

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<Float> {

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<Int> {

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<Long> {

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<String> {

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<T>(val key: String, val defValue: T, val get: Get<T>, val put: Put<T>) {

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)
}
}

0 comments on commit d663bd4

Please sign in to comment.