Hayk
07/31/2024, 7:18 PM// That's in the module
scope(named(LOGGED_IN_SCOPE)) {
scopedOf(::MyViewModel)
}
// When I want to create my scope, I call this
fun createLoggedInScope() {
val koin = KoinPlatformTools.defaultContext().getOrNull() ?: return
_loggedInScope.update {
koin.getOrCreateScope(LOGGED_IN_SCOPE_ID, named(LOGGED_IN_SCOPE))
}
}
fun closeLoggedInScope() {
_loggedInScope.value?.close()
_loggedInScope.value = null
}
And whenever I use my ViewModel, I do
val myViewModel by loggedInScope.inject<MyViewModel>()
Caleb Cook
07/31/2024, 8:21 PMHayk
07/31/2024, 9:08 PMviewModelOf(::MyViewModel)
- and I pass it in a constructor of my composable, the ViewModel gets cleared when that composable is disposed or a configuration change happens. So I guess it's tied to my Composable lifecycle, not activity or fragment. For my purpose I can use the viewModelOf
in my module, it's just gets cleared and reinit on configuration change, i.e., new instance is created each time. If I could fix that, I wouldn't need custom scopes too. Regarding other issues, I can check whether my scope is closed or not before performing any operation. Though yeah, I agree that my approach is not that good, but it's just a template code, cause in the real app everything is way more complexed.Caleb Cook
08/01/2024, 2:27 AMCaleb Cook
08/01/2024, 2:29 AMHayk
08/01/2024, 9:41 AMHayk
08/01/2024, 9:41 AMStylianos Gakis
08/01/2024, 9:45 AM