https://kotlinlang.org logo
#moko
Title
# moko
a

Ahmad Hassan

03/28/2023, 11:40 AM
How to use moko-mvvm ViewModel with Composable Screens?
a

alex009

03/28/2023, 11:56 AM
hi. at now use can use ViewModel with
remember
. like:
Copy code
val vm = remember { MyViewModel() }
but on android after activity recreation you got new viewmodel instance in this case. also
onCleared
will be not called automatically.
one of my team research now how integrate ViewModel better then just
remember
. when we found solution - we put it in
moko-mvvm
new release and also add into https://github.com/icerockdev/moko-compose-multiplatform-template
it's all about Compose Multiplatform. if you want to use moko-mvvm in android Composable you can use any way that you use with android jetpack ViewModel
a

Ahmad Hassan

03/28/2023, 12:31 PM
so, as of now, State will be lost in configuration changes?
a

alex009

03/28/2023, 12:32 PM
if you want use from Compose Multiplatform - yes. if from android compose - no
a

Ahmad Hassan

03/28/2023, 12:34 PM
Well, I want to use from Compose Multiplatform. Any time, when it will be fixed?
a

alex009

03/28/2023, 12:37 PM
there nothing to "fix". it's not bug because ViewModel self not store self. On android ViewModels saved on configuration change by android navigation component, or by hilt, or by koin. not by viewmodel component. we now research how implement similar behavior for compose multiplatform. now i can't say any estimates
you can try to implement own solution, with expect actual for example. also if you share own this solution to include in moko-mvvm - it will be good help 🙂
now i implement experiment version of
getViewModel
for compose multiplatform. while release not published you can use it in own project just by copy-paste 🙂 and can give feedback about usability 🙂 https://github.com/icerockdev/moko-mvvm/tree/%23232-compose-multiplatform/mvvm-compose
🥳 1
Moko mvvm 0.16.0 released with getviewmodel support for compose multiplatform
🎉 1
https://github.com/icerockdev/moko-compose-multiplatform-template here you can check usage. Configuration change handled correctly, onCleared work good too
🎉 1
825 Views