Split loadRecipeInfo to refresh/load
This commit is contained in:
@@ -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<Result<FullRecipeEntity>> = liveData {
|
||||
val result = runCatchingExceptCancel {
|
||||
recipeRepo.loadRecipeInfo(summaryEntity.remoteId, summaryEntity.slug)
|
||||
fun refreshRecipeInfo(recipeSlug: String): LiveData<Result<Unit>> {
|
||||
logger.v { "refreshRecipeInfo called with: recipeSlug = $recipeSlug" }
|
||||
return liveData {
|
||||
val result = recipeRepo.refreshRecipeInfo(recipeSlug)
|
||||
logger.v { "refreshRecipeInfo: emitting $result" }
|
||||
emit(result)
|
||||
}
|
||||
emit(result)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ class RecipeInfoViewModel @Inject constructor(
|
||||
|
||||
val uiState: LiveData<RecipeInfoUiState> = 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(),
|
||||
|
||||
Reference in New Issue
Block a user