Ignore ingredients with empty note

This commit is contained in:
Kirill Kamakin
2022-11-06 20:02:51 +01:00
parent 377509d160
commit 0c0248dbbc
4 changed files with 38 additions and 21 deletions

View File

@@ -62,15 +62,13 @@ class RecipeInfoFragment : BottomSheetDialogFragment() {
private fun onUiStateChange(uiState: RecipeInfoUiState) = with(binding) { private fun onUiStateChange(uiState: RecipeInfoUiState) = with(binding) {
logger.v { "onUiStateChange() called" } logger.v { "onUiStateChange() called" }
ingredientsHolder.isVisible = uiState.areIngredientsVisible ingredientsHolder.isVisible = uiState.showIngredients
instructionsGroup.isVisible = uiState.areInstructionsVisible instructionsGroup.isVisible = uiState.showInstructions
uiState.recipeInfo?.let { recipeImageLoader.loadRecipeImage(image, uiState.summaryEntity)
recipeImageLoader.loadRecipeImage(image, it.recipeSummaryEntity) title.text = uiState.title
title.text = it.recipeSummaryEntity.name description.text = uiState.description
description.text = it.recipeSummaryEntity.description ingredientsAdapter.submitList(uiState.recipeIngredients)
ingredientsAdapter.submitList(it.recipeIngredients) instructionsAdapter.submitList(uiState.recipeInstructions)
instructionsAdapter.submitList(it.recipeInstructions)
}
} }
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = override fun onCreateDialog(savedInstanceState: Bundle?): Dialog =

View File

@@ -1,9 +1,15 @@
package gq.kirmanak.mealient.ui.recipes.info package gq.kirmanak.mealient.ui.recipes.info
import gq.kirmanak.mealient.database.recipe.entity.FullRecipeEntity import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
data class RecipeInfoUiState( data class RecipeInfoUiState(
val areIngredientsVisible: Boolean = false, val showIngredients: Boolean = false,
val areInstructionsVisible: Boolean = false, val showInstructions: Boolean = false,
val recipeInfo: FullRecipeEntity? = null, val summaryEntity: RecipeSummaryEntity? = null,
val recipeIngredients: List<RecipeIngredientEntity> = emptyList(),
val recipeInstructions: List<RecipeInstructionEntity> = emptyList(),
val title: String? = null,
val description: String? = null,
) )

View File

@@ -20,11 +20,15 @@ class RecipeInfoViewModel @Inject constructor(
val uiState: LiveData<RecipeInfoUiState> = liveData { val uiState: LiveData<RecipeInfoUiState> = liveData {
logger.v { "Initializing UI state with args = $args" } logger.v { "Initializing UI state with args = $args" }
val state = recipeRepo.loadRecipeInfo(args.recipeId)?.let { val state = recipeRepo.loadRecipeInfo(args.recipeId)?.let { entity ->
RecipeInfoUiState( RecipeInfoUiState(
areIngredientsVisible = it.recipeIngredients.isNotEmpty(), showIngredients = entity.recipeIngredients.isNotEmpty(),
areInstructionsVisible = it.recipeInstructions.isNotEmpty(), showInstructions = entity.recipeInstructions.isNotEmpty(),
recipeInfo = it, summaryEntity = entity.recipeSummaryEntity,
recipeIngredients = entity.recipeIngredients.filter { it.note.isNotBlank() },
recipeInstructions = entity.recipeInstructions.filter { it.text.isNotBlank() },
title = entity.recipeSummaryEntity.name,
description = entity.recipeSummaryEntity.description,
) )
} ?: RecipeInfoUiState() } ?: RecipeInfoUiState()
emit(state) emit(state)

View File

@@ -4,6 +4,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.asFlow import androidx.lifecycle.asFlow
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import gq.kirmanak.mealient.data.recipes.RecipeRepo import gq.kirmanak.mealient.data.recipes.RecipeRepo
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
import gq.kirmanak.mealient.logging.Logger import gq.kirmanak.mealient.logging.Logger
import gq.kirmanak.mealient.test.FakeLogger import gq.kirmanak.mealient.test.FakeLogger
import gq.kirmanak.mealient.test.RecipeImplTestData.FULL_CAKE_INFO_ENTITY import gq.kirmanak.mealient.test.RecipeImplTestData.FULL_CAKE_INFO_ENTITY
@@ -54,11 +55,19 @@ class RecipeInfoViewModelTest {
@Test @Test
fun `when recipe is found then UI state has data`() = runTest { fun `when recipe is found then UI state has data`() = runTest {
coEvery { recipeRepo.loadRecipeInfo(eq(RECIPE_ID)) } returns FULL_CAKE_INFO_ENTITY val emptyNoteIngredient = RecipeIngredientEntity(recipeId = "42", note = "")
val returnedEntity = FULL_CAKE_INFO_ENTITY.copy(
recipeIngredients = FULL_CAKE_INFO_ENTITY.recipeIngredients + emptyNoteIngredient
)
coEvery { recipeRepo.loadRecipeInfo(eq(RECIPE_ID)) } returns returnedEntity
val expected = RecipeInfoUiState( val expected = RecipeInfoUiState(
areIngredientsVisible = true, showIngredients = true,
areInstructionsVisible = true, showInstructions = true,
recipeInfo = FULL_CAKE_INFO_ENTITY summaryEntity = FULL_CAKE_INFO_ENTITY.recipeSummaryEntity,
recipeIngredients = FULL_CAKE_INFO_ENTITY.recipeIngredients,
recipeInstructions = FULL_CAKE_INFO_ENTITY.recipeInstructions,
title = FULL_CAKE_INFO_ENTITY.recipeSummaryEntity.name,
description = FULL_CAKE_INFO_ENTITY.recipeSummaryEntity.description,
) )
val actual = createSubject().uiState.asFlow().first() val actual = createSubject().uiState.asFlow().first()
assertThat(actual).isEqualTo(expected) assertThat(actual).isEqualTo(expected)