Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android-Live-Shared-Preferences-Gist6
class MultiPreference<T> constructor(private val updates: Observable<String>,
private val preferences: SharedPreferences,
private val keys: List<String>,
private val defaultValue: T) : MutableLiveData<Map<String, T>>() {
private var disposable: Disposable? = null
private val values = mutableMapOf<String, T>()
init {
for (key in keys)
values[key] = preferences.all[key] as T ?: defaultValue
}
override fun onActive() {
super.onActive()
value = values
disposable = updates.filter { t -> keys.contains(t) }.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribeWith(object: DisposableObserver<String>() {
override fun onComplete() {
}
override fun onNext(t: String) {
values[t] = preferences.all[t] as T ?: defaultValue
postValue(values)
}
override fun onError(e: Throwable) {
}
})
}
override fun onInactive() {
super.onInactive()
disposable?.dispose()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.