Skip to content

Instantly share code, notes, and snippets.

@egorikftp
Created August 22, 2021 20:14
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 egorikftp/de0579d1bcd29d783ebec72a5b21a727 to your computer and use it in GitHub Desktop.
Save egorikftp/de0579d1bcd29d783ebec72a5b21a727 to your computer and use it in GitHub Desktop.
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