Probably gonna seem dumb for asking this. But how ...
# compose-desktop
c
Probably gonna seem dumb for asking this. But how do people do stuff in compose desktop since you don't have a "VM" like in Android? I understand that AAC VM is an uneeded thing, but I've only ever really done android dev and so im confused on how you'd create a presenter or something that survives for the "lifetime" of a "screen" in the compose desktop world.
s
The navigation library Voyager has the concept of ScreenModel, similar to VM, it survives until the Screen is in the stack. https://voyager.adriel.cafe/screenmodel
c
A “ViewModel” is just a design pattern, there’s nothing inherent to it that’s only available on Android. The ViewModel base class in Android just exists to tie the ViewModel’s lifecycle to the app, but with Compose you could use
rememberCoroutineScope()
to define the lifetime of a ViewModel as when a particular Composable function is displayed
This snippet in the #ballast documentation shows the general idea https://copper-leaf.github.io/ballast/wiki/platforms/compose-desktop/ Ballast is specifically an MVI library, providing a structure for managing state, which ensures everything is processed in a given CoroutineScope
a
Decompose offers cross-platform lifecycle management as well
t
https://github.com/Tlaster/PreCompose come with ViewModel that is similar with AAC ViewModel
a
The other portion of this, in android, a VM will circumvent the normal activity lifecycle, in desktop, there's less lifecycle to worry about. You could probably make an kmm VM if you want to share the pattern, if you want to do things from scratch
c
Here’s an example I’ve shared here many times, showing how one could create a custom ViewModel class for use in Compose Desktop. No need to bring in additional libraries primarily built for other purposes, or make things more complicated than they need to be https://kotlinlang.slack.com/archives/C01D6HTPATV/p1644259697620799?thread_ts=1644257413.142249&cid=C01D6HTPATV
a
In Compose for Android, the
ViewModel
has one additional and very important purpose - it lives while the corresponding screen is in the back stack. This may be also applicable to single-window desktop apps.
d
I've worked on something that has a view model and navigation like Android has, but I haven't worked on it because I moved to voyager https://github.com/dragossusi/navigation-compose