Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.53 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.         WaybillConfirmationViewModel.Intent,
  7.         WaybillConfirmationViewModel.Error>() {
  8.  
  9.     override fun bindIntents(viewIntents: Observable<Intent>): Observable<State> {
  10.         val intentsViewStates = viewIntents
  11.             .flatMap {
  12.                 when (it) {
  13.                     Intent.LOAD_WAYBILL -> loadWaybill()
  14.                     Intent.SEND_GO_LATER_STATUS -> sendGoLaterStatus()
  15.                     Intent.SEND_READY_STATUS -> sendReadyStatus()
  16.                 }
  17.             }
  18.  
  19.         return Observable
  20.             .merge(loadWaybill(), intentsViewStates)
  21.             .startWith(State(isLoading = true, noWaybill = false))
  22.     }
  23.  
  24.     private fun loadWaybill(): Observable<State> {
  25.         return interactor
  26.             .getWaybill()
  27.             .toObservable()
  28.             .map { mapper.mapToState(it) }
  29.             .startWith(viewState.copy(isLoading = true))
  30.             .onErrorReturn {
  31.                 if (it !is NoWaybillException) {
  32.                     sendEvent(Error.WAYBILL_LOADING_ERROR)
  33.                 }
  34.  
  35.                 State(isLoading = false, noWaybill = true)
  36.             }
  37.     }
  38.  
  39.     private fun sendReadyStatus(): Observable<State> {
  40.         return interactor
  41.             .sendReadyToRideStatus()
  42.             .andThen(Observable.just(viewState.copy(isLoading = false)))
  43.             .onErrorReturnItem(viewState.copy(isLoading = false))
  44.             .doOnError { sendEvent(Error.STATUS_SENDING_ERROR) }
  45.             .startWith(Observable.just(viewState.copy(isLoading = true)))
  46.     }
  47.  
  48.     private fun sendGoLaterStatus(): Observable<State> {
  49.         return Completable
  50.             .fromAction { router.navigateTo(Screen.GoLaterScreen()) }
  51.             .andThen(Observable.just(viewState.copy(isLoading = false)))
  52.     }
  53.  
  54.     data class State(
  55.         val isLoading: Boolean,
  56.         val noWaybill: Boolean,
  57.         val departurePlace: String? = null,
  58.         val invoiceDate: String? = null,
  59.         val invoiceTime: String? = null,
  60.         val scheduledShiftEndDate: String? = null,
  61.         val scheduledShiftEndTime: String? = null
  62.     )
  63.  
  64.     enum class Intent {
  65.         LOAD_WAYBILL,
  66.         SEND_GO_LATER_STATUS,
  67.         SEND_READY_STATUS
  68.     }
  69.  
  70.     enum class Error {
  71.         WAYBILL_LOADING_ERROR,
  72.         STATUS_SENDING_ERROR
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement