Comments (4)
This would only work if the types are know and the same for all preferences. SharedPreferences.OnSharedPreferenceChangeListener
only emits the key
of the preference that has changed, we still need to know what method to call to retrieve the updated value (e.g. getBoolean()
or getFloat()
). Because of this limitation, and because of how the library currently plays every nicely when it comes to types, I don't see this as an interesting addition.
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.
from flow-preferences.
Thanks for the detailed response - I've set something up on my side that works as I want it now.
Just one thing: The OnSharedPreferenceChangeListener
is generic as well and does not provide type safety nor does it know which type of data was changed - you could at least consider observing changes by observing keys (no values, the observer is responsible to correctly handle the keys... I must say that I mostly only observe preferences to recreate or update activities/fragments views and mostly, I do not optimise this. This means I simply observe a list of keys and call recreate
in an activity so that the UI uses the new preferences (theme, colors, sizes, styles, ...)
from flow-preferences.
Yeah that's fair, what you're asking is a simple translation of OnSharedPreferenceChangeListener
to a flow. I could turn my keyFlow
into a public extension on top of SharedPreferences
so consumers could use it directly if they want. I'll experiment a bit here and will get back to you soon.
from flow-preferences.
The new API was added on 1.4.0, thanks for the idea!
from flow-preferences.
Related Issues (9)
- kotlinx.coroutines.JobCancellationException: FlowSubscription was cancelled HOT 9
- Random NPEs HOT 6
- Error if change type of preference with same name HOT 2
- incomprehensible flow collection HOT 11
- Flow not updating on SharedPreferences value change HOT 8
- [Feature Request] Add support for null values HOT 6
- Add documentation HOT 2
- Make preference key public to read HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flow-preferences.