Reputation: 4712
I would like to combine two livedata / flow values conditionally. Here is my problem: Currently, I have two livedata / flow values. One LiveData value always emits type Status<Unit>
while the second LiveData value emits T
. When the first LiveData value emits Status.Success
I manually set View to visible and now know that the second LiveData value will emit T
.
What I now want is, to get the second Livedata value T
inside my first LiveData value onSucess
block
class MyViewModel() : ViewModel() {
val myDownloadState: LiveData<Status<Unit>> = ...
val myDownloadData: LiveData<T> = ...
}
class MyFragment : Fragment() {
val myViewModel = ...
myViewModel.myDownloadState.observeStatus(
viewLifecycleOwner,
onSuccess = { it: Unit
binding.myRecyclerView.isVisible = true
},
onLoading = {
binding.myRecyclerView.isVisible = false
},
onError = { it: String?
toast(it.toString())
}
)
myViewModel.myDownloadData.observe(viewLifecycleOwner) { data: T
binding.myRecylerView.submitList(data)
}
}
class MyViewModel() : ViewModel() {
val myCombinedState: LiveData<Status<T>> = ...
}
class MyFragment : Fragment() {
val myViewModel = ...
myViewModel.myCombinedState.observeStatus(
viewLifecycleOwner,
onSuccess = { it: T
binding.myRecyclerView.isVisible = true
binding.myRecylerView.submitList(data)
},
onLoading = {
binding.myRecyclerView.isVisible = false
},
onError = { it: String?
toast(it.toString())
}
)
}
Here is where the two livedata values are coming from:
interface IWorkerContract<T, R> {
// this is "myDownloadData"
val appDatabaseData: LiveData<R>
// this is "myDownloadState"
val workInfo: LiveData<Status<Unit>>
}
@Singleton
class DocumentWorkerContract @Inject constructor(
@ApplicationContext private val context: Context,
private val documentDao: DocumentDao,
) : IWorkerContract<Unit, List<DocumentCacheEntity>> {
// this is "myDownloadData"
override val appDatabaseData: LiveData<List<DocumentCacheEntity>>
get() = documentDao.getListLiveData()
// this is "myDownloadState"
override val workInfo: LiveData<Status<Unit>>
get() = WorkManager
.getInstance(context)
.getWorkInfoByIdLiveData(worker.id)
.mapToState()
}
sealed class Status<out T> {
data class Success<out T>(val data: T) : Status<T>()
class Loading<out T>(val message: String? = null) : Status<T>()
data class Failure<out T>(val message: String?) : Status<T>()
companion object {
fun <T> success(data: T) = Success(data)
fun <T> loading(message: String? = null) = Loading<T>(message)
fun <T> failed(message: String?) = Failure<T>(message)
}
}
Upvotes: 0
Views: 286
Reputation: 2421
I think you should try using switchMap
in combination with map
in this case.
Try it this way:
class MyViewModel() : ViewModel() {
val myCombineState: LiveData<List<DocumentCacheEntity>> = myDownloadState.switchMap { state ->
myDownloadData.map { data ->
when (state) {
is Status.Success -> {
Status.Success(data)
}
is Status.Loading -> {
Status.Loading()
}
is Status.Error -> {
Status.Error()
}
}
}
}
}
Upvotes: 1