Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.58 KB | None | 0 0
  1. class WaybillConfirmationViewModel @Inject constructor(
  2.     private val router: Router,
  3.     private val interactor: DriverInteractor,
  4.     private val mapper: WaybillConfirmationMapper
  5. ) : BaseViewModel<WaybillConfirmationViewModel.State>() {
  6.  
  7.     init {
  8.         loadWaybill()
  9.     }
  10.  
  11.     override fun getInitialViewState(): State = State(isLoading = true)
  12.  
  13.     fun retry() {
  14.         if (viewState.error == State.Error.WAYBILL_LOADING_ERROR) {
  15.             loadWaybill()
  16.         } else if (viewState.error == State.Error.STATUS_SENDING_ERROR) {
  17.             sendReadyStatus()
  18.         }
  19.     }
  20.  
  21.     private fun loadWaybill() {
  22.         disposables += interactor
  23.             .getWaybill()
  24.             .map<State> { mapper.mapToState(it) }
  25.             .toObservable()
  26.             .startWith(viewState.copy(isLoading = true))
  27.             .observeOn(mainThread())
  28.             .subscribeBy(
  29.                 onNext = {
  30.                     viewState = it
  31.                 },
  32.                 onError = {
  33.                     viewState = if (it is NoWaybillException) {
  34.                         viewState.copy(isLoading = false, error = State.Error.NO_WAYBILL)
  35.                     } else {
  36.                         viewState.copy(isLoading = false, error = State.Error.WAYBILL_LOADING_ERROR)
  37.                     }
  38.                 }
  39.             )
  40.     }
  41.  
  42.     fun sendReadyStatus() {
  43.         disposables += interactor
  44.             .sendReadyToRideStatus()
  45.             .observeOn(mainThread())
  46.             .doOnSubscribe { viewState = viewState.copy(isLoading = true, error = null) }
  47.             .subscribeBy(
  48.                 onComplete = {
  49.                     //                    router.navigateTo(TODO())
  50.                     viewState = viewState.copy(isLoading = false)
  51.                 },
  52.                 onError = {
  53.                     viewState = viewState.copy(isLoading = false, error = State.Error.STATUS_SENDING_ERROR)
  54.                 }
  55.             )
  56.     }
  57.  
  58.     fun sendGoLaterStatus() {
  59.         viewState = viewState.copy(isLoading = false, error = null)
  60.         router.navigateTo(Screen.GoLaterScreen())
  61.     }
  62.  
  63.     data class State(
  64.         val isLoading: Boolean,
  65.         val error: Error? = null,
  66.         val departurePlace: String? = null,
  67.         val invoiceDate: String? = null,
  68.         val invoiceTime: String? = null,
  69.         val scheduledShiftEndDate: String? = null,
  70.         val scheduledShiftEndTime: String? = null
  71.     ) {
  72.  
  73.         enum class Error {
  74.             NO_WAYBILL,
  75.             WAYBILL_LOADING_ERROR,
  76.             STATUS_SENDING_ERROR
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement