Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class WaybillConfirmationViewModel @Inject constructor(
- private val router: Router,
- private val interactor: DriverInteractor,
- private val mapper: WaybillConfirmationMapper
- ) : BaseViewModel<WaybillConfirmationViewModel.State,
- WaybillConfirmationViewModel.Intent,
- WaybillConfirmationViewModel.Error>() {
- override fun bindIntents(viewIntents: Observable<Intent>): Observable<State> {
- val intentsViewStates = viewIntents
- .flatMap {
- when (it) {
- Intent.LOAD_WAYBILL -> loadWaybill()
- Intent.SEND_GO_LATER_STATUS -> sendGoLaterStatus()
- Intent.SEND_READY_STATUS -> sendReadyStatus()
- }
- }
- return Observable
- .merge(loadWaybill(), intentsViewStates)
- .startWith(State(isLoading = true, noWaybill = false))
- }
- private fun loadWaybill(): Observable<State> {
- return interactor
- .getWaybill()
- .toObservable()
- .map { mapper.mapToState(it) }
- .startWith(viewState.copy(isLoading = true))
- .onErrorReturn {
- if (it !is NoWaybillException) {
- sendEvent(Error.WAYBILL_LOADING_ERROR)
- }
- State(isLoading = false, noWaybill = true)
- }
- }
- private fun sendReadyStatus(): Observable<State> {
- return interactor
- .sendReadyToRideStatus()
- .andThen(Observable.just(viewState.copy(isLoading = false)))
- .onErrorReturnItem(viewState.copy(isLoading = false))
- .doOnError { sendEvent(Error.STATUS_SENDING_ERROR) }
- .startWith(Observable.just(viewState.copy(isLoading = true)))
- }
- private fun sendGoLaterStatus(): Observable<State> {
- return Completable
- .fromAction { router.navigateTo(Screen.GoLaterScreen()) }
- .andThen(Observable.just(viewState.copy(isLoading = false)))
- }
- data class State(
- val isLoading: Boolean,
- val noWaybill: Boolean,
- val departurePlace: String? = null,
- val invoiceDate: String? = null,
- val invoiceTime: String? = null,
- val scheduledShiftEndDate: String? = null,
- val scheduledShiftEndTime: String? = null
- )
- enum class Intent {
- LOAD_WAYBILL,
- SEND_GO_LATER_STATUS,
- SEND_READY_STATUS
- }
- enum class Error {
- WAYBILL_LOADING_ERROR,
- STATUS_SENDING_ERROR
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement