jean
02/24/2023, 9:47 AMclass PaymentViewModel(...) : ViewModel() { // in an android app
private val _state = MutableStateFlow<PaymentStatus>()
val state = _state.asStateFlow()
fun pay() {
...
}
private fun payWithCard {
paymentWithCard() // returns a flow of PaymentStatus
}
private fun payWithApp {
payWithApp() // returns a flow of PaymentStatus
}
}
How can I update _state
with the data coming from paymentWithCard()
and paymentWithApp()
? Do I have to use collect { … }
on each of them and manually update the state?Sabbib Chowdhury
02/25/2023, 2:29 PMval nums = flowOf(1, 2, 3)
val strs = flowOf("one", "two", "three")
nums.combine(strs) { a, b -> "$a -> $b" } // composed value of both flows
.collect { println(it) }
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/combine.htmljean
02/27/2023, 12:33 PM