diff --git a/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeDao.kt b/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeDao.kt index 0e1d10e..f836c52 100644 --- a/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeDao.kt +++ b/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeDao.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeStorageImpl.kt b/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeStorageImpl.kt index e01bbda..9bddb44 100644 --- a/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeStorageImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealie/data/recipes/db/RecipeStorageImpl.kt @@ -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) } diff --git a/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInfoFragment.kt b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInfoFragment.kt index 9b40df4..fecf86b 100644 --- a/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInfoFragment.kt +++ b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInfoFragment.kt @@ -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) } } diff --git a/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientViewHolder.kt b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientViewHolder.kt new file mode 100644 index 0000000..9d4646f --- /dev/null +++ b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientViewHolder.kt @@ -0,0 +1,2 @@ +package gq.kirmanak.mealie.ui.recipes.info + diff --git a/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientsAdapter.kt b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientsAdapter.kt new file mode 100644 index 0000000..3300ce2 --- /dev/null +++ b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeIngredientsAdapter.kt @@ -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(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() { + 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)) + } +} \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInstructionsAdapter.kt b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInstructionsAdapter.kt new file mode 100644 index 0000000..e60f3c2 --- /dev/null +++ b/app/src/main/java/gq/kirmanak/mealie/ui/recipes/info/RecipeInstructionsAdapter.kt @@ -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( + RecipeInstructionDiffCallback + ) { + + private object RecipeInstructionDiffCallback : + DiffUtil.ItemCallback() { + 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) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_recipe_info.xml b/app/src/main/res/layout/fragment_recipe_info.xml index 1676719..e4e1180 100644 --- a/app/src/main/res/layout/fragment_recipe_info.xml +++ b/app/src/main/res/layout/fragment_recipe_info.xml @@ -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"