Split loadRecipeInfo to refresh/load

This commit is contained in:
Kirill Kamakin
2022-11-06 13:46:45 +01:00
parent cc73f68751
commit 4ad3e7662e
8 changed files with 27 additions and 44 deletions

View File

@@ -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?
}

View File

@@ -17,5 +17,5 @@ interface RecipeStorage {
suspend fun saveRecipeInfo(recipe: FullRecipeInfo)
suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity
suspend fun queryRecipeInfo(recipeId: String): FullRecipeEntity?
}

View File

@@ -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
}

View File

@@ -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
}
}