Fix opening recipe info without ingredients/instructions

This commit is contained in:
Kirill Kamakin
2021-11-17 23:21:37 +03:00
parent a67a3a5de0
commit d64a49b31f
7 changed files with 117 additions and 0 deletions

View File

@@ -67,4 +67,10 @@ interface RecipeDao {
@Transaction @Transaction
@Query("SELECT * FROM recipe JOIN recipe_summaries ON recipe.remote_id = recipe_summaries.remote_id JOIN recipe_ingredient ON recipe_ingredient.recipe_id = recipe.remote_id JOIN recipe_instruction ON recipe_instruction.recipe_id = recipe.remote_id WHERE recipe.remote_id = :recipeId") @Query("SELECT * FROM recipe JOIN recipe_summaries ON recipe.remote_id = recipe_summaries.remote_id JOIN recipe_ingredient ON recipe_ingredient.recipe_id = recipe.remote_id JOIN recipe_instruction ON recipe_instruction.recipe_id = recipe.remote_id WHERE recipe.remote_id = :recipeId")
suspend fun queryFullRecipeInfo(recipeId: Long): FullRecipeInfo? suspend fun queryFullRecipeInfo(recipeId: Long): FullRecipeInfo?
@Query("DELETE FROM recipe_ingredient WHERE recipe_id = :recipeId")
suspend fun deleteRecipeIngredients(recipeId: Long)
@Query("DELETE FROM recipe_instruction WHERE recipe_id = :recipeId")
suspend fun deleteRecipeInstructions(recipeId: Long)
} }

View File

@@ -122,10 +122,14 @@ class RecipeStorageImpl @Inject constructor(
Timber.v("saveRecipeInfo() called with: recipe = $recipe") Timber.v("saveRecipeInfo() called with: recipe = $recipe")
db.withTransaction { db.withTransaction {
recipeDao.insertRecipe(recipe.toRecipeEntity()) recipeDao.insertRecipe(recipe.toRecipeEntity())
recipeDao.deleteRecipeIngredients(recipe.remoteId)
val ingredients = recipe.recipeIngredients.map { val ingredients = recipe.recipeIngredients.map {
it.toRecipeIngredientEntity(recipe.remoteId) it.toRecipeIngredientEntity(recipe.remoteId)
} }
recipeDao.insertRecipeIngredients(ingredients) recipeDao.insertRecipeIngredients(ingredients)
recipeDao.deleteRecipeInstructions(recipe.remoteId)
val instructions = recipe.recipeInstructions.map { val instructions = recipe.recipeInstructions.map {
it.toRecipeInstructionEntity(recipe.remoteId) it.toRecipeInstructionEntity(recipe.remoteId)
} }

View File

@@ -9,6 +9,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealie.databinding.FragmentRecipeInfoBinding import gq.kirmanak.mealie.databinding.FragmentRecipeInfoBinding
import gq.kirmanak.mealie.ui.auth.AuthenticationViewModel import gq.kirmanak.mealie.ui.auth.AuthenticationViewModel
@@ -43,6 +44,16 @@ class RecipeInfoFragment : Fragment() {
viewModel.recipeInfo.observe(viewLifecycleOwner) { viewModel.recipeInfo.observe(viewLifecycleOwner) {
binding.title.text = it.recipeSummaryEntity.name binding.title.text = it.recipeSummaryEntity.name
binding.description.text = it.recipeSummaryEntity.description binding.description.text = it.recipeSummaryEntity.description
val recipeIngredientsAdapter = RecipeIngredientsAdapter()
binding.ingredientsList.adapter = recipeIngredientsAdapter
binding.ingredientsList.layoutManager = LinearLayoutManager(requireContext())
recipeIngredientsAdapter.submitList(it.recipeIngredients)
val recipeInstructionsAdapter = RecipeInstructionsAdapter()
binding.instructionsList.adapter = recipeInstructionsAdapter
binding.instructionsList.layoutManager = LinearLayoutManager(requireContext())
recipeInstructionsAdapter.submitList(it.recipeInstructions)
} }
} }

View File

@@ -0,0 +1,2 @@
package gq.kirmanak.mealie.ui.recipes.info

View File

@@ -0,0 +1,45 @@
package gq.kirmanak.mealie.ui.recipes.info
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeIngredientEntity
import gq.kirmanak.mealie.databinding.ViewHolderIngredientBinding
import gq.kirmanak.mealie.ui.recipes.info.RecipeIngredientsAdapter.RecipeIngredientViewHolder
class RecipeIngredientsAdapter() :
ListAdapter<RecipeIngredientEntity, RecipeIngredientViewHolder>(RecipeIngredientDiffCallback) {
class RecipeIngredientViewHolder(
private val binding: ViewHolderIngredientBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: RecipeIngredientEntity) {
binding.checkBox.text = item.note
}
}
private object RecipeIngredientDiffCallback : DiffUtil.ItemCallback<RecipeIngredientEntity>() {
override fun areItemsTheSame(
oldItem: RecipeIngredientEntity,
newItem: RecipeIngredientEntity
): Boolean = oldItem.localId == newItem.localId
override fun areContentsTheSame(
oldItem: RecipeIngredientEntity,
newItem: RecipeIngredientEntity
): Boolean = oldItem == newItem
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeIngredientViewHolder {
val inflater = LayoutInflater.from(parent.context)
return RecipeIngredientViewHolder(
ViewHolderIngredientBinding.inflate(inflater, parent, false)
)
}
override fun onBindViewHolder(holder: RecipeIngredientViewHolder, position: Int) {
holder.bind(getItem(position))
}
}

View File

@@ -0,0 +1,47 @@
package gq.kirmanak.mealie.ui.recipes.info
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import gq.kirmanak.mealie.data.recipes.db.entity.RecipeInstructionEntity
import gq.kirmanak.mealie.databinding.ViewHolderInstructionBinding
class RecipeInstructionsAdapter :
ListAdapter<RecipeInstructionEntity, RecipeInstructionsAdapter.RecipeInstructionViewHolder>(
RecipeInstructionDiffCallback
) {
private object RecipeInstructionDiffCallback :
DiffUtil.ItemCallback<RecipeInstructionEntity>() {
override fun areItemsTheSame(
oldItem: RecipeInstructionEntity,
newItem: RecipeInstructionEntity
): Boolean = oldItem.localId == newItem.localId
override fun areContentsTheSame(
oldItem: RecipeInstructionEntity,
newItem: RecipeInstructionEntity
): Boolean = oldItem == newItem
}
class RecipeInstructionViewHolder(private val binding: ViewHolderInstructionBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: RecipeInstructionEntity, position: Int) {
binding.step.text = "Step: ${position + 1}"
binding.instruction.text = item.text
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeInstructionViewHolder {
val inflater = LayoutInflater.from(parent.context)
return RecipeInstructionViewHolder(
ViewHolderInstructionBinding.inflate(inflater, parent, false)
)
}
override fun onBindViewHolder(holder: RecipeInstructionViewHolder, position: Int) {
holder.bind(getItem(position), position)
}
}

View File

@@ -59,6 +59,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_margin="@dimen/margin_small" android:layout_margin="@dimen/margin_small"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:overScrollMode="never"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ingredients_header" app:layout_constraintTop_toBottomOf="@+id/ingredients_header"
tools:itemCount="3" tools:itemCount="3"
@@ -79,6 +80,7 @@
android:id="@+id/instructions_list" android:id="@+id/instructions_list"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_margin="@dimen/margin_small" android:layout_margin="@dimen/margin_small"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"