Skip to content

Instantly share code, notes, and snippets.

@AzizaHelmy
Last active November 9, 2023 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AzizaHelmy/2a19bcc926e8a3f5b0eab16c50b8bea1 to your computer and use it in GitHub Desktop.
Save AzizaHelmy/2a19bcc926e8a3f5b0eab16c50b8bea1 to your computer and use it in GitHub Desktop.
class MealsScreenModel(
private val cuisineId: String,
private val manageRestaurant: IExploreRestaurantUseCase,
) : BaseScreenModel<MealsUiState, MealsUiEffect>(MealsUiState()), MealsInteractionListener,
MealInteractionListener {
init {
getData()
}
private fun getData() {
updateState { it.copy(isLoading = true, cuisineName = cuisineName) }
tryToExecute(
{ manageRestaurant.getMealsInCuisine(cuisineId) },
::onGetMealsSuccess,
::onError
)
}
private fun onError(errorState: ErrorState) {
updateState { it.copy(isLoading = false) }
when (errorState) {
is ErrorState.NoInternet -> {
updateState { it.copy(error = errorState) }
}
else -> {
updateState { it.copy(error = errorState) }
}
}
}
private fun onGetMealsSuccess(meals: Flow<PagingData<Meal>>) {
updateState { it.copy(meals = meals.toUIState(), isLoading = false) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment