Hi there, congrats for the release of Koin 4.0.0 a...
# koin
u
Hi there, congrats for the release of Koin 4.0.0 and it’s great Kotlin/Compose multiplatform support. I am currently looking into compose navigation and found
koinNavViewModel
were the only available piece of information on the whole internet is from the online documentation:
Copy code
koinNavViewModel() - inject ViewModel instance + Navigation arguments data (if you are using Navigation API)
And from KDoc
Copy code
Resolve ViewModel instance with Navigation NavBackStackEntry as extras parameters
I do not have the slightest idea what that might mean nor do I understand if both claims are the same. Well, I have ideas but none of them turned out to be what the method does. Same questions has been asked before but just before the final answer the thread dies :-) https://kotlinlang.slack.com/archives/C67HDJZ2N/p1680127134580369 Most surprising, comparing the source code of
koinViewModel
and
koinNavViewModel
I could not find any difference. Well
koinViewModel
has a default parameter of
defaultExtras()
while
koinNavViewModel
has a default parameter of
defaultNavExtras
. But those two are finnaly 100% identical. Thanks for shedding any light on 1. Precondition: when and where can the method be used. A.k.a can ‘if you are using Navigation API’ be further specified. Do I need to be inside a NavGraphBuilder? Just in a composable that is nested in any nav graph? 2. What does it do? A.k.a can ‘instance with Navigation NavBackStackEntry as extras parameters’ be further specified. I expected a view model constructor parameter of type NavBackStackEntry to be automatically injected by koin. But that seems as if it does not work. Maybe a small example would be really helpful.
a
ViewModel Nav is bringing NavBackStack arguments to your VM with the
extras
those extras allow extraction to SavedStateBundle usage
u
Thanks for the response. But this code is not in 4.0, is it?
Well, it is. What I mean is, is the implementation from v4.0.0 of
defaultNavExtras
the expected implementation?
Copy code
fun defaultNavExtras(viewModelStoreOwner: ViewModelStoreOwner): CreationExtras = when {
    viewModelStoreOwner is HasDefaultViewModelProviderFactory -> viewModelStoreOwner.defaultViewModelCreationExtras
    else -> CreationExtras.Empty
}
a
yes. NavBackStackEntry fall in 1st case automatically yes