From 4ad3e7662ec0f0a8363f2babfa39b704f13d9878 Mon Sep 17 00:00:00 2001 From: Kirill Kamakin Date: Sun, 6 Nov 2022 13:46:45 +0100 Subject: [PATCH] Split loadRecipeInfo to refresh/load --- .../mealient/data/recipes/RecipeRepo.kt | 4 ++-- .../mealient/data/recipes/db/RecipeStorage.kt | 2 +- .../data/recipes/db/RecipeStorageImpl.kt | 6 ++--- .../data/recipes/impl/RecipeRepoImpl.kt | 22 +++++++------------ .../mealient/ui/recipes/RecipeViewModel.kt | 15 +++++-------- .../mealient/ui/recipes/RecipesFragment.kt | 17 ++++++-------- .../ui/recipes/info/RecipeInfoViewModel.kt | 2 +- app/src/main/res/navigation/nav_graph.xml | 3 --- 8 files changed, 27 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt index 1c200b1..998043e 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/RecipeRepo.kt @@ -9,7 +9,7 @@ interface RecipeRepo { suspend fun clearLocalData() - suspend fun loadRecipeInfo(recipeId: String, recipeSlug: String): FullRecipeEntity + suspend fun refreshRecipeInfo(recipeSlug: String): Result - suspend fun loadRecipeInfoFromDb(recipeId: String, recipeSlug: String): FullRecipeEntity? + suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity? } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorage.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorage.kt index 431e74a..89f9439 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorage.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorage.kt @@ -17,5 +17,5 @@ interface RecipeStorage { suspend fun saveRecipeInfo(recipe: FullRecipeInfo) - suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity + suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity? } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorageImpl.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorageImpl.kt index 5b7f257..3431207 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorageImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/db/RecipeStorageImpl.kt @@ -74,11 +74,9 @@ class RecipeStorageImpl @Inject constructor( } } - override suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity { + override suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity? { logger.v { "queryRecipeInfo() called with: recipeId = $recipeId" } - val fullRecipeInfo = checkNotNull(recipeDao.queryFullRecipeInfo(recipeId)) { - "Can't find recipe by id $recipeId in DB" - } + val fullRecipeInfo = recipeDao.queryFullRecipeInfo(recipeId) logger.v { "queryRecipeInfo() returned: $fullRecipeInfo" } return fullRecipeInfo } diff --git a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt index c6412c7..4bedf65 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/recipes/impl/RecipeRepoImpl.kt @@ -38,25 +38,19 @@ class RecipeRepoImpl @Inject constructor( storage.clearAllLocalData() } - override suspend fun loadRecipeInfo(recipeId: String, recipeSlug: String): FullRecipeEntity { - logger.v { "loadRecipeInfo() called with: recipeId = $recipeId, recipeSlug = $recipeSlug" } - - runCatchingExceptCancel { + override suspend fun refreshRecipeInfo(recipeSlug: String): Result { + logger.v { "refreshRecipeInfo() called with: recipeSlug = $recipeSlug" } + return runCatchingExceptCancel { storage.saveRecipeInfo(dataSource.requestRecipeInfo(recipeSlug)) }.onFailure { logger.e(it) { "loadRecipeInfo: can't update full recipe info" } } - - return storage.queryRecipeInfo(recipeId) } - override suspend fun loadRecipeInfoFromDb( - recipeId: String, - recipeSlug: String - ): FullRecipeEntity? { - logger.v { "loadRecipeInfoFromDb() called with: recipeId = $recipeId, recipeSlug = $recipeSlug" } - return runCatchingExceptCancel { storage.queryRecipeInfo(recipeId) } - .onFailure { logger.e(it) { "loadRecipeInfoFromDb failed" } } - .getOrNull() + override suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity? { + logger.v { "loadRecipeInfo() called with: recipeId = $recipeId" } + val recipeInfo = storage.queryRecipeInfo(recipeId) + logger.v { "loadRecipeInfo() returned: $recipeInfo" } + return recipeInfo } } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipeViewModel.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipeViewModel.kt index 9fe0df0..7760d28 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipeViewModel.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipeViewModel.kt @@ -5,9 +5,6 @@ import androidx.paging.cachedIn import dagger.hilt.android.lifecycle.HiltViewModel import gq.kirmanak.mealient.data.auth.AuthRepo import gq.kirmanak.mealient.data.recipes.RecipeRepo -import gq.kirmanak.mealient.database.recipe.entity.FullRecipeEntity -import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity -import gq.kirmanak.mealient.datasource.runCatchingExceptCancel import gq.kirmanak.mealient.extensions.valueUpdatesOnly import gq.kirmanak.mealient.logging.Logger import kotlinx.coroutines.flow.launchIn @@ -38,12 +35,12 @@ class RecipeViewModel @Inject constructor( _isAuthorized.postValue(null) } - fun loadRecipeInfo( - summaryEntity: RecipeSummaryEntity - ): LiveData> = liveData { - val result = runCatchingExceptCancel { - recipeRepo.loadRecipeInfo(summaryEntity.remoteId, summaryEntity.slug) + fun refreshRecipeInfo(recipeSlug: String): LiveData> { + logger.v { "refreshRecipeInfo called with: recipeSlug = $recipeSlug" } + return liveData { + val result = recipeRepo.refreshRecipeInfo(recipeSlug) + logger.v { "refreshRecipeInfo: emitting $result" } + emit(result) } - emit(result) } } \ No newline at end of file diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesFragment.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesFragment.kt index 37c9af8..0f2260b 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesFragment.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/RecipesFragment.kt @@ -54,23 +54,20 @@ class RecipesFragment : Fragment(R.layout.fragment_recipes) { setupRecipeAdapter() } - private fun navigateToRecipeInfo(recipeSummaryEntity: RecipeSummaryEntity) { - logger.v { "navigateToRecipeInfo() called with: recipeSummaryEntity = $recipeSummaryEntity" } - findNavController().navigate( - RecipesFragmentDirections.actionRecipesFragmentToRecipeInfoFragment( - recipeSlug = recipeSummaryEntity.slug, recipeId = recipeSummaryEntity.remoteId - ) - ) + private fun navigateToRecipeInfo(id: String) { + logger.v { "navigateToRecipeInfo() called with: id = $id" } + val directions = RecipesFragmentDirections.actionRecipesFragmentToRecipeInfoFragment(id) + findNavController().navigate(directions) } private fun onRecipeClicked(recipe: RecipeSummaryEntity) { - logger.d { "onRecipeClicked() called with: recipe = $recipe" } + logger.v { "onRecipeClicked() called with: recipe = $recipe" } if (ignoreRecipeClicks) return binding.progress.isVisible = true ignoreRecipeClicks = true // TODO doesn't really work - viewModel.loadRecipeInfo(recipe).observeOnce(viewLifecycleOwner) { result -> + viewModel.refreshRecipeInfo(recipe.slug).observeOnce(viewLifecycleOwner) { result -> binding.progress.isVisible = false - if (result.isSuccess) navigateToRecipeInfo(recipe) + if (result.isSuccess) navigateToRecipeInfo(recipe.remoteId) ignoreRecipeClicks = false } } diff --git a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/info/RecipeInfoViewModel.kt b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/info/RecipeInfoViewModel.kt index 6abe446..7a23e2a 100644 --- a/app/src/main/java/gq/kirmanak/mealient/ui/recipes/info/RecipeInfoViewModel.kt +++ b/app/src/main/java/gq/kirmanak/mealient/ui/recipes/info/RecipeInfoViewModel.kt @@ -20,7 +20,7 @@ class RecipeInfoViewModel @Inject constructor( val uiState: LiveData = liveData { logger.v { "Initializing UI state with args = $args" } - val state = recipeRepo.loadRecipeInfoFromDb(args.recipeId, args.recipeSlug)?.let { + val state = recipeRepo.loadRecipeInfo(args.recipeId)?.let { RecipeInfoUiState( areIngredientsVisible = it.recipeIngredients.isNotEmpty(), areInstructionsVisible = it.recipeInstructions.isNotEmpty(), diff --git a/app/src/main/res/navigation/nav_graph.xml b/app/src/main/res/navigation/nav_graph.xml index 243f6f7..6d506bc 100644 --- a/app/src/main/res/navigation/nav_graph.xml +++ b/app/src/main/res/navigation/nav_graph.xml @@ -31,9 +31,6 @@ android:name="gq.kirmanak.mealient.ui.recipes.info.RecipeInfoFragment" android:label="RecipeInfoFragment" tools:layout="@layout/fragment_recipe_info"> -