Show error message when favorite update fails
This commit is contained in:
@@ -18,5 +18,5 @@ interface RecipeRepo {
|
||||
|
||||
suspend fun refreshRecipes()
|
||||
|
||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean)
|
||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Unit>
|
||||
}
|
||||
@@ -72,14 +72,15 @@ class RecipeRepoImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean) {
|
||||
override suspend fun updateIsRecipeFavorite(
|
||||
recipeSlug: String,
|
||||
isFavorite: Boolean,
|
||||
): Result<Unit> = runCatchingExceptCancel {
|
||||
logger.v { "updateIsRecipeFavorite() called with: recipeSlug = $recipeSlug, isFavorite = $isFavorite" }
|
||||
runCatchingExceptCancel {
|
||||
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
||||
mediator.onFavoritesChange()
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't update recipe's is favorite status" }
|
||||
}
|
||||
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
||||
mediator.onFavoritesChange()
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't update recipe's is favorite status" }
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -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) }
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,7 @@
|
||||
<string name="fragment_recipes_load_failure_toast_unauthorized">unauthorized</string>
|
||||
<string name="fragment_recipes_load_failure_toast_unexpected_response">unexpected response</string>
|
||||
<string name="fragment_recipes_load_failure_toast_no_connection">no connection</string>
|
||||
<string name="fragment_recipes_favorite_update_failed">Favorite status update failed</string>
|
||||
<string name="menu_navigation_drawer_change_url">Change URL</string>
|
||||
<string name="search_recipes_hint">Search recipes</string>
|
||||
<string name="menu_navigation_drawer_header" translatable="false">@string/app_name</string>
|
||||
|
||||
Reference in New Issue
Block a user