Hey :wave: Is there any way to scope a `viewModel`...
# koin
n
Hey 👋 Is there any way to scope a
viewModel
to a navigation graph using Jetpack compose navigation? I would like a
viewModel
to be used by several composables and get cleared and reinstantiated when the user exists -> reenters to that screen flow.
If I just use it like in the docs:
Copy code
@Composable
fun App(viewModel : Clazz = getViewModel()) {
    ...
}

@Composable
fun App2(viewModel : Clazz = getViewModel()) {
    ...
}
I’m getting different instances of the viewmodel on each composable. I guess this is expected, but is there any way to share it?
So basically I’m asking if this: https://developer.android.com/jetpack/compose/libraries#hilt
Copy code
val parentViewModel = hiltViewModel<ParentViewModel>(
    navController.getBackStackEntry("Parent")
)
is possible with Koin.
What I don't really get is why I'm receiving different instances of the ViewModel on the composables if they are used within the same Activity. I had to retrieve the ViewModel on the Activity and not on the Composables to be able to share data between them. Is this the expected behaviour?
s
n
Is downgrading a solution for this?
s
Seems no. I saw external fix before. This is a problem with viewModelOwner detection. But can't find it again now.
n
“This parameter allows wiring your ViewModel with a custom ViewModelOwner, like Graph Navigation component:” https://link.medium.com/7RzafKeMqib
Maybe?
@arnaud.giuliani sorry for the ping. Are you aware of this issue with the compose navigation component? Do you have any idea how to fix it? I could prepare a PR with some directions.
a
Yes, scoping VM is not yet really safe 😕 we need work on that
n
Ok, please, let me know if there’s something I can do to help.
a
VM, Navigation & Scopes are not quite clear for now. I would need a sandbox app to help work against it for Koin.
n
I can set that up
a
Yes, if you can. I always use a “toy app”, really minimal to just illustrate the usecases we need to get working
👍 1
if you want to help, you can draft something 👍
n
I will start by creating the toy app, I’m not really sure in which direction should the actual fix go, though.
https://github.com/iruizmar/koin-nav-compose-toy/tree/master Here it is. It's very basic! I wrote down a description on the Readme. Please, let me know if you are looking for something else.
a
Great, let’s continue a thread on #koin-dev 👍
👍 1