Skip to content

Instantly share code, notes, and snippets.

@SpaceBison
Last active September 25, 2018 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpaceBison/a318ec21e319f7ac20e37bb29d92509d to your computer and use it in GitHub Desktop.
Save SpaceBison/a318ec21e319f7ac20e37bb29d92509d to your computer and use it in GitHub Desktop.
Android Kotlin - SharedPreferences property delegates
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KFunction3
import kotlin.reflect.KProperty
@Suppress("UNCHECKED_CAST")
inline fun <reified T> sharedPreferences(sharedPreferences: SharedPreferences, key: String): SharedPreferencesNullableProperty<T> =
when (T::class) {
Boolean::class -> SharedPreferencesNullableProperty(sharedPreferences, key, false,
SharedPreferences::getBoolean, SharedPreferences.Editor::putBoolean)
Float::class -> SharedPreferencesNullableProperty(sharedPreferences, key, 0f,
SharedPreferences::getFloat, SharedPreferences.Editor::putFloat)
Int::class -> SharedPreferencesNullableProperty(sharedPreferences, key, 0,
SharedPreferences::getInt, SharedPreferences.Editor::putInt)
Long::class -> SharedPreferencesNullableProperty(sharedPreferences, key, 0L,
SharedPreferences::getLong, SharedPreferences.Editor::putLong)
String::class -> SharedPreferencesNullableProperty(sharedPreferences, key, null,
SharedPreferences::getString, SharedPreferences.Editor::putString)
Set::class -> SharedPreferencesNullableProperty(sharedPreferences, key, null,
SharedPreferences::getStringSet, SharedPreferences.Editor::putStringSet)
else -> throw IllegalStateException("Illegal property type: ${T::class}")
} as SharedPreferencesNullableProperty<T>
class SharedPreferencesNullableProperty<T>(
private val sharedPreferences: SharedPreferences,
val key: String,
val defaultValue: T,
val getValue: KFunction3<SharedPreferences, String, T, T>,
val setValue: KFunction3<SharedPreferences.Editor, String, T, SharedPreferences.Editor>)
: ReadWriteProperty<Any, T?> {
override fun getValue(thisRef: Any, property: KProperty<*>): T? =
if (!sharedPreferences.contains(key)) {
null
} else {
getValue(sharedPreferences, key, defaultValue)
}
override fun setValue(thisRef: Any, property: KProperty<*>, value: T?) =
sharedPreferences.edit().apply {
if (value == null) {
remove(key)
} else {
setValue(sharedPreferences.edit(), key, value).apply()
}
}.apply()
}
import android.content.SharedPreferences
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KFunction3
import kotlin.reflect.KProperty
@Suppress("UNCHECKED_CAST")
fun <T> sharedPreferences(sharedPreferences: SharedPreferences, key: String, defaultValue: T): SharedPreferencesProperty<T> =
when (defaultValue) {
is Boolean -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
SharedPreferences::getBoolean, SharedPreferences.Editor::putBoolean)
is Float -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
SharedPreferences::getFloat, SharedPreferences.Editor::putFloat)
is Int -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
SharedPreferences::getInt, SharedPreferences.Editor::putInt)
is Long -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
SharedPreferences::getLong, SharedPreferences.Editor::putLong)
is String -> SharedPreferencesProperty(sharedPreferences, key, defaultValue,
SharedPreferences::getString, SharedPreferences.Editor::putString)
is Set<*> -> SharedPreferencesProperty(sharedPreferences, key, defaultValue as Set<String>,
SharedPreferences::getStringSet, SharedPreferences.Editor::putStringSet)
else -> throw IllegalStateException("Illegal property type: ${(defaultValue as Any).javaClass}")
} as SharedPreferencesProperty<T>
open class SharedPreferencesProperty<T>(
private val sharedPreferences: SharedPreferences,
val key: String,
val defaultValue: T,
val getValue: KFunction3<SharedPreferences, String, T, T>,
val setValue: KFunction3<SharedPreferences.Editor, String, T, SharedPreferences.Editor>)
: ReadWriteProperty<Any, T> {
override fun getValue(thisRef: Any, property: KProperty<*>): T =
getValue(sharedPreferences, key, defaultValue)
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) =
setValue(sharedPreferences.edit(), key, value).apply()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment