Split loadRecipeInfo to refresh/load
This commit is contained in:
@@ -9,7 +9,7 @@ interface RecipeRepo {
|
||||
|
||||
suspend fun clearLocalData()
|
||||
|
||||
suspend fun loadRecipeInfo(recipeId: String, recipeSlug: String): FullRecipeEntity
|
||||
suspend fun refreshRecipeInfo(recipeSlug: String): Result<Unit>
|
||||
|
||||
suspend fun loadRecipeInfoFromDb(recipeId: String, recipeSlug: String): FullRecipeEntity?
|
||||
suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity?
|
||||
}
|
||||
@@ -17,5 +17,5 @@ interface RecipeStorage {
|
||||
|
||||
suspend fun saveRecipeInfo(recipe: FullRecipeInfo)
|
||||
|
||||
suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity
|
||||
suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity?
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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<Unit> {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
@@ -31,9 +31,6 @@
|
||||
android:name="gq.kirmanak.mealient.ui.recipes.info.RecipeInfoFragment"
|
||||
android:label="RecipeInfoFragment"
|
||||
tools:layout="@layout/fragment_recipe_info">
|
||||
<argument
|
||||
android:name="recipe_slug"
|
||||
app:argType="string" />
|
||||
<argument
|
||||
android:name="recipe_id"
|
||||
app:argType="string" />
|
||||
|
||||
Reference in New Issue
Block a user