Instantly share code, notes, and snippets.
Last active
July 19, 2019 13:28
-
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 AlonsoFloo/035aa5d778a1d2bb11c9d9624dc8474c to your computer and use it in GitHub Desktop.
Koltin property delegate tools for Android SharedPreferences
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
class SharedPreferenceManager( | |
val context: Context | |
) : SharedPreferences.OnSharedPreferenceChangeListener { | |
private val sharedPreferences by lazy { | |
PreferenceManager.getDefaultSharedPreferences(context) | |
} | |
private val propertyChangeListeners = hashMapOf<String, HashSet<() -> Unit>>() | |
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?): Unit { | |
propertyChangeListeners[key]?.forEach { it() } | |
} | |
fun addListener(property: KProperty<*>, callback: () -> Unit) { | |
propertyChangeListeners[property.name] | |
.takeIf { it == null } | |
.run { | |
propertyChangeListeners[property.name] = HashSet() | |
} | |
propertyChangeListeners[property.name]?.apply { | |
this.add(callback) | |
} | |
} | |
fun removeListener(property: KProperty<*>, callback: () -> Unit) { | |
propertyChangeListeners[property.name]?.apply { | |
this.remove(callback) | |
} | |
} | |
var propertyOne by sharedPreferences.stringDelegate() | |
var propertyTwo by sharedPreferences.dateDelegate() | |
var propertyBool by sharedPreferences.booleanDelegate(false) | |
} |
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
class SharedPreferencePropertyDelegate<T>( | |
private val sharedPreferences: SharedPreferences, | |
private val defaultValue: T, | |
private val getter: SharedPreferences.(String, T) -> T, | |
private val setter: SharedPreferences.Editor.(String, T) -> SharedPreferences.Editor | |
) : ReadWriteProperty<Any, T> { | |
override fun getValue(thisRef: Any, property: KProperty<*>): T = | |
sharedPreferences.getter(property.name, defaultValue) | |
override fun setValue(thisRef: Any, property: KProperty<*>, value: T) = | |
sharedPreferences.edit() | |
.setter(property.name, value) | |
.apply() | |
} | |
fun SharedPreferences.booleanDelegate(defaultValue: Boolean = false): ReadWriteProperty<Any, Boolean> = | |
SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getBoolean, | |
setter = SharedPreferences.Editor::putBoolean | |
) | |
fun SharedPreferences.intDelegate(defaultValue: Int = 0): ReadWriteProperty<Any, Int> = | |
SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getInt, | |
setter = SharedPreferences.Editor::putInt | |
) | |
fun SharedPreferences.longDelegate(defaultValue: Long = 0L): ReadWriteProperty<Any, Long> = | |
SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getLong, | |
setter = SharedPreferences.Editor::putLong | |
) | |
fun SharedPreferences.floatDelegate(defaultValue: Float = 0f): ReadWriteProperty<Any, Float> = | |
SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getFloat, | |
setter = SharedPreferences.Editor::putFloat | |
) | |
fun SharedPreferences.stringDelegate(defaultValue: String? = null): ReadWriteProperty<Any, String?> = | |
SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getString, | |
setter = SharedPreferences.Editor::putString | |
) | |
fun SharedPreferences.getDate(key: String, defaultValue: Date? = null): Date? = | |
getLong(key, -1).let { | |
if (it >= 0) Date(it) else defaultValue | |
} | |
fun SharedPreferences.Editor.putDate(key: String, value: Date? = null): SharedPreferences.Editor = | |
if (value != null) { | |
putLong(key, value.time) | |
} else { | |
remove(key) | |
} | |
fun SharedPreferences.dateDelegate(defaultValue: Date? = null): ReadWriteProperty<Any, Date?> { | |
return SharedPreferencePropertyDelegate( | |
sharedPreferences = this, | |
defaultValue = defaultValue, | |
getter = SharedPreferences::getDate, | |
setter = SharedPreferences.Editor::putDate | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment