Implement hiding ingredients and instructions

This commit is contained in:
Kirill Kamakin
2021-12-27 19:41:56 +03:00
parent 6232af2295
commit c8019e9c85
6 changed files with 99 additions and 59 deletions

View File

@@ -5,7 +5,7 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity import androidx.core.view.isVisible
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs import androidx.navigation.fragment.navArgs
import by.kirich1409.viewbindingdelegate.viewBinding import by.kirich1409.viewbindingdelegate.viewBinding
@@ -15,50 +15,62 @@ import dagger.hilt.android.AndroidEntryPoint
import gq.kirmanak.mealient.R import gq.kirmanak.mealient.R
import gq.kirmanak.mealient.databinding.FragmentRecipeInfoBinding import gq.kirmanak.mealient.databinding.FragmentRecipeInfoBinding
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class RecipeInfoFragment : BottomSheetDialogFragment() { class RecipeInfoFragment : BottomSheetDialogFragment() {
private val binding by viewBinding(FragmentRecipeInfoBinding::bind) private val binding by viewBinding(FragmentRecipeInfoBinding::bind)
private val arguments by navArgs<RecipeInfoFragmentArgs>() private val arguments by navArgs<RecipeInfoFragmentArgs>()
private val viewModel by viewModels<RecipeInfoViewModel>() private val viewModel by viewModels<RecipeInfoViewModel>()
override fun onCreateView( @Inject
inflater: LayoutInflater, lateinit var ingredientsAdapter: RecipeIngredientsAdapter
container: ViewGroup?,
savedInstanceState: Bundle? @Inject
): View { lateinit var instructionsAdapter: RecipeInstructionsAdapter
Timber.v("onCreateView() called with: inflater = $inflater, container = $container, savedInstanceState = $savedInstanceState")
return FragmentRecipeInfoBinding.inflate(inflater, container, false).root override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
Timber.v("onCreateView() called")
return FragmentRecipeInfoBinding.inflate(inflater, container, false).root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Timber.v("onViewCreated() called")
binding.ingredientsList.adapter = ingredientsAdapter
binding.instructionsList.adapter = instructionsAdapter
viewModel.loadRecipeImage(binding.image, arguments.recipeSlug)
viewModel.loadRecipeInfo(arguments.recipeId, arguments.recipeSlug)
viewModel.recipeInfo.observe(viewLifecycleOwner) {
Timber.d("onViewCreated: full info $it")
binding.title.text = it.recipeSummaryEntity.name
binding.description.text = it.recipeSummaryEntity.description
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { viewModel.listsVisibility.observe(viewLifecycleOwner) {
super.onViewCreated(view, savedInstanceState) Timber.d("onViewCreated: lists visibility $it")
Timber.v("onViewCreated() called with: view = $view, savedInstanceState = $savedInstanceState") binding.ingredientsHolder.isVisible = it.areIngredientsVisible
binding.instructionsGroup.isVisible = it.areInstructionsVisible
binding.ingredientsList.adapter = viewModel.recipeIngredientsAdapter
binding.instructionsList.adapter = viewModel.recipeInstructionsAdapter
viewModel.loadRecipeImage(binding.image, arguments.recipeSlug)
viewModel.loadRecipeInfo(arguments.recipeId, arguments.recipeSlug)
viewModel.recipeInfo.observe(viewLifecycleOwner) {
Timber.d("onViewCreated: full info $it")
binding.title.text = it.recipeSummaryEntity.name
binding.description.text = it.recipeSummaryEntity.description
}
(requireActivity() as? AppCompatActivity)?.supportActionBar?.title = null
} }
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =
BottomSheetDialog(requireContext(), R.style.NoShapeBottomSheetDialog) BottomSheetDialog(requireContext(), R.style.NoShapeBottomSheetDialog)
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
Timber.v("onDestroyView() called") Timber.v("onDestroyView() called")
// Prevent RV leaking through mObservers list in adapter // Prevent RV leaking through mObservers list in adapter
with(binding) { with(binding) {
ingredientsList.adapter = null ingredientsList.adapter = null
instructionsList.adapter = null instructionsList.adapter = null
}
} }
}
} }

View File

@@ -0,0 +1,6 @@
package gq.kirmanak.mealient.ui.recipes.info
data class RecipeInfoListsVisibility(
val areIngredientsVisible: Boolean = false,
val areInstructionsVisible: Boolean = false,
)

View File

@@ -14,35 +14,44 @@ import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@HiltViewModel @HiltViewModel
class RecipeInfoViewModel @Inject constructor( class RecipeInfoViewModel
@Inject
constructor(
private val recipeRepo: RecipeRepo, private val recipeRepo: RecipeRepo,
private val recipeImageLoader: RecipeImageLoader private val recipeImageLoader: RecipeImageLoader,
private val recipeIngredientsAdapter: RecipeIngredientsAdapter,
private val recipeInstructionsAdapter: RecipeInstructionsAdapter,
) : ViewModel() { ) : ViewModel() {
private val _recipeInfo = MutableLiveData<FullRecipeInfo>() private val _recipeInfo = MutableLiveData<FullRecipeInfo>()
val recipeInfo: LiveData<FullRecipeInfo> = _recipeInfo val recipeInfo: LiveData<FullRecipeInfo> by ::_recipeInfo
val recipeIngredientsAdapter = RecipeIngredientsAdapter() private val _listsVisibility = MutableLiveData(RecipeInfoListsVisibility())
val recipeInstructionsAdapter = RecipeInstructionsAdapter() val listsVisibility: LiveData<RecipeInfoListsVisibility> by ::_listsVisibility
fun loadRecipeImage(view: ImageView, recipeSlug: String) { fun loadRecipeImage(view: ImageView, recipeSlug: String) {
Timber.v("loadRecipeImage() called with: view = $view, recipeSlug = $recipeSlug") Timber.v("loadRecipeImage() called with: view = $view, recipeSlug = $recipeSlug")
viewModelScope.launch { viewModelScope.launch { recipeImageLoader.loadRecipeImage(view, recipeSlug) }
recipeImageLoader.loadRecipeImage(view, recipeSlug)
}
} }
fun loadRecipeInfo(recipeId: Long, recipeSlug: String) { fun loadRecipeInfo(recipeId: Long, recipeSlug: String) {
Timber.v("loadRecipeInfo() called with: recipeId = $recipeId, recipeSlug = $recipeSlug") Timber.v("loadRecipeInfo() called with: recipeId = $recipeId, recipeSlug = $recipeSlug")
_listsVisibility.value = RecipeInfoListsVisibility()
recipeIngredientsAdapter.submitList(null)
recipeInstructionsAdapter.submitList(null)
viewModelScope.launch { viewModelScope.launch {
runCatching { runCatching { recipeRepo.loadRecipeInfo(recipeId, recipeSlug) }
recipeRepo.loadRecipeInfo(recipeId, recipeSlug) .onSuccess {
}.onSuccess { Timber.d("loadRecipeInfo: received recipe info = $it")
Timber.d("loadRecipeInfo: received recipe info = $it") _recipeInfo.value = it
_recipeInfo.value = it recipeIngredientsAdapter.submitList(it.recipeIngredients)
recipeIngredientsAdapter.submitList(it.recipeIngredients) recipeInstructionsAdapter.submitList(it.recipeInstructions)
recipeInstructionsAdapter.submitList(it.recipeInstructions) _listsVisibility.value =
}.onFailure { RecipeInfoListsVisibility(
Timber.e(it, "loadRecipeInfo: can't load recipe info") areIngredientsVisible = it.recipeIngredients.isNotEmpty(),
} areInstructionsVisible = it.recipeInstructions.isNotEmpty()
)
}
.onFailure { Timber.e(it, "loadRecipeInfo: can't load recipe info") }
} }
} }
} }

View File

@@ -9,8 +9,11 @@ import gq.kirmanak.mealient.data.recipes.db.entity.RecipeIngredientEntity
import gq.kirmanak.mealient.databinding.ViewHolderIngredientBinding import gq.kirmanak.mealient.databinding.ViewHolderIngredientBinding
import gq.kirmanak.mealient.ui.recipes.info.RecipeIngredientsAdapter.RecipeIngredientViewHolder import gq.kirmanak.mealient.ui.recipes.info.RecipeIngredientsAdapter.RecipeIngredientViewHolder
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
class RecipeIngredientsAdapter : @Singleton
class RecipeIngredientsAdapter @Inject constructor() :
ListAdapter<RecipeIngredientEntity, RecipeIngredientViewHolder>(RecipeIngredientDiffCallback) { ListAdapter<RecipeIngredientEntity, RecipeIngredientViewHolder>(RecipeIngredientDiffCallback) {
class RecipeIngredientViewHolder( class RecipeIngredientViewHolder(

View File

@@ -10,8 +10,11 @@ import gq.kirmanak.mealient.data.recipes.db.entity.RecipeInstructionEntity
import gq.kirmanak.mealient.databinding.ViewHolderInstructionBinding import gq.kirmanak.mealient.databinding.ViewHolderInstructionBinding
import gq.kirmanak.mealient.ui.recipes.info.RecipeInstructionsAdapter.RecipeInstructionViewHolder import gq.kirmanak.mealient.ui.recipes.info.RecipeInstructionsAdapter.RecipeInstructionViewHolder
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
class RecipeInstructionsAdapter : @Singleton
class RecipeInstructionsAdapter @Inject constructor() :
ListAdapter<RecipeInstructionEntity, RecipeInstructionViewHolder>(RecipeInstructionDiffCallback) { ListAdapter<RecipeInstructionEntity, RecipeInstructionViewHolder>(RecipeInstructionDiffCallback) {
private object RecipeInstructionDiffCallback : private object RecipeInstructionDiffCallback :

View File

@@ -109,6 +109,13 @@
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
<androidx.constraintlayout.widget.Group
android:id="@+id/instructions_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="instructions_header,instructions_list" />
<TextView <TextView
android:id="@+id/instructions_header" android:id="@+id/instructions_header"
android:layout_width="0dp" android:layout_width="0dp"