Show error message when favorite update fails
This commit is contained in:
@@ -18,5 +18,5 @@ interface RecipeRepo {
|
|||||||
|
|
||||||
suspend fun refreshRecipes()
|
suspend fun refreshRecipes()
|
||||||
|
|
||||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean)
|
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Unit>
|
||||||
}
|
}
|
||||||
@@ -72,15 +72,16 @@ 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" }
|
logger.v { "updateIsRecipeFavorite() called with: recipeSlug = $recipeSlug, isFavorite = $isFavorite" }
|
||||||
runCatchingExceptCancel {
|
|
||||||
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
||||||
mediator.onFavoritesChange()
|
mediator.onFavoritesChange()
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
logger.e(it) { "Can't update recipe's is favorite status" }
|
logger.e(it) { "Can't update recipe's is favorite status" }
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val LOAD_PAGE_SIZE = 50
|
private const val LOAD_PAGE_SIZE = 50
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
|
|||||||
val recipesAdapter = recipePagingAdapterFactory.build {
|
val recipesAdapter = recipePagingAdapterFactory.build {
|
||||||
when (it) {
|
when (it) {
|
||||||
is RecipeViewHolder.ClickEvent.FavoriteClick -> {
|
is RecipeViewHolder.ClickEvent.FavoriteClick -> {
|
||||||
viewModel.onFavoriteIconClick(it.recipeSummaryEntity)
|
onFavoriteClick(it)
|
||||||
}
|
}
|
||||||
is RecipeViewHolder.ClickEvent.RecipeClick -> {
|
is RecipeViewHolder.ClickEvent.RecipeClick -> {
|
||||||
onRecipeClicked(it.recipeSummaryEntity)
|
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) {
|
private fun onLoadFailure(error: Throwable) {
|
||||||
logger.w(error) { "onLoadFailure() called" }
|
logger.w(error) { "onLoadFailure() called" }
|
||||||
val reason = error.toLoadErrorReasonText()?.let { getString(it) }
|
val reason = error.toLoadErrorReasonText()?.let { getString(it) }
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import gq.kirmanak.mealient.extensions.valueUpdatesOnly
|
|||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
import kotlinx.coroutines.flow.onEach
|
import kotlinx.coroutines.flow.onEach
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
@@ -42,14 +41,11 @@ class RecipesListViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO hide favourite icons if not authorized
|
// TODO hide favourite icons if not authorized
|
||||||
// TODO show error message when can't update favourite status
|
fun onFavoriteIconClick(recipeSummaryEntity: RecipeSummaryEntity) = liveData {
|
||||||
fun onFavoriteIconClick(recipeSummaryEntity: RecipeSummaryEntity) {
|
|
||||||
logger.v { "onFavoriteIconClick() called with: recipeSummaryEntity = $recipeSummaryEntity" }
|
logger.v { "onFavoriteIconClick() called with: recipeSummaryEntity = $recipeSummaryEntity" }
|
||||||
viewModelScope.launch {
|
|
||||||
recipeRepo.updateIsRecipeFavorite(
|
recipeRepo.updateIsRecipeFavorite(
|
||||||
recipeSlug = recipeSummaryEntity.slug,
|
recipeSlug = recipeSummaryEntity.slug,
|
||||||
isFavorite = recipeSummaryEntity.isFavorite.not(),
|
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_unauthorized">unauthorized</string>
|
||||||
<string name="fragment_recipes_load_failure_toast_unexpected_response">unexpected response</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_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="menu_navigation_drawer_change_url">Change URL</string>
|
||||||
<string name="search_recipes_hint">Search recipes</string>
|
<string name="search_recipes_hint">Search recipes</string>
|
||||||
<string name="menu_navigation_drawer_header" translatable="false">@string/app_name</string>
|
<string name="menu_navigation_drawer_header" translatable="false">@string/app_name</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user