Omkar Amberkar
06/13/2023, 10:23 PMSam Pengilly
06/13/2023, 10:27 PMtheapache64
06/13/2023, 10:33 PMsealed interface Event
class OnTaskOneFinished(
valueFromFoo : Int
) : Event
class FooViewModel {
private val events : MutableSharedFlow<Event>
fun performTaskOne(){
viewModelScope.launch {
// task goes here
val myValue = calculateValue()
events.value = OnTaskOneFinished(myValue)
}
}
}
@Composable
fun MyComposable(
foo : FooViewModel,
bar : BarViewModel,
){
LaunchedEffect(foo, bar){
foo.events.collect { event ->
when(event){
is OnTaskOneFinished -> {
bar.onTaskOneFinished(event.valueFromFoo)
}
}
}
}
Button(
onClick = {
foo.performTaskOne()
}
) {
Text("Click Me!")
}
}
@Sam Pengilly Any problem with this approach ?Omkar Amberkar
06/13/2023, 10:36 PMSam Pengilly
06/13/2023, 10:39 PMSnackbarHostState
) but less so for ViewModels, I’d say if arbitrary injection into composables/state holders becomes possible then there wouldn’t be a need for using ViewModels in compose at alldewildte
06/14/2023, 12:08 AMbar.onTaskOneFinished(event.valueFromFoo)
Implies that bar knows something about foo, ie TaskOne. (which sounds like a UseCase object to me)
When really you are saying that this screen needs to know about both.dewildte
06/14/2023, 12:12 AM