Replace add/remove with update
This commit is contained in:
@@ -69,25 +69,25 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
ServerVersion.V1 -> v1Source.requestUserInfo().favoriteRecipes
|
||||
}
|
||||
|
||||
override suspend fun removeFavoriteRecipe(recipeSlug: String) = when (getVersion()) {
|
||||
override suspend fun updateIsRecipeFavorite(
|
||||
recipeSlug: String,
|
||||
isFavorite: Boolean
|
||||
) = when (getVersion()) {
|
||||
ServerVersion.V0 -> {
|
||||
val userId = v0Source.requestUserInfo().id
|
||||
v0Source.removeFavoriteRecipe(userId, recipeSlug)
|
||||
if (isFavorite) {
|
||||
v0Source.addFavoriteRecipe(userId, recipeSlug)
|
||||
} else {
|
||||
v0Source.removeFavoriteRecipe(userId, recipeSlug)
|
||||
}
|
||||
}
|
||||
ServerVersion.V1 -> {
|
||||
val userId = v1Source.requestUserInfo().id
|
||||
v1Source.removeFavoriteRecipe(userId, recipeSlug)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun addFavoriteRecipe(recipeSlug: String) = when (getVersion()) {
|
||||
ServerVersion.V0 -> {
|
||||
val userId = v0Source.requestUserInfo().id
|
||||
v0Source.addFavoriteRecipe(userId, recipeSlug)
|
||||
}
|
||||
ServerVersion.V1 -> {
|
||||
val userId = v1Source.requestUserInfo().id
|
||||
v1Source.addFavoriteRecipe(userId, recipeSlug)
|
||||
if (isFavorite) {
|
||||
v1Source.addFavoriteRecipe(userId, recipeSlug)
|
||||
} else {
|
||||
v1Source.removeFavoriteRecipe(userId, recipeSlug)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,5 @@ interface RecipeRepo {
|
||||
|
||||
suspend fun refreshRecipes()
|
||||
|
||||
suspend fun removeFavoriteRecipe(recipeSlug: String)
|
||||
|
||||
suspend fun addFavoriteRecipe(recipeSlug: String)
|
||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean)
|
||||
}
|
||||
@@ -72,21 +72,12 @@ class RecipeRepoImpl @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun removeFavoriteRecipe(recipeSlug: String) {
|
||||
logger.v { "removeFavoriteRecipe() called with: recipeSlug = $recipeSlug" }
|
||||
override suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean) {
|
||||
logger.v { "updateIsRecipeFavorite() called with: recipeSlug = $recipeSlug, isFavorite = $isFavorite" }
|
||||
runCatchingExceptCancel {
|
||||
dataSource.removeFavoriteRecipe(recipeSlug)
|
||||
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't remove a favorite recipe" }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun addFavoriteRecipe(recipeSlug: String) {
|
||||
logger.v { "addFavoriteRecipe() called with: recipeSlug = $recipeSlug" }
|
||||
runCatchingExceptCancel {
|
||||
dataSource.addFavoriteRecipe(recipeSlug)
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't add a favorite recipe" }
|
||||
logger.e(it) { "Can't update recipe's is favorite status" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,5 @@ interface RecipeDataSource {
|
||||
|
||||
suspend fun getFavoriteRecipes(): List<String>
|
||||
|
||||
suspend fun removeFavoriteRecipe(recipeSlug: String)
|
||||
|
||||
suspend fun addFavoriteRecipe(recipeSlug: String)
|
||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean)
|
||||
}
|
||||
@@ -44,11 +44,10 @@ class RecipesListViewModel @Inject constructor(
|
||||
fun onFavoriteIconClick(recipeSummaryEntity: RecipeSummaryEntity) {
|
||||
logger.v { "onFavoriteIconClick() called with: recipeSummaryEntity = $recipeSummaryEntity" }
|
||||
viewModelScope.launch {
|
||||
if (recipeSummaryEntity.isFavorite) {
|
||||
recipeRepo.removeFavoriteRecipe(recipeSummaryEntity.slug)
|
||||
} else {
|
||||
recipeRepo.addFavoriteRecipe(recipeSummaryEntity.slug)
|
||||
}
|
||||
recipeRepo.updateIsRecipeFavorite(
|
||||
recipeSlug = recipeSummaryEntity.slug,
|
||||
isFavorite = recipeSummaryEntity.isFavorite.not(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user