Created
December 23, 2014 05:20
-
-
Save rlac/8ed54e5f38237a436208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.properties.ReadWriteProperty | |
import android.content.SharedPreferences | |
import android.app.Activity | |
import android.content.Context | |
import android.app.Fragment | |
import android.support.v4.app.Fragment as SupportFragment | |
import kotlin.properties.Delegates | |
public fun Activity.intPreference(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = intPref(prefs, key, default) | |
public fun Activity.stringPreference(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = stringPref(prefs, key, default) | |
public fun Activity.booleanPreference(prefs: String, key: String, default: Boolean): ReadWriteProperty<Any, Boolean> = booleanPref(prefs, key, default) | |
public fun Activity.floatPreference(prefs: String, key: String, default: Float): ReadWriteProperty<Any, Float> = floatPref(prefs, key, default) | |
public fun Fragment.intPreference(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = intPref(prefs, key, default) | |
public fun Fragment.stringPreference(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = stringPref(prefs, key, default) | |
public fun Fragment.booleanPreference(prefs: String, key: String, default: Boolean): ReadWriteProperty<Any, Boolean> = booleanPref(prefs, key, default) | |
public fun Fragment.floatPreference(prefs: String, key: String, default: Float): ReadWriteProperty<Any, Float> = floatPref(prefs, key, default) | |
public fun SupportFragment.intPreference(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = intPref(prefs, key, default) | |
public fun SupportFragment.stringPreference(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = stringPref(prefs, key, default) | |
public fun SupportFragment.booleanPreference(prefs: String, key: String, default: Boolean): ReadWriteProperty<Any, Boolean> = booleanPref(prefs, key, default) | |
public fun SupportFragment.floatPreference(prefs: String, key: String, default: Float): ReadWriteProperty<Any, Float> = floatPref(prefs, key, default) | |
public fun ContextProvider.intPreference(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = intPref(prefs, key, default) | |
public fun ContextProvider.stringPreference(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = stringPref(prefs, key, default) | |
public fun ContextProvider.booleanPreference(prefs: String, key: String, default: Boolean): ReadWriteProperty<Any, Boolean> = booleanPref(prefs, key, default) | |
public fun ContextProvider.floatPreference(prefs: String, key: String, default: Float): ReadWriteProperty<Any, Float> = floatPref(prefs, key, default) | |
private fun intPref(prefs: String, key: String, default: Int): ReadWriteProperty<Any, Int> = PreferenceVal(prefs, key, default, { this.getInt(key, default) }, { i -> this.putInt(key, i) }) | |
private fun stringPref(prefs: String, key: String, default: String): ReadWriteProperty<Any, String> = PreferenceVal(prefs, key, default, { this.getString(key, default) }, { s -> this.putString(key, s) }) | |
private fun booleanPref(prefs: String, key: String, default: Boolean): ReadWriteProperty<Any, Boolean> = PreferenceVal(prefs, key, default, { this.getBoolean(key, default) }, { b -> this.putBoolean(key, b) }) | |
private fun floatPref(prefs: String, key: String, default: Float): ReadWriteProperty<Any, Float> = PreferenceVal(prefs, key, default, { this.getFloat(key, default) }, { f -> this.putFloat(key, f) }) | |
public trait ContextProvider { | |
val context: Context | |
} | |
private class PreferenceVal<T>(val prefs: String, | |
val key: String, | |
val default: T, | |
val read: SharedPreferences.() -> T, | |
val write: SharedPreferences.Editor.(T) -> Unit) : ReadWriteProperty<Any, T> { | |
private var sp: SharedPreferences? = null | |
override fun set(thisRef: Any, desc: PropertyMetadata, value: T) { | |
if (sp == null) { | |
sp = get(thisRef) | |
} | |
val edit = sp!!.edit() | |
edit.write(value) | |
edit.apply() | |
} | |
override fun get(thisRef: Any, desc: PropertyMetadata): T { | |
if (sp == null) { | |
sp = get(thisRef) | |
} | |
return sp!!.read() | |
} | |
private fun get(ref: Any): SharedPreferences = | |
when (ref) { | |
is Activity -> ref.getSharedPreferences(prefs, Context.MODE_PRIVATE) | |
is Fragment -> ref.getActivity().getSharedPreferences(prefs, Context.MODE_PRIVATE) | |
is SupportFragment -> ref.getActivity().getSharedPreferences(prefs, Context.MODE_PRIVATE) | |
is ContextProvider -> ref.context.getSharedPreferences(prefs, Context.MODE_PRIVATE) | |
else -> throw IllegalStateException("Unsupported type") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment