There is no enough information about how to use ko...
# kodein
k
There is no enough information about how to use kodein with viewmodels and save state handler with compose in kodein docs, can someone explain about how to use it, I couldn't find any thing about it
Please update compose docs there is zero information about how to use viewmodels in kodein
r
Here is how to use Kodein and view models on Android https://kosi-libs.org/kodein/7.16/framework/android.html#_view_models
There is nothing specific about Android view models in Kodein with Compose docs because it is compatible with Compose Multiplatform, thus not relying only on Android but also on the Desktop and the Web.
I guess we could add a new module with proper helper functions targeting only Android and Compose
k
viewModelWithSavedStateHandle only works in activity and fragement but I want to use it inside composable I tried to make a extension but it seams my knowledge is not enough can you write down an extension for geting viewmodel with save state in composables?
also does kodein has a feature like koin to inject parameter that is only available during runime when user input them ?
Copy code
@OptIn(KoinInternalApi::class)
@Composable
inline fun <reified T : ViewModel> getViewModel(
    qualifier: Qualifier? = null,
    viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
        "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
    },
    key: String? = null,
    extras: CreationExtras = defaultExtras(viewModelStoreOwner),
    scope: Scope = GlobalContext.get().scopeRegistry.rootScope,
    noinline parameters: ParametersDefinition? = null,
): T {
    return koinViewModel(qualifier, viewModelStoreOwner, key, extras, scope, parameters)
}
someting like koin's parameters
r
By Koin’s parameters you mean using a factory then ? Otherwise I don’t get why you would need such parameters. In Kodein we declare Factories https://kosi-libs.org/kodein/7.16/core/bindings.html#factory-bindings and then use arg at retrieval, like
by instance<Foo>(arg = 42)
But your right
viewModelWithSavedStateHandle
is an extension on Activity or Fragment, thus this is not usable in Compose as it is.
We would need to work on our project structure in order to improve our Android support.
102 Views