raamcosta / compose-destinations

Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
https://composedestinations.rafaelcosta.xyz
Apache License 2.0
3.2k stars 132 forks source link

ViewModel state not updated after send via parameter #230

Closed ghost closed 2 years ago

ghost commented 2 years ago

Hi, I was originally using Compose Navigation and everything worked fine. But after I switched to the Compose Destinations library the ViewModels got broken. Now when I for example have ViewModel passed as a parameter, and then try to pass it into inner Composable, and when I try to get state of variable that is inside of the ViewModel from that inner Composable, the state is not updated. I tried all the possible solutions included on your documentation page, such as creating the Factory for ViewModel, setting it as a dependency but nothing worked.

Here is the Screen composable @Destination @Composable fun ProductScreen( vm: ProductViewModel = viewModel() )

And here is the inner component ContentList( vm = vm )

Could you please give me a hint where could be a problem, or maybe which area to focus on?

raamcosta commented 2 years ago

Hi! I’ll need more information. Like how is the state exposed inside the VM (is it a LiveData or a StateFlow or State?).

And what exactly is not working? Does it not recompose when that state changes?

ghost commented 2 years ago

After deeper researching of the issue to be able to describe the problem I found that it was caused by another error and then the ViewModel sharing between the destinations was fixed quickly, but thank you anyway for the quick reply.