https://kotlinlang.org logo
#koin-contributors
Title
# koin-contributors
r

Ruben Quadros

03/25/2022, 5:19 PM
Hi guys, this is regarding this PR. Want your suggestions. More info in the thread
I was thinking of creating an extension function on
ViewModelOwner
in
koin-androidx-navigation
module and then calling this extension function from
koin-androidx-compose getViewModel(..)
func as follows:
Copy code
fun ViewModelOwner.fromStoreOwner(storeOwner: ViewModelStoreOwner): ViewModelOwner = ViewModelOwner(
    store = storeOwner.viewModelStore,
    stateRegistry = stateRegistry,
    defaultArgs = if (storeOwner is NavBackStackEntry) storeOwner.arguments else null
)
Copy code
inline fun <reified T : ViewModel> getViewModel(
    qualifier: Qualifier? = null,
    owner: ViewModelStoreOwner? = null,
    scope: Scope = GlobalContext.get().scopeRegistry.rootScope,
    noinline parameters: ParametersDefinition? = null,
): T {
    val storeOwner = owner?.let { ViewModelOwner.fromStoreOwner(it) } ?: getComposeViewModelOwner()
    return remember(qualifier, parameters) {
        scope.getViewModel(qualifier, { storeOwner }, parameters)
    }
}
but this would still require
koin-androidx-navigation
dependency on
koin-androidx-compose
@arnaud.giuliani sorry for the tag but would need your suggestion. Please check comments on PR as well 🙂
a

arnaud.giuliani

04/04/2022, 7:01 AM
sure, let me see 👍
🙏 1
r

Ruben Quadros

04/14/2022, 4:58 PM
Based on PR comments I have made the following changes:
Copy code
inline fun <reified T : ViewModel> viewModel(
    qualifier: Qualifier? = null,
    owner: ViewModelStoreOwner? = null,
    scope: Scope = GlobalContext.get().scopeRegistry.rootScope,
    noinline parameters: ParametersDefinition? = null,
): Lazy<T> {
    val storeOwner = owner?.let { ViewModelOwner.fromCompose(it) } ?: getComposeViewModelOwner()
    val state = (owner as? NavBackStackEntry)?.arguments ?: storeOwner.defaultArgs
    return remember(qualifier, parameters) {
        ViewModelLazy(T::class, { storeOwner.storeOwner.viewModelStore }){
            getViewModelFactory<T>(
                owner = {storeOwner},
                qualifier = qualifier,
                parameters = parameters,
                scope = scope,
                state = state?.let { {it} } ?: emptyState()
            )
        }
    }
}
Copy code
fun getComposeViewModelOwner(): ViewModelOwner {
    return ViewModelOwner.fromCompose(
        LocalViewModelStoreOwner.current!!,
        LocalSavedStateRegistryOwner.current
    )
}
Copy code
fun ViewModelOwner.Companion.fromCompose(
    storeOwner: ViewModelStoreOwner,
    stateRegistry: SavedStateRegistryOwner? = null
) = ViewModelOwner(
    storeOwner = storeOwner,
    stateRegistry = stateRegistry,
    defaultArgs = (storeOwner as? NavBackStackEntry)?.arguments
)
Copy code
fun ViewModelOwner.Companion.fromCompose(
    storeOwner: ViewModelStoreOwner
) = ViewModelOwner(
    storeOwner = storeOwner,
    defaultArgs = (storeOwner as? NavBackStackEntry)?.arguments
)
But
ViewModelOwner
will be deprecated next? If so, what should I use?
a

arnaud.giuliani

04/15/2022, 6:59 AM
I believe in 3.2, I need to clean up the way we are composing all of this. With 3.1.6 patch, it brings things closer to last Google VM API
need a bit of time to make a concrete proposal
👍 1
🙏 1
r

Ruben Quadros

04/15/2022, 4:54 PM
Will be waiting for your reply/changes to proceed 🙏
5 Views