Last active
May 16, 2024 12:34
-
-
Save MkhytarMkhoian/888a6ae1e260c6a30f6dc01ab5689052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FareListViewModel( | |
private val exceptionHandler: ExceptionHandler, | |
private val ryderId: String, | |
private val getFaresByIdUseCase: GetFaresByIdUseCase, | |
) : ViewModel(), ContainerHost<FareListState, FareListEffect> { | |
override val container: Container<FareListState, FareListEffect> = container( | |
initialState = FareListState(), | |
buildSettings = { | |
this.exceptionHandler = | |
this@FareListViewModel.exceptionHandler.asCoroutineExceptionHandler() | |
}, | |
) { | |
fetchFares() | |
} | |
private fun fetchFares() = intent { | |
reduce { state.copy(status = ScreenContentStatus.Loading) } | |
executeUseCase { getFaresByIdUseCase(ryderId).asPresentation() } | |
.onSuccess { fares -> | |
reduce { | |
state.copy( | |
status = ScreenContentStatus.Success, | |
fares = fares | |
) | |
} | |
} | |
.onFailure { | |
reduce { state.copy(status = ScreenContentStatus.Failure) } | |
postSideEffect(FareListEffect.ShowGeneralNetworkError) | |
} | |
} | |
fun onFareClick(fare: FareModel) = intent { | |
postSideEffect(FareListEffect.GoToConfirmation(ryderId, fare)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment