'Scope a viewmodel between 2 fragments with koin 2.2.3 and jetpack navigation
My project is on Koin 2.2.3 and I search to scope a viewModel in function of a graph. In my ViewModelModule I got this:
val Int.navQualifier: Qualifier
get() = named("FlowQualifier (NavGraphId@$this)")
fun navQualifier(navGraphId: Int) =
navGraphId.navQualifier
val Int.navScopeId: ScopeID
get() = "ScopeId (NavGraphId@${this})"
scope(navQualifier(R.id.expenses_list_nav_graph)) {
viewModel { (activity: AppCompatActivity) ->
ExpensesListViewModel(get(), get(), get(), activity, androidContext())
}
}
In my fragments, I got this:
private val viewModel: ExpensesListViewModel by sharedGraphViewModel(R.id.expenses_list_nav_graph) {
parametersOf(requireActivity())}
And this is my function sharedGraphViewModel:
inline fun <reified VM : ViewModel> Fragment.sharedGraphViewModel(
@IdRes navGraphId: Int,
qualifier: Qualifier? = null,
noinline parameters: ParametersDefinition? = null) = lazy {
val store = findNavController().getViewModelStoreOwner(navGraphId).viewModelStore
getKoin().getViewModel(ViewModelParameter(VM::class, qualifier, parameters, null, store, null))}
Solution 1:[1]
you should increase your koin version to 3.1.3, there is a whole new delegation : koinNavGraphViewModel https://insert-koin.io/docs/reference/koin-android/viewmodel/#navigation-graph-viewmodel-updated-in-313
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | keivan shamlu |