Create separate model for v1 add recipe request
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package gq.kirmanak.mealient.data.add
|
||||
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
|
||||
interface AddRecipeDataSource {
|
||||
suspend fun addRecipe(recipe: AddRecipeRequestV0): String
|
||||
|
||||
suspend fun addRecipe(recipe: AddRecipeInfo): String
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package gq.kirmanak.mealient.data.add
|
||||
|
||||
data class AddRecipeInfo(
|
||||
val name: String = "",
|
||||
val description: String = "",
|
||||
val image: String = "",
|
||||
val recipeYield: String = "",
|
||||
val recipeIngredient: List<AddRecipeIngredientInfo> = emptyList(),
|
||||
val recipeInstructions: List<AddRecipeInstructionInfo> = emptyList(),
|
||||
val slug: String = "",
|
||||
val filePath: String = "",
|
||||
val tags: List<String> = emptyList(),
|
||||
val categories: List<String> = emptyList(),
|
||||
val notes: List<AddRecipeNoteInfo> = emptyList(),
|
||||
val extras: Map<String, String> = emptyMap(),
|
||||
val assets: List<String> = emptyList(),
|
||||
val settings: AddRecipeSettingsInfo = AddRecipeSettingsInfo(),
|
||||
)
|
||||
|
||||
data class AddRecipeSettingsInfo(
|
||||
val disableAmount: Boolean = true,
|
||||
val disableComments: Boolean = false,
|
||||
val landscapeView: Boolean = true,
|
||||
val public: Boolean = true,
|
||||
val showAssets: Boolean = true,
|
||||
val showNutrition: Boolean = true,
|
||||
)
|
||||
|
||||
data class AddRecipeNoteInfo(
|
||||
val title: String = "",
|
||||
val text: String = "",
|
||||
)
|
||||
|
||||
data class AddRecipeIngredientInfo(
|
||||
val disableAmount: Boolean = true,
|
||||
val food: String? = null,
|
||||
val note: String = "",
|
||||
val quantity: Int = 1,
|
||||
val title: String? = null,
|
||||
val unit: String? = null,
|
||||
)
|
||||
|
||||
data class AddRecipeInstructionInfo(
|
||||
val title: String = "",
|
||||
val text: String = "",
|
||||
)
|
||||
@@ -1,13 +1,12 @@
|
||||
package gq.kirmanak.mealient.data.add
|
||||
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface AddRecipeRepo {
|
||||
|
||||
val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
||||
val addRecipeRequestFlow: Flow<AddRecipeInfo>
|
||||
|
||||
suspend fun preserve(recipe: AddRecipeRequestV0)
|
||||
suspend fun preserve(recipe: AddRecipeInfo)
|
||||
|
||||
suspend fun clear()
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealient.data.add.impl
|
||||
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
||||
import gq.kirmanak.mealient.extensions.toAddRecipeRequest
|
||||
import gq.kirmanak.mealient.extensions.toAddRecipeInfo
|
||||
import gq.kirmanak.mealient.extensions.toDraft
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -20,10 +20,10 @@ class AddRecipeRepoImpl @Inject constructor(
|
||||
private val logger: Logger,
|
||||
) : AddRecipeRepo {
|
||||
|
||||
override val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
||||
get() = addRecipeStorage.updates.map { it.toAddRecipeRequest() }
|
||||
override val addRecipeRequestFlow: Flow<AddRecipeInfo>
|
||||
get() = addRecipeStorage.updates.map { it.toAddRecipeInfo() }
|
||||
|
||||
override suspend fun preserve(recipe: AddRecipeRequestV0) {
|
||||
override suspend fun preserve(recipe: AddRecipeInfo) {
|
||||
logger.v { "preserveRecipe() called with: recipe = $recipe" }
|
||||
addRecipeStorage.save(recipe.toDraft())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package gq.kirmanak.mealient.data.network
|
||||
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||
import gq.kirmanak.mealient.data.auth.AuthRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
|
||||
import gq.kirmanak.mealient.data.baseurl.ServerVersion
|
||||
@@ -10,10 +11,11 @@ import gq.kirmanak.mealient.data.recipes.network.RecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.datasource.NetworkError
|
||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v1.MealieDataSourceV1
|
||||
import gq.kirmanak.mealient.extensions.toFullRecipeInfo
|
||||
import gq.kirmanak.mealient.extensions.toRecipeSummaryInfo
|
||||
import gq.kirmanak.mealient.extensions.toV0Request
|
||||
import gq.kirmanak.mealient.extensions.toV1Request
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -21,21 +23,26 @@ import javax.inject.Singleton
|
||||
class MealieDataSourceWrapper @Inject constructor(
|
||||
private val serverInfoRepo: ServerInfoRepo,
|
||||
private val authRepo: AuthRepo,
|
||||
private val v0source: MealieDataSourceV0,
|
||||
private val v0Source: MealieDataSourceV0,
|
||||
private val v1Source: MealieDataSourceV1,
|
||||
) : AddRecipeDataSource, RecipeDataSource {
|
||||
|
||||
override suspend fun addRecipe(recipe: AddRecipeRequestV0): String = withAuthHeader { token ->
|
||||
v0source.addRecipe(getUrl(), token, recipe)
|
||||
override suspend fun addRecipe(
|
||||
recipe: AddRecipeInfo,
|
||||
): String = makeCall { token, url, version ->
|
||||
when (version) {
|
||||
ServerVersion.V0 -> v0Source.addRecipe(url, token, recipe.toV0Request())
|
||||
ServerVersion.V1 -> v1Source.addRecipe(url, token, recipe.toV1Request())
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun requestRecipes(
|
||||
start: Int, limit: Int
|
||||
): List<RecipeSummaryInfo> = withAuthHeader { token ->
|
||||
val url = getUrl()
|
||||
when (getVersion()) {
|
||||
start: Int,
|
||||
limit: Int,
|
||||
): List<RecipeSummaryInfo> = makeCall { token, url, version ->
|
||||
when (version) {
|
||||
ServerVersion.V0 -> {
|
||||
v0source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
v0Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||
}
|
||||
ServerVersion.V1 -> {
|
||||
// Imagine start is 30 and limit is 15. It means that we already have page 1 and 2, now we need page 3
|
||||
@@ -45,26 +52,25 @@ class MealieDataSourceWrapper @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun requestRecipeInfo(slug: String): FullRecipeInfo = withAuthHeader { token ->
|
||||
val url = getUrl()
|
||||
when (getVersion()) {
|
||||
ServerVersion.V0 -> v0source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
override suspend fun requestRecipeInfo(
|
||||
slug: String,
|
||||
): FullRecipeInfo = makeCall { token, url, version ->
|
||||
when (version) {
|
||||
ServerVersion.V0 -> v0Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
ServerVersion.V1 -> v1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun getUrl() = serverInfoRepo.requireUrl()
|
||||
|
||||
private suspend fun getVersion() = serverInfoRepo.getVersion()
|
||||
|
||||
private suspend inline fun <T> withAuthHeader(block: (String?) -> T): T {
|
||||
private suspend inline fun <T> makeCall(block: (String?, String, ServerVersion) -> T): T {
|
||||
val authHeader = authRepo.getAuthHeader()
|
||||
return runCatchingExceptCancel { block(authHeader) }.getOrElse {
|
||||
val url = serverInfoRepo.requireUrl()
|
||||
val version = serverInfoRepo.getVersion()
|
||||
return runCatchingExceptCancel { block(authHeader, url, version) }.getOrElse {
|
||||
if (it is NetworkError.Unauthorized) {
|
||||
authRepo.invalidateAuthHeader()
|
||||
// Trying again with new authentication header
|
||||
val newHeader = authRepo.getAuthHeader()
|
||||
if (newHeader == authHeader) throw it else block(newHeader)
|
||||
if (newHeader == authHeader) throw it else block(newHeader, url, version)
|
||||
} else {
|
||||
throw it
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user