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>() {
- init {
- loadWaybill()
- }
- override fun getInitialViewState(): State = State(isLoading = true)
- fun retry() {
- if (viewState.error == State.Error.WAYBILL_LOADING_ERROR) {
- loadWaybill()
- } else if (viewState.error == State.Error.STATUS_SENDING_ERROR) {
- sendReadyStatus()
- }
- }
- private fun loadWaybill() {
- disposables += interactor
- .getWaybill()
- .map<State> { mapper.mapToState(it) }
- .toObservable()
- .startWith(viewState.copy(isLoading = true))
- .observeOn(mainThread())
- .subscribeBy(
- onNext = {
- viewState = it
- },
- onError = {
- viewState = if (it is NoWaybillException) {
- viewState.copy(isLoading = false, error = State.Error.NO_WAYBILL)
- } else {
- viewState.copy(isLoading = false, error = State.Error.WAYBILL_LOADING_ERROR)
- }
- }
- )
- }
- fun sendReadyStatus() {
- disposables += interactor
- .sendReadyToRideStatus()
- .observeOn(mainThread())
- .doOnSubscribe { viewState = viewState.copy(isLoading = true, error = null) }
- .subscribeBy(
- onComplete = {
- // router.navigateTo(TODO())
- viewState = viewState.copy(isLoading = false)
- },
- onError = {
- viewState = viewState.copy(isLoading = false, error = State.Error.STATUS_SENDING_ERROR)
- }
- )
- }
- fun sendGoLaterStatus() {
- viewState = viewState.copy(isLoading = false, error = null)
- router.navigateTo(Screen.GoLaterScreen())
- }
- data class State(
- val isLoading: Boolean,
- val error: Error? = null,
- val departurePlace: String? = null,
- val invoiceDate: String? = null,
- val invoiceTime: String? = null,
- val scheduledShiftEndDate: String? = null,
- val scheduledShiftEndTime: String? = null
- ) {
- enum class Error {
- NO_WAYBILL,
- WAYBILL_LOADING_ERROR,
- STATUS_SENDING_ERROR
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement