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
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package gq.kirmanak.mealient.extensions
|
||||
|
||||
import gq.kirmanak.mealient.data.add.*
|
||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.FullRecipeInfo
|
||||
import gq.kirmanak.mealient.data.recipes.network.RecipeIngredientInfo
|
||||
@@ -78,19 +79,19 @@ fun VersionResponseV0.toVersionInfo() = VersionInfo(version)
|
||||
|
||||
fun VersionResponseV1.toVersionInfo() = VersionInfo(version)
|
||||
|
||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequestV0(
|
||||
fun AddRecipeDraft.toAddRecipeInfo() = AddRecipeInfo(
|
||||
name = recipeName,
|
||||
description = recipeDescription,
|
||||
recipeYield = recipeYield,
|
||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredientV0(note = it) },
|
||||
recipeInstructions = recipeInstructions.map { AddRecipeInstructionV0(text = it) },
|
||||
settings = AddRecipeSettingsV0(
|
||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredientInfo(note = it) },
|
||||
recipeInstructions = recipeInstructions.map { AddRecipeInstructionInfo(text = it) },
|
||||
settings = AddRecipeSettingsInfo(
|
||||
public = isRecipePublic,
|
||||
disableComments = areCommentsDisabled,
|
||||
)
|
||||
)
|
||||
|
||||
fun AddRecipeRequestV0.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
fun AddRecipeInfo.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||
recipeName = name,
|
||||
recipeDescription = description,
|
||||
recipeYield = recipeYield,
|
||||
@@ -159,3 +160,93 @@ fun GetRecipeInstructionResponseV1.toRecipeInstructionInfo() = RecipeInstruction
|
||||
title = title,
|
||||
text = text
|
||||
)
|
||||
|
||||
fun AddRecipeInfo.toV0Request() = AddRecipeRequestV0(
|
||||
name = name,
|
||||
description = description,
|
||||
image = image,
|
||||
recipeYield = recipeYield,
|
||||
recipeIngredient = recipeIngredient.map { it.toV0Ingredient() },
|
||||
recipeInstructions = recipeInstructions.map { it.toV0Instruction() },
|
||||
slug = slug,
|
||||
filePath = filePath,
|
||||
tags = tags,
|
||||
categories = categories,
|
||||
notes = notes.map { it.toV0Note() },
|
||||
extras = extras,
|
||||
assets = assets,
|
||||
settings = settings.toV0Settings(),
|
||||
)
|
||||
|
||||
private fun AddRecipeSettingsInfo.toV0Settings() = AddRecipeSettingsV0(
|
||||
disableAmount = disableAmount,
|
||||
disableComments = disableComments,
|
||||
landscapeView = landscapeView,
|
||||
public = public,
|
||||
showAssets = showAssets,
|
||||
showNutrition = showNutrition,
|
||||
)
|
||||
|
||||
private fun AddRecipeNoteInfo.toV0Note() = AddRecipeNoteV0(
|
||||
title = title,
|
||||
text = text,
|
||||
)
|
||||
|
||||
private fun AddRecipeIngredientInfo.toV0Ingredient() = AddRecipeIngredientV0(
|
||||
disableAmount = disableAmount,
|
||||
food = food,
|
||||
note = note,
|
||||
quantity = quantity,
|
||||
title = title,
|
||||
unit = unit
|
||||
)
|
||||
|
||||
private fun AddRecipeInstructionInfo.toV0Instruction() = AddRecipeInstructionV0(
|
||||
title = title,
|
||||
text = text,
|
||||
)
|
||||
|
||||
fun AddRecipeInfo.toV1Request() = AddRecipeRequestV1(
|
||||
name = name,
|
||||
description = description,
|
||||
image = image,
|
||||
recipeYield = recipeYield,
|
||||
recipeIngredient = recipeIngredient.map { it.toV1Ingredient() },
|
||||
recipeInstructions = recipeInstructions.map { it.toV1Instruction() },
|
||||
slug = slug,
|
||||
filePath = filePath,
|
||||
tags = tags,
|
||||
categories = categories,
|
||||
notes = notes.map { it.toV1Note() },
|
||||
extras = extras,
|
||||
assets = assets,
|
||||
settings = settings.toV1Settings(),
|
||||
)
|
||||
|
||||
private fun AddRecipeSettingsInfo.toV1Settings() = AddRecipeSettingsV1(
|
||||
disableAmount = disableAmount,
|
||||
disableComments = disableComments,
|
||||
landscapeView = landscapeView,
|
||||
public = public,
|
||||
showAssets = showAssets,
|
||||
showNutrition = showNutrition,
|
||||
)
|
||||
|
||||
private fun AddRecipeNoteInfo.toV1Note() = AddRecipeNoteV1(
|
||||
title = title,
|
||||
text = text,
|
||||
)
|
||||
|
||||
private fun AddRecipeIngredientInfo.toV1Ingredient() = AddRecipeIngredientV1(
|
||||
disableAmount = disableAmount,
|
||||
food = food,
|
||||
note = note,
|
||||
quantity = quantity,
|
||||
title = title,
|
||||
unit = unit
|
||||
)
|
||||
|
||||
private fun AddRecipeInstructionInfo.toV1Instruction() = AddRecipeInstructionV1(
|
||||
title = title,
|
||||
text = text,
|
||||
)
|
||||
@@ -12,12 +12,12 @@ import androidx.fragment.app.viewModels
|
||||
import by.kirich1409.viewbindingdelegate.viewBinding
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import gq.kirmanak.mealient.R
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeIngredientInfo
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeInstructionInfo
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeSettingsInfo
|
||||
import gq.kirmanak.mealient.databinding.FragmentAddRecipeBinding
|
||||
import gq.kirmanak.mealient.databinding.ViewSingleInputBinding
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
|
||||
import gq.kirmanak.mealient.extensions.checkIfInputIsEmpty
|
||||
import gq.kirmanak.mealient.extensions.collectWhenViewResumed
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
@@ -122,14 +122,15 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
||||
|
||||
private fun saveValues() = with(binding) {
|
||||
logger.v { "saveValues() called" }
|
||||
val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstructionV0(text = it) }
|
||||
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientV0(note = it) }
|
||||
val settings = AddRecipeSettingsV0(
|
||||
val instructions =
|
||||
parseInputRows(instructionsFlow).map { AddRecipeInstructionInfo(text = it) }
|
||||
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientInfo(note = it) }
|
||||
val settings = AddRecipeSettingsInfo(
|
||||
public = publicRecipe.isChecked,
|
||||
disableComments = disableComments.isChecked,
|
||||
)
|
||||
viewModel.preserve(
|
||||
AddRecipeRequestV0(
|
||||
AddRecipeInfo(
|
||||
name = recipeNameInput.text.toString(),
|
||||
description = recipeDescriptionInput.text.toString(),
|
||||
recipeYield = recipeYieldInput.text.toString(),
|
||||
@@ -148,7 +149,7 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
||||
.filterNot { it.isBlank() }
|
||||
.toList()
|
||||
|
||||
private fun onSavedInputLoaded(request: AddRecipeRequestV0) = with(binding) {
|
||||
private fun onSavedInputLoaded(request: AddRecipeInfo) = with(binding) {
|
||||
logger.v { "onSavedInputLoaded() called with: request = $request" }
|
||||
recipeNameInput.setText(request.name)
|
||||
recipeDescriptionInput.setText(request.description)
|
||||
|
||||
@@ -3,9 +3,9 @@ package gq.kirmanak.mealient.ui.add
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@@ -23,8 +23,8 @@ class AddRecipeViewModel @Inject constructor(
|
||||
private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED)
|
||||
val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow()
|
||||
|
||||
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequestV0>(Channel.UNLIMITED)
|
||||
val preservedAddRecipeRequest: Flow<AddRecipeRequestV0>
|
||||
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeInfo>(Channel.UNLIMITED)
|
||||
val preservedAddRecipeRequest: Flow<AddRecipeInfo>
|
||||
get() = _preservedAddRecipeRequestChannel.receiveAsFlow()
|
||||
|
||||
fun loadPreservedRequest() {
|
||||
@@ -47,7 +47,7 @@ class AddRecipeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun preserve(request: AddRecipeRequestV0) {
|
||||
fun preserve(request: AddRecipeInfo) {
|
||||
logger.v { "preserve() called with: request = $request" }
|
||||
viewModelScope.launch { addRecipeRepo.preserve(request) }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user