arnaud.giuliani
10/21/2021, 2:05 PMkoinNavGraphViewModel() function to scope a ViewModel to a given Nav Grapharnaud.giuliani
10/21/2021, 2:06 PMarnaud.giuliani
10/21/2021, 2:07 PMNacho Ruiz Martin
10/21/2021, 2:09 PMarnaud.giuliani
10/22/2021, 9:02 AMNacho Ruiz Martin
10/28/2021, 3:17 PM.xml graph files, nor graph IDs.
I have a proposal for a modification of the current getViewModel Composable function to be able to pass the ViewModelStoreOwner. Since NavBackStackEntry implements the interface (doc) it would be easy to use. You can see the proposal in this gist:
https://gist.github.com/iruizmar/e15c89e6cbe868f3633facbd4cd313cc
⚠️ It’s not tested in depth, this is just a draft proposal.arnaud.giuliani
10/29/2021, 10:30 AMviewModelStoreOwner: ViewModelStoreOwner = LocalViewModelStoreOwner.current!!, inside the getViewModel ?Nacho Ruiz Martin
10/29/2021, 10:31 AMViewModelStoreOwner such as a NavBackStackEntry and the ViewModel scope will be attached to that one.Nacho Ruiz Martin
10/29/2021, 10:34 AMNavBackStackEntry.
Here’s how Hilt is doing it (similar way):
https://developer.android.com/jetpack/compose/libraries?#hilt-navigationNacho Ruiz Martin
11/02/2021, 5:40 PMNacho Ruiz Martin
11/04/2021, 1:28 PMarnaud.giuliani
11/04/2021, 5:59 PMarnaud.giuliani
11/04/2021, 6:04 PMval parentEntry = remember {
navController.getBackStackEntry("bc")
}
ScreenB(
viewModel = getViewModel(parentEntry),
navigateToA = { navController.navigate("a") },
navigateToC = { navController.navigate("c") }
)arnaud.giuliani
11/04/2021, 6:04 PMgetNavGraphViewModel("bc") directlyNacho Ruiz Martin
11/04/2021, 7:23 PMgetNavGraphViewModel for that, though.arnaud.giuliani
11/05/2021, 9:09 AMNacho Ruiz Martin
11/05/2021, 9:11 AMgetNavGraphViewModel("bc", navController)
Is that good enough?arnaud.giuliani
11/05/2021, 10:39 AMarnaud.giuliani
11/10/2021, 4:06 PMNacho Ruiz Martin
11/10/2021, 4:11 PMLocalViewModelStoreOwner.current which happens to be the current destination on the graph.
So if you use the current API from composableB, you’ll get a ViewModel that will live while you are inside that destination. You can’t have a ViewModel attached to a subgraph (shared between B and C, in other words).
And that is what fixes my approach, by letting the user to provide it with a different owner, in this case:
navController.getBackStackEntry("bc")arnaud.giuliani
11/10/2021, 5:09 PMNacho Ruiz Martin
11/11/2021, 11:55 AMgetViewModel, is that OK?arnaud.giuliani
11/12/2021, 8:25 AMarnaud.giuliani
11/12/2021, 8:26 AMarnaud.giuliani
11/12/2021, 8:26 AMNacho Ruiz Martin
11/12/2021, 8:57 AMNacho Ruiz Martin
12/01/2021, 11:37 AMgetViewModel expects a ViewModelOwner and Navigation’s NavStackEntry are actually `ViewModelStoreOwner`s .
This makes the way to use the API uglier, like this:
getViewModel(owner = ViewModelOwner.from(parentEntry))
I don’t know the real difference between ViewModelOwner and ViewModelStoreOwner . Is there any reason for you to be using the first one?arnaud.giuliani
12/02/2021, 8:58 AMarnaud.giuliani
12/02/2021, 8:58 AMNacho Ruiz Martin
12/04/2021, 1:10 PMgetViewModel method would suffice. If you provide a ViewModelStoreOwner then Koin will use ViewModelOwner.from to get a ViewModelOwner. WDYT?arnaud.giuliani
12/06/2021, 9:07 AMarnaud.giuliani
12/06/2021, 9:07 AM