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