Jan
05/16/2023, 7:50 AMclass LoginComponent(
componentContext: ComponentContext,
) : ILoginComponent, ComponentContext by componentContext, KoinComponent {
private val _viewModel: LoginViewModel =
instanceKeeper.getOrCreate {
getKoin().get()
}
override val uiState: StateFlow<LoginUiState> = _viewModel.uiState
...
}
• My LoginViewModel extends InstanceKeeper.Instance and has an own coroutine scope which gets cleared 'onDestroy' and is lazily initiated
• LoginContent/LoginComponent/LoginViewModel is used within a SlotNavigation
So it seems that the lifecycle of the ViewModel is working correctly on Android. E.g. LoginComponent is recreated during orientationChange, but LoginViewModel stays the same. Thats what I want. So good so far.
I am just wondering if thats a valid approach. I mean its working, so its valid, but is there a "better" way to do it? Should I instead retain the Component instead of the ViewModel or is that exactly the as described here? ThanksArkadii Ivanov
05/16/2023, 7:58 AMJan
05/16/2023, 9:26 AMclass RootComponent:KoinComponent {
...
childSlot(
source = slotNavigation,
handleBackButton = true
) { config, componentContext ->
LoginComponent(
componentContext = componentContext
loginViewModel = getKoin().get()
)
}
It seems that the ViewModel is then created on each orientationChange when using this way of injection.
So I think I have to use constructor injection in my RootComponentn as well which leads me to the question if its a good approach to put DefaultComponentContext and Components to Koin?Arkadii Ivanov
05/16/2023, 9:37 AMJan
05/16/2023, 9:37 AM