Simplify RecipeInfoFragment code
This commit is contained in:
@@ -15,19 +15,15 @@ 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>()
|
||||||
|
private val ingredientsAdapter = RecipeIngredientsAdapter()
|
||||||
@Inject
|
private val instructionsAdapter = RecipeInstructionsAdapter()
|
||||||
lateinit var ingredientsAdapter: RecipeIngredientsAdapter
|
|
||||||
|
|
||||||
@Inject
|
|
||||||
lateinit var instructionsAdapter: RecipeInstructionsAdapter
|
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater,
|
inflater: LayoutInflater,
|
||||||
@@ -42,22 +38,27 @@ class RecipeInfoFragment : BottomSheetDialogFragment() {
|
|||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
Timber.v("onViewCreated() called")
|
Timber.v("onViewCreated() called")
|
||||||
|
|
||||||
binding.ingredientsList.adapter = ingredientsAdapter
|
with(binding) {
|
||||||
binding.instructionsList.adapter = instructionsAdapter
|
ingredientsList.adapter = ingredientsAdapter
|
||||||
|
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
viewModel.listsVisibility.observe(viewLifecycleOwner) {
|
with(viewModel) {
|
||||||
Timber.d("onViewCreated: lists visibility $it")
|
loadRecipeImage(binding.image, arguments.recipeSlug)
|
||||||
binding.ingredientsHolder.isVisible = it.areIngredientsVisible
|
loadRecipeInfo(arguments.recipeId, arguments.recipeSlug)
|
||||||
binding.instructionsGroup.isVisible = it.areInstructionsVisible
|
uiState.observe(viewLifecycleOwner, ::onUiStateChange)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onUiStateChange(uiState: RecipeInfoUiState) = with(binding) {
|
||||||
|
Timber.v("onUiStateChange() called")
|
||||||
|
ingredientsHolder.isVisible = uiState.areIngredientsVisible
|
||||||
|
instructionsGroup.isVisible = uiState.areInstructionsVisible
|
||||||
|
uiState.recipeInfo?.let {
|
||||||
|
title.text = it.recipeSummaryEntity.name
|
||||||
|
description.text = it.recipeSummaryEntity.description
|
||||||
|
ingredientsAdapter.submitList(it.recipeIngredients)
|
||||||
|
instructionsAdapter.submitList(it.recipeInstructions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package gq.kirmanak.mealient.ui.recipes.info
|
package gq.kirmanak.mealient.ui.recipes.info
|
||||||
|
|
||||||
data class RecipeInfoListsVisibility(
|
import gq.kirmanak.mealient.data.recipes.impl.FullRecipeInfo
|
||||||
|
|
||||||
|
data class RecipeInfoUiState(
|
||||||
val areIngredientsVisible: Boolean = false,
|
val areIngredientsVisible: Boolean = false,
|
||||||
val areInstructionsVisible: Boolean = false,
|
val areInstructionsVisible: Boolean = false,
|
||||||
|
val recipeInfo: FullRecipeInfo? = null,
|
||||||
)
|
)
|
||||||
@@ -8,28 +8,19 @@ import androidx.lifecycle.viewModelScope
|
|||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeImageLoader
|
import gq.kirmanak.mealient.data.recipes.RecipeImageLoader
|
||||||
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
import gq.kirmanak.mealient.data.recipes.RecipeRepo
|
||||||
import gq.kirmanak.mealient.data.recipes.impl.FullRecipeInfo
|
|
||||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class RecipeInfoViewModel
|
class RecipeInfoViewModel @Inject constructor(
|
||||||
@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 _uiState = MutableLiveData(RecipeInfoUiState())
|
||||||
val recipeInfo: LiveData<FullRecipeInfo>
|
val uiState: LiveData<RecipeInfoUiState> get() = _uiState
|
||||||
get() = _recipeInfo
|
|
||||||
private val _listsVisibility = MutableLiveData(RecipeInfoListsVisibility())
|
|
||||||
val listsVisibility: LiveData<RecipeInfoListsVisibility>
|
|
||||||
get() = _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")
|
||||||
@@ -38,21 +29,16 @@ constructor(
|
|||||||
|
|
||||||
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()
|
_uiState.value = RecipeInfoUiState()
|
||||||
recipeIngredientsAdapter.submitList(null)
|
|
||||||
recipeInstructionsAdapter.submitList(null)
|
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
runCatchingExceptCancel { recipeRepo.loadRecipeInfo(recipeId, recipeSlug) }
|
runCatchingExceptCancel { recipeRepo.loadRecipeInfo(recipeId, recipeSlug) }
|
||||||
.onSuccess {
|
.onSuccess {
|
||||||
Timber.d("loadRecipeInfo: received recipe info = $it")
|
Timber.d("loadRecipeInfo: received recipe info = $it")
|
||||||
_recipeInfo.value = it
|
_uiState.value = RecipeInfoUiState(
|
||||||
recipeIngredientsAdapter.submitList(it.recipeIngredients)
|
areIngredientsVisible = it.recipeIngredients.isNotEmpty(),
|
||||||
recipeInstructionsAdapter.submitList(it.recipeInstructions)
|
areInstructionsVisible = it.recipeInstructions.isNotEmpty(),
|
||||||
_listsVisibility.value =
|
recipeInfo = it,
|
||||||
RecipeInfoListsVisibility(
|
)
|
||||||
areIngredientsVisible = it.recipeIngredients.isNotEmpty(),
|
|
||||||
areInstructionsVisible = it.recipeInstructions.isNotEmpty()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
.onFailure { Timber.e(it, "loadRecipeInfo: can't load recipe info") }
|
.onFailure { Timber.e(it, "loadRecipeInfo: can't load recipe info") }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,8 @@ 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
|
|
||||||
|
|
||||||
@Singleton
|
class RecipeIngredientsAdapter :
|
||||||
class RecipeIngredientsAdapter @Inject constructor() :
|
|
||||||
ListAdapter<RecipeIngredientEntity, RecipeIngredientViewHolder>(RecipeIngredientDiffCallback) {
|
ListAdapter<RecipeIngredientEntity, RecipeIngredientViewHolder>(RecipeIngredientDiffCallback) {
|
||||||
|
|
||||||
class RecipeIngredientViewHolder(
|
class RecipeIngredientViewHolder(
|
||||||
|
|||||||
@@ -10,11 +10,8 @@ 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
|
|
||||||
|
|
||||||
@Singleton
|
class RecipeInstructionsAdapter :
|
||||||
class RecipeInstructionsAdapter @Inject constructor() :
|
|
||||||
ListAdapter<RecipeInstructionEntity, RecipeInstructionViewHolder>(RecipeInstructionDiffCallback) {
|
ListAdapter<RecipeInstructionEntity, RecipeInstructionViewHolder>(RecipeInstructionDiffCallback) {
|
||||||
|
|
||||||
private object RecipeInstructionDiffCallback :
|
private object RecipeInstructionDiffCallback :
|
||||||
|
|||||||
Reference in New Issue
Block a user