Title
e

Emmanuel

06/15/2022, 1:35 PM
Hey folks, is there a concept of viewModel in compose desktop?
b

Big Chungus

06/15/2022, 1:44 PM
Concept - yes, it's just a concept afterall so it's framework ambiguous. If you're looking for dedicated API, then no. But you can achieve very similar result with regular view model classes that contain mutable state fields
šŸ¤™šŸ¾ 1
e

Emmanuel

06/15/2022, 1:45 PM
That's the direction I was headed but didn't want to reinvent. Thanks!
b

Big Chungus

06/15/2022, 1:46 PM
e.g.:
class MyViewModel {
  var count by mutableStateOf(0)
}

@Composable
fun Component() {
  val vm = remember{MyViewModel()}
  Button(
    text = vm.count.toString(),
    onClick = {vm.count++}
  )
}
e

Emmanuel

06/15/2022, 1:48 PM
That looks about right. This follows jetpack compose state holders as the source of truth option.
m

Mitchell Syer

06/15/2022, 1:53 PM
Some multiplatform navigation libraries come with a implementation of ViewModels. Voyager for instance comes with ScreenModel that works really well with its navigation
šŸ‘†šŸ» 1
šŸ‘† 1
šŸ‘šŸ¾ 1
m

Michael Paus

06/15/2022, 2:21 PM
I prefer to use StateFlows in my view models to keep them independent from any compose libraries.
šŸ¤™šŸ¾ 1
s

SrSouza

06/15/2022, 2:30 PM
Take a look at Voyager one: https://voyager.adriel.cafe/screenmodel
t

Tlaster

06/16/2022, 12:40 PM