Last active
November 9, 2023 23:41
-
-
Save AzizaHelmy/2a19bcc926e8a3f5b0eab16c50b8bea1 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 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