Fix absent images on v0.5.6
This commit is contained in:
@@ -6,13 +6,14 @@ import gq.kirmanak.mealient.data.baseurl.BaseURLStorage
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionDataSource
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeDataSource
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.datasource.MealieDataSource
|
||||
import gq.kirmanak.mealient.datasource.models.AddRecipeRequest
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.models.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.extensions.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.extensions.toRecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.extensions.toVersionInfo
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -21,48 +22,36 @@ import javax.inject.Singleton
|
||||
class MealieDataSourceWrapper @Inject constructor(
|
||||
private val baseURLStorage: BaseURLStorage,
|
||||
private val authRepo: AuthRepo,
|
||||
private val mealieDataSource: MealieDataSource,
|
||||
private val mealieDataSourceV1: MealieDataSourceV1,
|
||||
private val source: MealieDataSource,
|
||||
private val v1Source: MealieDataSourceV1,
|
||||
) : AddRecipeDataSource, RecipeDataSource, VersionDataSource {
|
||||
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequest): String =
|
||||
withAuthHeader { token -> mealieDataSource.addRecipe(getUrl(), token, recipe) }
|
||||
withAuthHeader { token -> source.addRecipe(getUrl(), token, recipe) }
|
||||
|
||||
override suspend fun getVersionInfo(baseUrl: String): VersionInfo =
|
||||
runCatchingExceptCancel {
|
||||
mealieDataSource.getVersionInfo(baseUrl).toVersionInfo()
|
||||
source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
}.getOrElse {
|
||||
if (it is NetworkError.NotMealie) {
|
||||
mealieDataSourceV1.getVersionInfo(baseUrl).toVersionInfo()
|
||||
v1Source.getVersionInfo(baseUrl).toVersionInfo()
|
||||
} else {
|
||||
throw it
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponseV1> =
|
||||
override suspend fun requestRecipes(start: Int, limit: Int): List<RecipeSummaryInfo> =
|
||||
withAuthHeader { token ->
|
||||
val url = getUrl()
|
||||
if (isV1()) {
|
||||
mealieDataSourceV1.requestRecipes(getUrl(), token, start, limit)
|
||||
v1Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
} else {
|
||||
mealieDataSource.requestRecipes(getUrl(), token, start, limit).map {
|
||||
GetRecipeSummaryResponseV1(
|
||||
remoteId = it.remoteId.toString(),
|
||||
name = it.name,
|
||||
slug = it.slug,
|
||||
image = it.image,
|
||||
description = it.description,
|
||||
recipeCategories = it.recipeCategories,
|
||||
tags = it.tags,
|
||||
rating = it.rating,
|
||||
dateAdded = it.dateAdded,
|
||||
dateUpdated = it.dateUpdated,
|
||||
)
|
||||
}
|
||||
source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun requestRecipeInfo(slug: String): GetRecipeResponse =
|
||||
withAuthHeader { token -> mealieDataSource.requestRecipeInfo(getUrl(), token, slug) }
|
||||
withAuthHeader { token -> source.requestRecipeInfo(getUrl(), token, slug) }
|
||||
|
||||
private suspend fun getUrl() = baseURLStorage.requireBaseURL()
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package gq.kirmanak.mealient.data.recipes.db
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.database.recipe.entity.FullRecipeInfo
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
|
||||
interface RecipeStorage {
|
||||
suspend fun saveRecipes(recipes: List<GetRecipeSummaryResponseV1>)
|
||||
suspend fun saveRecipes(recipes: List<RecipeSummaryInfo>)
|
||||
|
||||
fun queryRecipes(): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
suspend fun refreshAll(recipes: List<GetRecipeSummaryResponseV1>)
|
||||
suspend fun refreshAll(recipes: List<RecipeSummaryInfo>)
|
||||
|
||||
suspend fun clearAllLocalData()
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ package gq.kirmanak.mealient.data.recipes.db
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.withTransaction
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.database.AppDb
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeDao
|
||||
import gq.kirmanak.mealient.database.recipe.entity.*
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
import gq.kirmanak.mealient.extensions.recipeEntity
|
||||
import gq.kirmanak.mealient.extensions.toRecipeEntity
|
||||
import gq.kirmanak.mealient.extensions.toRecipeIngredientEntity
|
||||
@@ -23,7 +23,7 @@ class RecipeStorageImpl @Inject constructor(
|
||||
private val recipeDao: RecipeDao by lazy { db.recipeDao() }
|
||||
|
||||
override suspend fun saveRecipes(
|
||||
recipes: List<GetRecipeSummaryResponseV1>
|
||||
recipes: List<RecipeSummaryInfo>
|
||||
) = db.withTransaction {
|
||||
logger.v { "saveRecipes() called with $recipes" }
|
||||
|
||||
@@ -96,7 +96,7 @@ class RecipeStorageImpl @Inject constructor(
|
||||
return recipeDao.queryRecipesByPages()
|
||||
}
|
||||
|
||||
override suspend fun refreshAll(recipes: List<GetRecipeSummaryResponseV1>) {
|
||||
override suspend fun refreshAll(recipes: List<RecipeSummaryInfo>) {
|
||||
logger.v { "refreshAll() called with: recipes = $recipes" }
|
||||
db.withTransaction {
|
||||
recipeDao.removeAllRecipes()
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package gq.kirmanak.mealient.data.recipes.network
|
||||
|
||||
import gq.kirmanak.mealient.datasource.models.GetRecipeResponse
|
||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||
|
||||
interface RecipeDataSource {
|
||||
suspend fun requestRecipes(start: Int, limit: Int): List<GetRecipeSummaryResponseV1>
|
||||
suspend fun requestRecipes(start: Int, limit: Int): List<RecipeSummaryInfo>
|
||||
|
||||
suspend fun requestRecipeInfo(slug: String): GetRecipeResponse
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package gq.kirmanak.mealient.data.recipes.network
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
|
||||
data class RecipeSummaryInfo(
|
||||
val remoteId: String,
|
||||
val name: String,
|
||||
val slug: String,
|
||||
val image: String?,
|
||||
val description: String = "",
|
||||
val recipeCategories: List<String>,
|
||||
val tags: List<String>,
|
||||
val rating: Int?,
|
||||
val dateAdded: LocalDate,
|
||||
val dateUpdated: LocalDateTime,
|
||||
val imageId: String,
|
||||
)
|
||||
@@ -1,6 +1,7 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
|
||||
@@ -33,7 +34,35 @@ fun GetRecipeInstructionResponse.toRecipeInstructionEntity(remoteId: String) =
|
||||
text = text
|
||||
)
|
||||
|
||||
fun GetRecipeSummaryResponseV1.recipeEntity() = RecipeSummaryEntity(
|
||||
fun GetRecipeSummaryResponse.toRecipeSummaryInfo() = RecipeSummaryInfo(
|
||||
remoteId = remoteId.toString(),
|
||||
name = name,
|
||||
slug = slug,
|
||||
image = image,
|
||||
description = description,
|
||||
recipeCategories = recipeCategories,
|
||||
tags = tags,
|
||||
rating = rating,
|
||||
dateAdded = dateAdded,
|
||||
dateUpdated = dateUpdated,
|
||||
imageId = slug,
|
||||
)
|
||||
|
||||
fun GetRecipeSummaryResponseV1.toRecipeSummaryInfo() = RecipeSummaryInfo(
|
||||
remoteId = remoteId,
|
||||
name = name,
|
||||
slug = slug,
|
||||
image = image,
|
||||
description = description,
|
||||
recipeCategories = recipeCategories,
|
||||
tags = tags,
|
||||
rating = rating,
|
||||
dateAdded = dateAdded,
|
||||
dateUpdated = dateUpdated,
|
||||
imageId = remoteId,
|
||||
)
|
||||
|
||||
fun RecipeSummaryInfo.recipeEntity() = RecipeSummaryEntity(
|
||||
remoteId = remoteId,
|
||||
name = name,
|
||||
slug = slug,
|
||||
@@ -42,6 +71,7 @@ fun GetRecipeSummaryResponseV1.recipeEntity() = RecipeSummaryEntity(
|
||||
rating = rating,
|
||||
dateAdded = dateAdded,
|
||||
dateUpdated = dateUpdated,
|
||||
imageId = imageId,
|
||||
)
|
||||
|
||||
fun VersionResponse.toVersionInfo() = VersionInfo(version)
|
||||
|
||||
@@ -44,7 +44,7 @@ class RecipeModelLoader private constructor(
|
||||
options: Options?
|
||||
): String? {
|
||||
logger.v { "getUrl() called with: model = $model, width = $width, height = $height, options = $options" }
|
||||
return runBlocking { recipeImageUrlProvider.generateImageUrl(model?.remoteId) }
|
||||
return runBlocking { recipeImageUrlProvider.generateImageUrl(model?.imageId) }
|
||||
}
|
||||
|
||||
override fun getHeaders(
|
||||
|
||||
Reference in New Issue
Block a user