The view model has 3 live data coming in from 3 different api calls. Now, I want to display all the data together as different cards in a list in the recycler view.
What would be the best way to do this ?
Currently I'm using MediatorLiveData, but now I need to have 4 different api calls, which would make 4 live data objects. Therefore, I'm trying to use kotlin flow for this problem. How can I combine the. 4 different Live Data coming in from different UseCases into a single Flow object and then collect it in the Fragment ?
Following is the method, where I'm trying to combine the 3 live data using combine method :
private val flow1 = pojo1UseCase.data.asFlow()
private val flow2 = pojo2UseCase.data.asFlow()
private val flow3 = pojo3UseCase.data.asFlow()
suspend fun combine() {
flow1.combine(flow2) { f1, f2 ->
listOf(f1.right(), f2.right())
}.combine(flow3) { f1, f3 ->
listOf(f1, f3.right())
}.collect { list ->
list.forEach {
ba = it as? Pojo1
oc = it as? Pojo2
sd = it as? Pojo3
}
}
}
I'm calling collect here just for testing purpose, but i intend to call collect method in fragment. Can someone share a better way to do similar thing ?