Remove recipe from local db when deleted
This commit is contained in:
@@ -20,5 +20,5 @@ interface RecipeRepo {
|
||||
|
||||
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Unit>
|
||||
|
||||
suspend fun deleteRecipe(recipeSlug: String): Result<Unit>
|
||||
suspend fun deleteRecipe(entity: RecipeSummaryEntity): Result<Unit>
|
||||
}
|
||||
@@ -19,4 +19,6 @@ interface RecipeStorage {
|
||||
suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity?
|
||||
|
||||
suspend fun updateFavoriteRecipes(favorites: List<String>)
|
||||
|
||||
suspend fun deleteRecipe(entity: RecipeSummaryEntity)
|
||||
}
|
||||
@@ -80,4 +80,9 @@ class RecipeStorageImpl @Inject constructor(
|
||||
recipeDao.setNonFavorite(favorites)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteRecipe(entity: RecipeSummaryEntity) {
|
||||
logger.v { "deleteRecipeBySlug() called with: entity = $entity" }
|
||||
recipeDao.deleteRecipe(entity)
|
||||
}
|
||||
}
|
||||
@@ -78,15 +78,20 @@ class RecipeRepoImpl @Inject constructor(
|
||||
): Result<Unit> = runCatchingExceptCancel {
|
||||
logger.v { "updateIsRecipeFavorite() called with: recipeSlug = $recipeSlug, isFavorite = $isFavorite" }
|
||||
dataSource.updateIsRecipeFavorite(recipeSlug, isFavorite)
|
||||
mediator.onFavoritesChange()
|
||||
val favorites = dataSource.getFavoriteRecipes()
|
||||
storage.updateFavoriteRecipes(favorites)
|
||||
pagingSourceFactory.invalidate()
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't update recipe's is favorite status" }
|
||||
}
|
||||
|
||||
override suspend fun deleteRecipe(recipeSlug: String): Result<Unit> = runCatchingExceptCancel {
|
||||
logger.v { "deleteRecipe() called with: recipeSlug = $recipeSlug" }
|
||||
dataSource.deleteRecipe(recipeSlug)
|
||||
// TODO update local db
|
||||
override suspend fun deleteRecipe(
|
||||
entity: RecipeSummaryEntity
|
||||
): Result<Unit> = runCatchingExceptCancel {
|
||||
logger.v { "deleteRecipe() called with: entity = $entity" }
|
||||
dataSource.deleteRecipe(entity.slug)
|
||||
storage.deleteRecipe(entity)
|
||||
pagingSourceFactory.invalidate()
|
||||
}.onFailure {
|
||||
logger.e(it) { "Can't delete recipe" }
|
||||
}
|
||||
|
||||
@@ -83,10 +83,4 @@ class RecipesRemoteMediator @Inject constructor(
|
||||
recipes.size
|
||||
}
|
||||
|
||||
suspend fun onFavoritesChange() {
|
||||
logger.v { "onFavoritesChange() called" }
|
||||
val favorites = network.getFavoriteRecipes()
|
||||
storage.updateFavoriteRecipes(favorites)
|
||||
pagingSourceFactory.invalidate()
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,7 @@ class RecipesListViewModel @Inject constructor(
|
||||
fun onDeleteConfirm(recipeSummaryEntity: RecipeSummaryEntity) {
|
||||
logger.v { "onDeleteConfirm() called with: recipeSummaryEntity = $recipeSummaryEntity" }
|
||||
viewModelScope.launch {
|
||||
val result = recipeRepo.deleteRecipe(recipeSummaryEntity.slug)
|
||||
val result = recipeRepo.deleteRecipe(recipeSummaryEntity)
|
||||
_deleteRecipeResult.emit(result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user