Fix opening recipe info without ingredients/instructions
This commit is contained in:
@@ -67,4 +67,10 @@ interface RecipeDao {
|
||||
@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")
|
||||
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)
|
||||
}
|
||||
@@ -122,10 +122,14 @@ class RecipeStorageImpl @Inject constructor(
|
||||
Timber.v("saveRecipeInfo() called with: recipe = $recipe")
|
||||
db.withTransaction {
|
||||
recipeDao.insertRecipe(recipe.toRecipeEntity())
|
||||
|
||||
recipeDao.deleteRecipeIngredients(recipe.remoteId)
|
||||
val ingredients = recipe.recipeIngredients.map {
|
||||
it.toRecipeIngredientEntity(recipe.remoteId)
|
||||
}
|
||||
recipeDao.insertRecipeIngredients(ingredients)
|
||||
|
||||
recipeDao.deleteRecipeInstructions(recipe.remoteId)
|
||||
val instructions = recipe.recipeInstructions.map {
|
||||
it.toRecipeInstructionEntity(recipe.remoteId)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import androidx.fragment.app.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.navigation.fragment.navArgs
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealie.databinding.FragmentRecipeInfoBinding
|
||||
import gq.kirmanak.mealie.ui.auth.AuthenticationViewModel
|
||||
@@ -43,6 +44,16 @@ class RecipeInfoFragment : Fragment() {
|
||||
viewModel.recipeInfo.observe(viewLifecycleOwner) {
|
||||
binding.title.text = it.recipeSummaryEntity.name
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
package gq.kirmanak.mealie.ui.recipes.info
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/margin_small"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:overScrollMode="never"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/ingredients_header"
|
||||
tools:itemCount="3"
|
||||
@@ -79,6 +80,7 @@
|
||||
android:id="@+id/instructions_list"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:overScrollMode="never"
|
||||
android:layout_margin="@dimen/margin_small"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
|
||||
Reference in New Issue
Block a user