-
Notifications
You must be signed in to change notification settings - Fork 7
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
Is it possible to also make those extension functions for StateFlow? #8
Comments
I'm thinking maybe you should convert your SharedFlow or Flow to StateFlow in your ViewModel using Another approach would be the MVI architecture approach, where you actually collect the flows in your ViewModel and deliver the whole view-state to the view. Then it would not matter to the view what is happening behind the scene -- they would get snapshots of a full, complete state over time. Of course that would be quite the rework, assuming you're using the Google-suggested MVVM approach. What do you think? Would that work out? I haven't used Compose in Android yet, but given your problem that's what I came up with at first thought. |
I already use StateFlow in my ViewModel. The problem is that the solution proposed in this blog post doesn't work. @Composable
fun LocationScreen(locationFlow: Flow<Flow>) {
val lifecycleOwner = LocalLifecycleOwner.current
val locationFlowLifecycleAware = remember(locationFlow, lifecycleOwner) {
locationFlow.flowWithLifecycle(lifecycleOwner.lifecycle, Lifecycle.State.STARTED)
}
val location by locationFlowLifecycleAware.collectAsState()
// Current location, do something with it
} It does not compile because This was reported 2 months ago in the comments of this blog post but so far no answer from his author. So I took a look around for different solutions, like how to use Do you see a solution to this problem? Would it make a nice addition to your library? |
I see, @benjdero. Perhaps the Is your I will need some time to experiment and study this matter, and might have better input later. |
I think the author made some small tweaks to his code while writing this blog post and didn't double check if it still work after that. The
This may be a workaround, I will take a deeper look at all of this tomorrow. |
The problem with using
Flow
orSharedFlow
as a replacement forLiveData
with Jetpack Compose is thatFlow.collectAsState()
require to pass as argument the initial value of theFlow
.StateFlow.collectAsState()
doesn't have this requirement as it use the initial value of theStateFlow
. Architecture wise it seems problematic to specify the initial value from within theView
, it should be up to theViewModel
(which contain thisFlow
) to define the initial value.I've found your library after reading the blog posts you linked in its description, and was hopping you got a solution for this.
The text was updated successfully, but these errors were encountered: