簡體   English   中英

根據第一個 LiveData 或 Flow 值組合 LiveData 或 Flow

[英]Combine LiveData or Flow based on the first LiveData or Flow value

我想有條件地組合兩個實時數據/流量值。 這是我的問題:目前,我有兩個實時數據/流量值。 一個 LiveData 值始終發出Status<Unit>類型,而第二個 LiveData 值發出T 當第一個 LiveData 值發出Status.Success時,我手動將 View 設置為可見,現在知道第二個 LiveData 值將發出T

我現在想要的是,在我的第一個 LiveData 值onSucess塊中獲取第二個 Livedata 值T

目前的做法

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

這是兩個 livedata 值的來源:

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

在這種情況下,我認為您應該嘗試將switchMapmap結合使用。

試試這樣:

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM