Skip to content

Instantly share code, notes, and snippets.

@egorikftp
Created Aug 22, 2021
Embed
What would you like to do?
class StringPreference(
private val preferences: SharedPreferences,
private val name: String,
private val defaultValue: String
) : ReadWriteProperty<Any, String?> {
@WorkerThread
override fun getValue(thisRef: Any, property: KProperty<*>) =
preferences.getString(name, defaultValue) ?: defaultValue
override fun setValue(thisRef: Any, property: KProperty<*>, value: String?) {
preferences.edit {
putString(name, value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment