Show error message when favorite update fails
This commit is contained in:
@@ -93,7 +93,7 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
|
||||
val recipesAdapter = recipePagingAdapterFactory.build {
|
||||
when (it) {
|
||||
is RecipeViewHolder.ClickEvent.FavoriteClick -> {
|
||||
viewModel.onFavoriteIconClick(it.recipeSummaryEntity)
|
||||
onFavoriteClick(it)
|
||||
}
|
||||
is RecipeViewHolder.ClickEvent.RecipeClick -> {
|
||||
onRecipeClicked(it.recipeSummaryEntity)
|
||||
@@ -137,6 +137,16 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun onFavoriteClick(event: RecipeViewHolder.ClickEvent) {
|
||||
logger.v { "onFavoriteClick() called with: event = $event" }
|
||||
viewModel.onFavoriteIconClick(event.recipeSummaryEntity).observe(viewLifecycleOwner) {
|
||||
logger.d { "onFavoriteClick: result is $it" }
|
||||
if (it.isFailure) {
|
||||
showLongToast(R.string.fragment_recipes_favorite_update_failed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onLoadFailure(error: Throwable) {
|
||||
logger.w(error) { "onLoadFailure() called" }
|
||||
val reason = error.toLoadErrorReasonText()?.let { getString(it) }
|
||||
|
||||
@@ -13,7 +13,6 @@ import gq.kirmanak.mealient.extensions.valueUpdatesOnly
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -42,14 +41,11 @@ class RecipesListViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
// TODO hide favourite icons if not authorized
|
||||
// TODO show error message when can't update favourite status
|
||||
fun onFavoriteIconClick(recipeSummaryEntity: RecipeSummaryEntity) {
|
||||
fun onFavoriteIconClick(recipeSummaryEntity: RecipeSummaryEntity) = liveData {
|
||||
logger.v { "onFavoriteIconClick() called with: recipeSummaryEntity = $recipeSummaryEntity" }
|
||||
viewModelScope.launch {
|
||||
recipeRepo.updateIsRecipeFavorite(
|
||||
recipeSlug = recipeSummaryEntity.slug,
|
||||
isFavorite = recipeSummaryEntity.isFavorite.not(),
|
||||
)
|
||||
}
|
||||
recipeRepo.updateIsRecipeFavorite(
|
||||
recipeSlug = recipeSummaryEntity.slug,
|
||||
isFavorite = recipeSummaryEntity.isFavorite.not(),
|
||||
).also { emit(it) }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user