Skip to content

Instantly share code, notes, and snippets.

@ibrahimsn98
Last active July 27, 2019 11:22
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 ibrahimsn98/fba90f1b6a5c442eae486cbbcbf066d3 to your computer and use it in GitHub Desktop.
Save ibrahimsn98/fba90f1b6a5c442eae486cbbcbf066d3 to your computer and use it in GitHub Desktop.
Android-Live-Shared-Preferences-Git2
class LiveSharedPreferences constructor(private val preferences: SharedPreferences) {
private val publisher = PublishSubject.create<String>()
private val listener = OnSharedPreferenceChangeListener { _, key -> publisher.onNext(key) }
private val updates = publisher.doOnSubscribe {
preferences.registerOnSharedPreferenceChangeListener(listener)
}.doOnDispose {
if (!publisher.hasObservers())
preferences.unregisterOnSharedPreferenceChangeListener(listener)
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment