I want to use koin to create a small library that includes some android UI - for this I would want to inject dependencies into viewmodels used by such UI. (Yes this library will be quite opinionated and not expected for generic reusability)
I found this on context isolation, but I’m not sure how to use this with functions such as
koinViewModel()
. Any suggestions on how to go about this?
Mini
06/13/2022, 10:32 AM
Copy code
@OptIn(KoinInternalApi::class)
@Composable
inline fun <reified T : ViewModel> myKoinViewModel(
qualifier: Qualifier? = null,
owner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
scope: Scope = MyKoinContext.koinApp!!.koin.scopeRegistry.rootScope,
noinline parameters: ParametersDefinition? = null
): T {
return getViewModel(qualifier, owner, scope, parameters)
}
This seems to work, but it requires my library/sdk to opt in to KoinInternalApi, which doesn’t feel great? 🤔
Mini
06/13/2022, 5:54 PM
@arnaud.giuliani sorry for the ping, but im very curious if you have any input on this 🙏
a
arnaud.giuliani
06/15/2022, 2:09 PM
Compose & Scope/Context are not well defined for now. You need to resolve against your Koin context and not the default one