Skip to content

Instantly share code, notes, and snippets.

@MkhytarMkhoian
Last active May 16, 2024 12:34
Show Gist options
  • Save MkhytarMkhoian/888a6ae1e260c6a30f6dc01ab5689052 to your computer and use it in GitHub Desktop.
Save MkhytarMkhoian/888a6ae1e260c6a30f6dc01ab5689052 to your computer and use it in GitHub Desktop.
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