Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.77 KB | None | 0 0
  1. sealed class DataHolder<out T> {
  2.     data class Success<out T>(val value: T) : DataHolder<T>()
  3.     data class Error(val cause: Throwable) : DataHolder<Nothing>()
  4. }
  5.  
  6. disposables += Usecase5sec().getSingle()
  7.     .observeOn(AndroidSchedulers.mainThread())
  8.     .subscribe (
  9.         {s -> liveData.value = DataHolder.Success(s)},
  10.         {e -> liveData.value = DataHolder.Error(e)}
  11.     )  
  12.  
  13.  
  14. val viewModel = ViewModelProviders.of(this).get(VmDashboard::class.java)
  15. viewModel.liveData.observe(this, object: Observer<DataHolder<String>> {
  16.     override fun onChanged(dataHolder:DataHolder<String>?) {
  17.         when(dataHolder){
  18.             is DataHolder.Success -> vh?.setData(dataHolder.value)
  19.             is DataHolder.Error -> vh?.setError(dataHolder.cause)
  20.         }
  21.     }
  22. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement