Instantly share code, notes, and snippets.
Created
April 26, 2019 15:42
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ivanvasheka/4aca52e8ce18125f671c58c806a6e8dc to your computer and use it in GitHub Desktop.
SharedPreferences Delegates
This file contains hidden or 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 android.content.SharedPreferences | |
import kotlin.properties.ReadWriteProperty | |
import kotlin.reflect.KProperty | |
fun SharedPreferences.boolean(defaultValue: Boolean, key: String = ""): SharedPreferencesBooleanDelegate { | |
return SharedPreferencesBooleanDelegate(this, key, defaultValue) | |
} | |
fun SharedPreferences.float(defaultValue: Float, key: String = ""): SharedPreferencesFloatDelegate { | |
return SharedPreferencesFloatDelegate(this, key, defaultValue) | |
} | |
fun SharedPreferences.int(defaultValue: Int, key: String = ""): SharedPreferencesIntDelegate { | |
return SharedPreferencesIntDelegate(this, key, defaultValue) | |
} | |
fun SharedPreferences.long(defaultValue: Long, key: String = ""): SharedPreferencesLongDelegate { | |
return SharedPreferencesLongDelegate(this, key, defaultValue) | |
} | |
fun SharedPreferences.string(defaultValue: String, key: String = ""): SharedPreferencesStringDelegate { | |
return SharedPreferencesStringDelegate(this, key, defaultValue) | |
} | |
fun SharedPreferences.stringSet(defaultValue: Set<String>, key: String = ""): SharedPreferencesStringSetDelegate { | |
return SharedPreferencesStringSetDelegate(this, key, defaultValue) | |
} | |
//region Delegates | |
sealed class SharedPreferencesDelegate<T>(private val key: String) : ReadWriteProperty<Any, T> { | |
protected fun getKey(property: KProperty<*>): String { | |
return if (key.isEmpty()) property.name else key | |
} | |
} | |
class SharedPreferencesBooleanDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: Boolean) : SharedPreferencesDelegate<Boolean>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): Boolean { | |
return preferences.getBoolean(getKey(property), defaultValue) | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: Boolean) { | |
preferences.edit().putBoolean(getKey(property), value).apply() | |
} | |
} | |
class SharedPreferencesFloatDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: Float) : SharedPreferencesDelegate<Float>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): Float { | |
return preferences.getFloat(getKey(property), defaultValue) | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: Float) { | |
preferences.edit().putFloat(getKey(property), value).apply() | |
} | |
} | |
class SharedPreferencesIntDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: Int) : SharedPreferencesDelegate<Int>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): Int { | |
return preferences.getInt(getKey(property), defaultValue) | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: Int) { | |
preferences.edit().putInt(getKey(property), value).apply() | |
} | |
} | |
class SharedPreferencesLongDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: Long) : SharedPreferencesDelegate<Long>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): Long { | |
return preferences.getLong(getKey(property), defaultValue) | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: Long) { | |
preferences.edit().putLong(getKey(property), value).apply() | |
} | |
} | |
class SharedPreferencesStringDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: String) : SharedPreferencesDelegate<String>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): String { | |
return preferences.getString(getKey(property), defaultValue) ?: defaultValue | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: String) { | |
preferences.edit().putString(getKey(property), value).apply() | |
} | |
} | |
class SharedPreferencesStringSetDelegate( | |
private val preferences: SharedPreferences, | |
key: String, | |
private val defaultValue: Set<String>) : SharedPreferencesDelegate<Set<String>>(key) { | |
override operator fun getValue(thisRef: Any, property: KProperty<*>): Set<String> { | |
return preferences.getStringSet(getKey(property), defaultValue) ?: defaultValue | |
} | |
override operator fun setValue(thisRef: Any, property: KProperty<*>, value: Set<String>) { | |
preferences.edit().putStringSet(getKey(property), value).apply() | |
} | |
} | |
//endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment