Show Toast when recipe favorite status is updated

This commit is contained in:
Kirill Kamakin
2023-04-07 14:14:25 +02:00
parent b78d30790f
commit 9b03cfcbb4
5 changed files with 16 additions and 2 deletions

View File

@@ -18,7 +18,7 @@ interface RecipeRepo {
suspend fun refreshRecipes()
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Unit>
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Boolean>
suspend fun deleteRecipe(entity: RecipeSummaryEntity): Result<Unit>
}

View File

@@ -75,12 +75,13 @@ class RecipeRepoImpl @Inject constructor(
override suspend fun updateIsRecipeFavorite(
recipeSlug: String,
isFavorite: Boolean,
): Result<Unit> = runCatchingExceptCancel {
): Result<Boolean> = runCatchingExceptCancel {
logger.v { "updateIsRecipeFavorite() called with: recipeSlug = $recipeSlug, isFavorite = $isFavorite" }
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
val favorites = dataSource.getFavoriteRecipes()
storage.updateFavoriteRecipes(favorites)
pagingSourceFactory.invalidate()
favorites.contains(recipeSlug)
}.onFailure {
logger.e(it) { "Can't update recipe's is favorite status" }
}

View File

@@ -177,6 +177,15 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
logger.d { "onFavoriteClick: result is $it" }
if (it.isFailure) {
showLongToast(R.string.fragment_recipes_favorite_update_failed)
} else {
val name = event.recipeSummaryEntity.name
val isFavorite = it.getOrThrow()
val message = if (isFavorite) {
getString(R.string.fragment_recipes_favorite_added, name)
} else {
getString(R.string.fragment_recipes_favorite_removed, name)
}
showLongToast(message)
}
}
}