Initialize recipe removal feature

This commit is contained in:
Kirill Kamakin
2022-12-14 22:49:52 +01:00
parent 8e45a30480
commit 9ab86e7be3
16 changed files with 99 additions and 3 deletions

View File

@@ -90,4 +90,9 @@ class MealieDataSourceWrapper @Inject constructor(
}
}
}
override suspend fun deleteRecipe(recipeSlug: String) = when (getVersion()) {
ServerVersion.V0 -> v0Source.deleteRecipe(recipeSlug)
ServerVersion.V1 -> v1Source.deleteRecipe(recipeSlug)
}
}

View File

@@ -19,4 +19,6 @@ interface RecipeRepo {
suspend fun refreshRecipes()
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean): Result<Unit>
suspend fun deleteRecipe(recipeSlug: String): Result<Unit>
}

View File

@@ -83,6 +83,14 @@ class RecipeRepoImpl @Inject constructor(
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
}.onFailure {
logger.e(it) { "Can't delete recipe" }
}
companion object {
private const val LOAD_PAGE_SIZE = 50
private const val INITIAL_LOAD_PAGE_SIZE = LOAD_PAGE_SIZE * 3

View File

@@ -8,4 +8,6 @@ interface RecipeDataSource {
suspend fun getFavoriteRecipes(): List<String>
suspend fun updateIsRecipeFavorite(recipeSlug: String, isFavorite: Boolean)
suspend fun deleteRecipe(recipeSlug: String)
}

View File

@@ -47,6 +47,10 @@ class RecipeViewHolder @AssistedInject constructor(
override val recipeSummaryEntity: RecipeSummaryEntity
) : ClickEvent()
data class DeleteClick(
override val recipeSummaryEntity: RecipeSummaryEntity
) : ClickEvent()
}
private val loadingPlaceholder by lazy {
@@ -62,6 +66,7 @@ class RecipeViewHolder @AssistedInject constructor(
logger.d { "bind: item clicked $entity" }
clickListener(ClickEvent.RecipeClick(entity))
}
binding.favoriteIcon.isVisible = showFavoriteIcon
binding.favoriteIcon.setOnClickListener {
clickListener(ClickEvent.FavoriteClick(entity))
@@ -80,6 +85,10 @@ class RecipeViewHolder @AssistedInject constructor(
R.string.view_holder_recipe_non_favorite_content_description
}
)
binding.deleteIcon.setOnClickListener {
clickListener(ClickEvent.DeleteClick(item))
}
}
}
}

View File

@@ -100,6 +100,9 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
is RecipeViewHolder.ClickEvent.RecipeClick -> {
onRecipeClicked(it.recipeSummaryEntity)
}
is RecipeViewHolder.ClickEvent.DeleteClick -> {
onDeleteClick(it)
}
}
}
@@ -139,6 +142,16 @@ class RecipesListFragment : Fragment(R.layout.fragment_recipes_list) {
}
}
private fun onDeleteClick(event: RecipeViewHolder.ClickEvent) {
logger.v { "onDeleteClick() called with: event = $event" }
viewModel.onDeleteConfirm(event.recipeSummaryEntity).observe(viewLifecycleOwner) {
logger.d { "onDeleteClick: result is $it" }
if (it.isFailure) {
showLongToast(R.string.fragment_recipes_delete_recipe_failed)
}
}
}
private fun onFavoriteClick(event: RecipeViewHolder.ClickEvent) {
logger.v { "onFavoriteClick() called with: event = $event" }
viewModel.onFavoriteIconClick(event.recipeSummaryEntity).observe(viewLifecycleOwner) {

View File

@@ -49,4 +49,9 @@ class RecipesListViewModel @Inject constructor(
isFavorite = recipeSummaryEntity.isFavorite.not(),
).also { emit(it) }
}
fun onDeleteConfirm(recipeSummaryEntity: RecipeSummaryEntity) = liveData {
logger.v { "onDeleteConfirm() called with: recipeSummaryEntity = $recipeSummaryEntity" }
recipeRepo.deleteRecipe(recipeSummaryEntity.slug).also { emit(it) }
}
}