CRamsan
05/30/2025, 2:56 AMViewModelDependencies
) that I want to scope to be a singleton
within a single Window of my application. I am not sure how to approach it, I have tried to use a rememberKoinModules(WindowModule)
on each window but the singleton is shared across both windows. I also tried to use KoinScope<String>(UUID.random().toString()) {...}
and put the rememberKoinModules(WindowModule)
inside the scope but the ViewModelDependencies
was still a singleton across both scopes. 🤔 I assume I am doing something wrong and I am not understanding something about the scoping API. Would someone have an idea for how to approach this problem?
internal val WindowModule = module {
single {
ViewModelDependencies()
}
viewModel {
WindowViewModel()
}
}
class WindowViewModel(
private val dependencies: ViewModelDependencies,
) : ViewModel() {...}
@Composable
fun WindowScreen(
viewModel: WindowViewModel= koinViewModel(),
) {...}
fun main() = application {
KoinApplication(
application = {
modules(...)
}
) {
Window(
onCloseRequest = ::exitApplication,
title = "Window 1",
) {
WindowScreen()
}
Window(
onCloseRequest = ::exitApplication,
title = "Window 2",
) {
WindowScreen()
}
}
}
CRamsan
05/30/2025, 3:02 AMfun main() = application {
KoinApplication(
application = {
modules(...)
}
) {
Window(
onCloseRequest = ::exitApplication,
title = "Window 1",
) {
KoinScope<Long>("Window1") {
rememberKoinModules { WindowModule }
WindowScreen()
}
}
Window(
onCloseRequest = ::exitApplication,
title = "Window 2",
) {
KoinScope<Long>("Window2") {
rememberKoinModules { WindowModule }
WindowScreen()
}
}
}
CRamsan
05/31/2025, 8:21 AM