-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to observe any / multiple preferences at once #12
Comments
This would only work if the types are know and the same for all preferences. Your first scenario can be achieved by combining different preferences, though, like this: val darkThemePref = flowSharedPreferences.getBoolean("darkTheme")
val mainColorPref = flowSharedPreferences.getInt("mainColor")
val accentColorPref = flowSharedPreferences.getInt("accentColor")
combine(darkThemePref.asFlow(), mainColorPref.asFlow(), accentColorPref.asFlow()) {
darkTheme, mainColor, accentColor -> // ...
} And to satisfy your second example, you could create your own extension based on the implementation we have here that could look like something like this: fun SharedPreferences.asFlowAny() =
callbackFlow {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> offerCatching(key) }
registerOnSharedPreferenceChangeListener(listener)
awaitClose { unregisterOnSharedPreferenceChangeListener(listener) }
}
.onStart { emit("first load trigger") }
.map { it to getString(it, "") } // assuming all preferences you have are string
.conflate()
// https://github.com/Kotlin/kotlinx.coroutines/issues/974
private fun <E> SendChannel<E>.offerCatching(element: E): Boolean {
return runCatching { offer(element) }.getOrDefault(false)
} The types must be the same in order for this to work, though, and that's a big limitation. |
Thanks for the detailed response - I've set something up on my side that works as I want it now. Just one thing: The |
Yeah that's fair, what you're asking is a simple translation of |
The new API was added on 1.4.0, thanks for the idea! |
I would like to be able to observe any key or only a bunch of keys inside a preference file - any plans on adding this?
Something like following:
The flow should emit a custom class that also holds the key, e.g. a simple sealed class...
The text was updated successfully, but these errors were encountered: