简体   繁体   中英

Combine LiveData or Flow based on the first LiveData or Flow value

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

Current approach

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)
   }

}

What I want

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()
}

State Class

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)
    }
}

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()  
                }
            }
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM