How do you use the `view model` for Kotlin Multipl...
# multiplatform
v
How do you use the
view model
for Kotlin Multiplatform(
Desktop, Android
) projects? In Android, the view model extends ViewModel (Android), but that's a natural thing to say, Desktop hasn't ViewModel. So I can't use the common view model. Additionally, I think some things, but It looks not good... A. Inject view model of each platform
Copy code
Window(onCloseRequest = { // Do something }, title = "Test") {
    val testViewModel by inject<TestViewModel>(TestViewModel::class.java)

    TesetScreen(
        user = testViewModel.user,
        testState = testViewModel.testState.collecAsState().value,
        testAction = testViewModel::testAction
    )
    
    DisposableEffect(Unit) {
        onDispose { 
            testViewModel.onCleared()
        }
    }
}
B. Use use-case direct in the Composable Screen
Copy code
Window(onCloseRequest = { // Do something }, title = "Test") {
    val getUserUseCase by inject<GetUserUseCase>(GetUserUseCase::class.java)
    val getTestState by inject<GetUserUseCase>(GetUserUseCase::class.java)

    TesetScreen(
        user = testViewModel.user,
        testState = getTestState().collecAsState().value,
        testAction = { doSomething() }
    )
}
C. Don't use the view model... What do you think about the above logic and how do you design architecture KMP?
m
v
Thank you! I try it!
263 Views