Create separate model for v1 add recipe request
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package gq.kirmanak.mealient.data.add
|
package gq.kirmanak.mealient.data.add
|
||||||
|
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
|
||||||
|
|
||||||
interface AddRecipeDataSource {
|
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
|
package gq.kirmanak.mealient.data.add
|
||||||
|
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
interface AddRecipeRepo {
|
interface AddRecipeRepo {
|
||||||
|
|
||||||
val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
val addRecipeRequestFlow: Flow<AddRecipeInfo>
|
||||||
|
|
||||||
suspend fun preserve(recipe: AddRecipeRequestV0)
|
suspend fun preserve(recipe: AddRecipeInfo)
|
||||||
|
|
||||||
suspend fun clear()
|
suspend fun clear()
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package gq.kirmanak.mealient.data.add.impl
|
package gq.kirmanak.mealient.data.add.impl
|
||||||
|
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
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.data.add.AddRecipeRepo
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
|
||||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeStorage
|
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.extensions.toDraft
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -20,10 +20,10 @@ class AddRecipeRepoImpl @Inject constructor(
|
|||||||
private val logger: Logger,
|
private val logger: Logger,
|
||||||
) : AddRecipeRepo {
|
) : AddRecipeRepo {
|
||||||
|
|
||||||
override val addRecipeRequestFlow: Flow<AddRecipeRequestV0>
|
override val addRecipeRequestFlow: Flow<AddRecipeInfo>
|
||||||
get() = addRecipeStorage.updates.map { it.toAddRecipeRequest() }
|
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" }
|
logger.v { "preserveRecipe() called with: recipe = $recipe" }
|
||||||
addRecipeStorage.save(recipe.toDraft())
|
addRecipeStorage.save(recipe.toDraft())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package gq.kirmanak.mealient.data.network
|
package gq.kirmanak.mealient.data.network
|
||||||
|
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeDataSource
|
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.auth.AuthRepo
|
||||||
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
|
import gq.kirmanak.mealient.data.baseurl.ServerInfoRepo
|
||||||
import gq.kirmanak.mealient.data.baseurl.ServerVersion
|
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.NetworkError
|
||||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||||
import gq.kirmanak.mealient.datasource.v0.MealieDataSourceV0
|
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.datasource.v1.MealieDataSourceV1
|
||||||
import gq.kirmanak.mealient.extensions.toFullRecipeInfo
|
import gq.kirmanak.mealient.extensions.toFullRecipeInfo
|
||||||
import gq.kirmanak.mealient.extensions.toRecipeSummaryInfo
|
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.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
@@ -21,21 +23,26 @@ import javax.inject.Singleton
|
|||||||
class MealieDataSourceWrapper @Inject constructor(
|
class MealieDataSourceWrapper @Inject constructor(
|
||||||
private val serverInfoRepo: ServerInfoRepo,
|
private val serverInfoRepo: ServerInfoRepo,
|
||||||
private val authRepo: AuthRepo,
|
private val authRepo: AuthRepo,
|
||||||
private val v0source: MealieDataSourceV0,
|
private val v0Source: MealieDataSourceV0,
|
||||||
private val v1Source: MealieDataSourceV1,
|
private val v1Source: MealieDataSourceV1,
|
||||||
) : AddRecipeDataSource, RecipeDataSource {
|
) : AddRecipeDataSource, RecipeDataSource {
|
||||||
|
|
||||||
override suspend fun addRecipe(recipe: AddRecipeRequestV0): String = withAuthHeader { token ->
|
override suspend fun addRecipe(
|
||||||
v0source.addRecipe(getUrl(), token, recipe)
|
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(
|
override suspend fun requestRecipes(
|
||||||
start: Int, limit: Int
|
start: Int,
|
||||||
): List<RecipeSummaryInfo> = withAuthHeader { token ->
|
limit: Int,
|
||||||
val url = getUrl()
|
): List<RecipeSummaryInfo> = makeCall { token, url, version ->
|
||||||
when (getVersion()) {
|
when (version) {
|
||||||
ServerVersion.V0 -> {
|
ServerVersion.V0 -> {
|
||||||
v0source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
v0Source.requestRecipes(url, token, start, limit).map { it.toRecipeSummaryInfo() }
|
||||||
}
|
}
|
||||||
ServerVersion.V1 -> {
|
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
|
// 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 ->
|
override suspend fun requestRecipeInfo(
|
||||||
val url = getUrl()
|
slug: String,
|
||||||
when (getVersion()) {
|
): FullRecipeInfo = makeCall { token, url, version ->
|
||||||
ServerVersion.V0 -> v0source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
when (version) {
|
||||||
|
ServerVersion.V0 -> v0Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||||
ServerVersion.V1 -> v1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
ServerVersion.V1 -> v1Source.requestRecipeInfo(url, token, slug).toFullRecipeInfo()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getUrl() = serverInfoRepo.requireUrl()
|
private suspend inline fun <T> makeCall(block: (String?, String, ServerVersion) -> T): T {
|
||||||
|
|
||||||
private suspend fun getVersion() = serverInfoRepo.getVersion()
|
|
||||||
|
|
||||||
private suspend inline fun <T> withAuthHeader(block: (String?) -> T): T {
|
|
||||||
val authHeader = authRepo.getAuthHeader()
|
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) {
|
if (it is NetworkError.Unauthorized) {
|
||||||
authRepo.invalidateAuthHeader()
|
authRepo.invalidateAuthHeader()
|
||||||
// Trying again with new authentication header
|
// Trying again with new authentication header
|
||||||
val newHeader = authRepo.getAuthHeader()
|
val newHeader = authRepo.getAuthHeader()
|
||||||
if (newHeader == authHeader) throw it else block(newHeader)
|
if (newHeader == authHeader) throw it else block(newHeader, url, version)
|
||||||
} else {
|
} else {
|
||||||
throw it
|
throw it
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package gq.kirmanak.mealient.extensions
|
package gq.kirmanak.mealient.extensions
|
||||||
|
|
||||||
|
import gq.kirmanak.mealient.data.add.*
|
||||||
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
import gq.kirmanak.mealient.data.baseurl.VersionInfo
|
||||||
import gq.kirmanak.mealient.data.recipes.network.FullRecipeInfo
|
import gq.kirmanak.mealient.data.recipes.network.FullRecipeInfo
|
||||||
import gq.kirmanak.mealient.data.recipes.network.RecipeIngredientInfo
|
import gq.kirmanak.mealient.data.recipes.network.RecipeIngredientInfo
|
||||||
@@ -78,19 +79,19 @@ fun VersionResponseV0.toVersionInfo() = VersionInfo(version)
|
|||||||
|
|
||||||
fun VersionResponseV1.toVersionInfo() = VersionInfo(version)
|
fun VersionResponseV1.toVersionInfo() = VersionInfo(version)
|
||||||
|
|
||||||
fun AddRecipeDraft.toAddRecipeRequest() = AddRecipeRequestV0(
|
fun AddRecipeDraft.toAddRecipeInfo() = AddRecipeInfo(
|
||||||
name = recipeName,
|
name = recipeName,
|
||||||
description = recipeDescription,
|
description = recipeDescription,
|
||||||
recipeYield = recipeYield,
|
recipeYield = recipeYield,
|
||||||
recipeIngredient = recipeIngredients.map { AddRecipeIngredientV0(note = it) },
|
recipeIngredient = recipeIngredients.map { AddRecipeIngredientInfo(note = it) },
|
||||||
recipeInstructions = recipeInstructions.map { AddRecipeInstructionV0(text = it) },
|
recipeInstructions = recipeInstructions.map { AddRecipeInstructionInfo(text = it) },
|
||||||
settings = AddRecipeSettingsV0(
|
settings = AddRecipeSettingsInfo(
|
||||||
public = isRecipePublic,
|
public = isRecipePublic,
|
||||||
disableComments = areCommentsDisabled,
|
disableComments = areCommentsDisabled,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
fun AddRecipeRequestV0.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
fun AddRecipeInfo.toDraft(): AddRecipeDraft = AddRecipeDraft(
|
||||||
recipeName = name,
|
recipeName = name,
|
||||||
recipeDescription = description,
|
recipeDescription = description,
|
||||||
recipeYield = recipeYield,
|
recipeYield = recipeYield,
|
||||||
@@ -159,3 +160,93 @@ fun GetRecipeInstructionResponseV1.toRecipeInstructionInfo() = RecipeInstruction
|
|||||||
title = title,
|
title = title,
|
||||||
text = text
|
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 by.kirich1409.viewbindingdelegate.viewBinding
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import gq.kirmanak.mealient.R
|
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.FragmentAddRecipeBinding
|
||||||
import gq.kirmanak.mealient.databinding.ViewSingleInputBinding
|
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.checkIfInputIsEmpty
|
||||||
import gq.kirmanak.mealient.extensions.collectWhenViewResumed
|
import gq.kirmanak.mealient.extensions.collectWhenViewResumed
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
@@ -122,14 +122,15 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
|||||||
|
|
||||||
private fun saveValues() = with(binding) {
|
private fun saveValues() = with(binding) {
|
||||||
logger.v { "saveValues() called" }
|
logger.v { "saveValues() called" }
|
||||||
val instructions = parseInputRows(instructionsFlow).map { AddRecipeInstructionV0(text = it) }
|
val instructions =
|
||||||
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientV0(note = it) }
|
parseInputRows(instructionsFlow).map { AddRecipeInstructionInfo(text = it) }
|
||||||
val settings = AddRecipeSettingsV0(
|
val ingredients = parseInputRows(ingredientsFlow).map { AddRecipeIngredientInfo(note = it) }
|
||||||
|
val settings = AddRecipeSettingsInfo(
|
||||||
public = publicRecipe.isChecked,
|
public = publicRecipe.isChecked,
|
||||||
disableComments = disableComments.isChecked,
|
disableComments = disableComments.isChecked,
|
||||||
)
|
)
|
||||||
viewModel.preserve(
|
viewModel.preserve(
|
||||||
AddRecipeRequestV0(
|
AddRecipeInfo(
|
||||||
name = recipeNameInput.text.toString(),
|
name = recipeNameInput.text.toString(),
|
||||||
description = recipeDescriptionInput.text.toString(),
|
description = recipeDescriptionInput.text.toString(),
|
||||||
recipeYield = recipeYieldInput.text.toString(),
|
recipeYield = recipeYieldInput.text.toString(),
|
||||||
@@ -148,7 +149,7 @@ class AddRecipeFragment : Fragment(R.layout.fragment_add_recipe) {
|
|||||||
.filterNot { it.isBlank() }
|
.filterNot { it.isBlank() }
|
||||||
.toList()
|
.toList()
|
||||||
|
|
||||||
private fun onSavedInputLoaded(request: AddRecipeRequestV0) = with(binding) {
|
private fun onSavedInputLoaded(request: AddRecipeInfo) = with(binding) {
|
||||||
logger.v { "onSavedInputLoaded() called with: request = $request" }
|
logger.v { "onSavedInputLoaded() called with: request = $request" }
|
||||||
recipeNameInput.setText(request.name)
|
recipeNameInput.setText(request.name)
|
||||||
recipeDescriptionInput.setText(request.description)
|
recipeDescriptionInput.setText(request.description)
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package gq.kirmanak.mealient.ui.add
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
|
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||||
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
import gq.kirmanak.mealient.datasource.runCatchingExceptCancel
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -23,8 +23,8 @@ class AddRecipeViewModel @Inject constructor(
|
|||||||
private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED)
|
private val _addRecipeResultChannel = Channel<Boolean>(Channel.UNLIMITED)
|
||||||
val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow()
|
val addRecipeResult: Flow<Boolean> get() = _addRecipeResultChannel.receiveAsFlow()
|
||||||
|
|
||||||
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeRequestV0>(Channel.UNLIMITED)
|
private val _preservedAddRecipeRequestChannel = Channel<AddRecipeInfo>(Channel.UNLIMITED)
|
||||||
val preservedAddRecipeRequest: Flow<AddRecipeRequestV0>
|
val preservedAddRecipeRequest: Flow<AddRecipeInfo>
|
||||||
get() = _preservedAddRecipeRequestChannel.receiveAsFlow()
|
get() = _preservedAddRecipeRequestChannel.receiveAsFlow()
|
||||||
|
|
||||||
fun loadPreservedRequest() {
|
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" }
|
logger.v { "preserve() called with: request = $request" }
|
||||||
viewModelScope.launch { addRecipeRepo.preserve(request) }
|
viewModelScope.launch { addRecipeRepo.preserve(request) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package gq.kirmanak.mealient.extensions
|
package gq.kirmanak.mealient.extensions
|
||||||
|
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeIngredientV0
|
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeInstructionV0
|
import gq.kirmanak.mealient.data.add.AddRecipeIngredientInfo
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
import gq.kirmanak.mealient.data.add.AddRecipeInstructionInfo
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeSettingsV0
|
import gq.kirmanak.mealient.data.add.AddRecipeSettingsInfo
|
||||||
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
|
import gq.kirmanak.mealient.datastore.recipe.AddRecipeDraft
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
@@ -22,42 +22,42 @@ class RemoteToLocalMappingsTest {
|
|||||||
areCommentsDisabled = true,
|
areCommentsDisabled = true,
|
||||||
)
|
)
|
||||||
|
|
||||||
val expected = AddRecipeRequestV0(
|
val expected = AddRecipeInfo(
|
||||||
name = "Recipe name",
|
name = "Recipe name",
|
||||||
description = "Recipe description",
|
description = "Recipe description",
|
||||||
recipeYield = "Recipe yield",
|
recipeYield = "Recipe yield",
|
||||||
recipeIngredient = listOf(
|
recipeIngredient = listOf(
|
||||||
AddRecipeIngredientV0(note = "Recipe ingredient 1"),
|
AddRecipeIngredientInfo(note = "Recipe ingredient 1"),
|
||||||
AddRecipeIngredientV0(note = "Recipe ingredient 2")
|
AddRecipeIngredientInfo(note = "Recipe ingredient 2")
|
||||||
),
|
),
|
||||||
recipeInstructions = listOf(
|
recipeInstructions = listOf(
|
||||||
AddRecipeInstructionV0(text = "Recipe instruction 1"),
|
AddRecipeInstructionInfo(text = "Recipe instruction 1"),
|
||||||
AddRecipeInstructionV0(text = "Recipe instruction 2")
|
AddRecipeInstructionInfo(text = "Recipe instruction 2")
|
||||||
),
|
),
|
||||||
settings = AddRecipeSettingsV0(
|
settings = AddRecipeSettingsInfo(
|
||||||
public = false,
|
public = false,
|
||||||
disableComments = true,
|
disableComments = true,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
assertThat(input.toAddRecipeRequest()).isEqualTo(expected)
|
assertThat(input.toAddRecipeInfo()).isEqualTo(expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when toDraft then fills fields correctly`() {
|
fun `when toDraft then fills fields correctly`() {
|
||||||
val request = AddRecipeRequestV0(
|
val request = AddRecipeInfo(
|
||||||
name = "Recipe name",
|
name = "Recipe name",
|
||||||
description = "Recipe description",
|
description = "Recipe description",
|
||||||
recipeYield = "Recipe yield",
|
recipeYield = "Recipe yield",
|
||||||
recipeIngredient = listOf(
|
recipeIngredient = listOf(
|
||||||
AddRecipeIngredientV0(note = "Recipe ingredient 1"),
|
AddRecipeIngredientInfo(note = "Recipe ingredient 1"),
|
||||||
AddRecipeIngredientV0(note = "Recipe ingredient 2")
|
AddRecipeIngredientInfo(note = "Recipe ingredient 2")
|
||||||
),
|
),
|
||||||
recipeInstructions = listOf(
|
recipeInstructions = listOf(
|
||||||
AddRecipeInstructionV0(text = "Recipe instruction 1"),
|
AddRecipeInstructionInfo(text = "Recipe instruction 1"),
|
||||||
AddRecipeInstructionV0(text = "Recipe instruction 2")
|
AddRecipeInstructionInfo(text = "Recipe instruction 2")
|
||||||
),
|
),
|
||||||
settings = AddRecipeSettingsV0(
|
settings = AddRecipeSettingsInfo(
|
||||||
public = false,
|
public = false,
|
||||||
disableComments = true,
|
disableComments = true,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package gq.kirmanak.mealient.ui.add
|
package gq.kirmanak.mealient.ui.add
|
||||||
|
|
||||||
import com.google.common.truth.Truth.assertThat
|
import com.google.common.truth.Truth.assertThat
|
||||||
|
import gq.kirmanak.mealient.data.add.AddRecipeInfo
|
||||||
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
import gq.kirmanak.mealient.data.add.AddRecipeRepo
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
|
||||||
import gq.kirmanak.mealient.logging.Logger
|
import gq.kirmanak.mealient.logging.Logger
|
||||||
import io.mockk.MockKAnnotations
|
import io.mockk.MockKAnnotations
|
||||||
import io.mockk.coEvery
|
import io.mockk.coEvery
|
||||||
@@ -61,21 +61,21 @@ class AddRecipeViewModelTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when preserve then doesn't update UI`() {
|
fun `when preserve then doesn't update UI`() {
|
||||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequestV0())
|
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeInfo())
|
||||||
subject.preserve(AddRecipeRequestV0())
|
subject.preserve(AddRecipeInfo())
|
||||||
coVerify(inverse = true) { addRecipeRepo.addRecipeRequestFlow }
|
coVerify(inverse = true) { addRecipeRepo.addRecipeRequestFlow }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when preservedAddRecipeRequest without loadPreservedRequest then empty`() = runTest {
|
fun `when preservedAddRecipeRequest without loadPreservedRequest then empty`() = runTest {
|
||||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeRequestV0())
|
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(AddRecipeInfo())
|
||||||
val actual = withTimeoutOrNull(10) { subject.preservedAddRecipeRequest.firstOrNull() }
|
val actual = withTimeoutOrNull(10) { subject.preservedAddRecipeRequest.firstOrNull() }
|
||||||
assertThat(actual).isNull()
|
assertThat(actual).isNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when loadPreservedRequest then updates preservedAddRecipeRequest`() = runTest {
|
fun `when loadPreservedRequest then updates preservedAddRecipeRequest`() = runTest {
|
||||||
val expected = AddRecipeRequestV0()
|
val expected = AddRecipeInfo()
|
||||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
||||||
subject.loadPreservedRequest()
|
subject.loadPreservedRequest()
|
||||||
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
||||||
@@ -83,7 +83,7 @@ class AddRecipeViewModelTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when clear then updates preservedAddRecipeRequest`() = runTest {
|
fun `when clear then updates preservedAddRecipeRequest`() = runTest {
|
||||||
val expected = AddRecipeRequestV0()
|
val expected = AddRecipeInfo()
|
||||||
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
coEvery { addRecipeRepo.addRecipeRequestFlow } returns flowOf(expected)
|
||||||
subject.clear()
|
subject.clear()
|
||||||
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
assertThat(subject.preservedAddRecipeRequest.first()).isSameInstanceAs(expected)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package gq.kirmanak.mealient.datasource.v1
|
package gq.kirmanak.mealient.datasource.v1
|
||||||
|
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
import gq.kirmanak.mealient.datasource.v1.models.AddRecipeRequestV1
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
||||||
@@ -10,7 +10,7 @@ interface MealieDataSourceV1 {
|
|||||||
suspend fun addRecipe(
|
suspend fun addRecipe(
|
||||||
baseUrl: String,
|
baseUrl: String,
|
||||||
token: String?,
|
token: String?,
|
||||||
recipe: AddRecipeRequestV0,
|
recipe: AddRecipeRequestV1,
|
||||||
): String
|
): String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,11 +3,7 @@ package gq.kirmanak.mealient.datasource.v1
|
|||||||
import gq.kirmanak.mealient.datasource.NetworkError
|
import gq.kirmanak.mealient.datasource.NetworkError
|
||||||
import gq.kirmanak.mealient.datasource.NetworkRequestWrapper
|
import gq.kirmanak.mealient.datasource.NetworkRequestWrapper
|
||||||
import gq.kirmanak.mealient.datasource.decode
|
import gq.kirmanak.mealient.datasource.decode
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
import gq.kirmanak.mealient.datasource.v1.models.*
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.ErrorDetailV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeSummaryResponseV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
|
||||||
import kotlinx.serialization.SerializationException
|
import kotlinx.serialization.SerializationException
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import retrofit2.HttpException
|
import retrofit2.HttpException
|
||||||
@@ -26,10 +22,12 @@ class MealieDataSourceV1Impl @Inject constructor(
|
|||||||
override suspend fun addRecipe(
|
override suspend fun addRecipe(
|
||||||
baseUrl: String,
|
baseUrl: String,
|
||||||
token: String?,
|
token: String?,
|
||||||
recipe: AddRecipeRequestV0
|
recipe: AddRecipeRequestV1
|
||||||
): String {
|
): String = networkRequestWrapper.makeCallAndHandleUnauthorized(
|
||||||
TODO("Not yet implemented")
|
block = { service.addRecipe("$baseUrl/api/recipes/create", token, recipe) },
|
||||||
}
|
logMethod = { "addRecipe" },
|
||||||
|
logParameters = { "baseUrl = $baseUrl, token = $token, recipe = $recipe" }
|
||||||
|
)
|
||||||
|
|
||||||
override suspend fun authenticate(
|
override suspend fun authenticate(
|
||||||
baseUrl: String,
|
baseUrl: String,
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package gq.kirmanak.mealient.datasource.v1
|
package gq.kirmanak.mealient.datasource.v1
|
||||||
|
|
||||||
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
import gq.kirmanak.mealient.datasource.DataSourceModule.Companion.AUTHORIZATION_HEADER_NAME
|
||||||
import gq.kirmanak.mealient.datasource.v0.models.AddRecipeRequestV0
|
import gq.kirmanak.mealient.datasource.v1.models.*
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipeResponseV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetRecipesResponseV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.GetTokenResponseV1
|
|
||||||
import gq.kirmanak.mealient.datasource.v1.models.VersionResponseV1
|
|
||||||
import retrofit2.http.*
|
import retrofit2.http.*
|
||||||
|
|
||||||
interface MealieServiceV1 {
|
interface MealieServiceV1 {
|
||||||
@@ -22,7 +18,7 @@ interface MealieServiceV1 {
|
|||||||
suspend fun addRecipe(
|
suspend fun addRecipe(
|
||||||
@Url url: String,
|
@Url url: String,
|
||||||
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
@Header(AUTHORIZATION_HEADER_NAME) token: String?,
|
||||||
@Body addRecipeRequestV0: AddRecipeRequestV0,
|
@Body addRecipeRequestV0: AddRecipeRequestV1,
|
||||||
): String
|
): String
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package gq.kirmanak.mealient.datasource.v1.models
|
||||||
|
|
||||||
|
import kotlinx.serialization.SerialName
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AddRecipeRequestV1(
|
||||||
|
@SerialName("name") val name: String = "",
|
||||||
|
@SerialName("description") val description: String = "",
|
||||||
|
@SerialName("image") val image: String = "",
|
||||||
|
@SerialName("recipeYield") val recipeYield: String = "",
|
||||||
|
@SerialName("recipeIngredient") val recipeIngredient: List<AddRecipeIngredientV1> = emptyList(),
|
||||||
|
@SerialName("recipeInstructions") val recipeInstructions: List<AddRecipeInstructionV1> = emptyList(),
|
||||||
|
@SerialName("slug") val slug: String = "",
|
||||||
|
@SerialName("filePath") val filePath: String = "",
|
||||||
|
@SerialName("tags") val tags: List<String> = emptyList(),
|
||||||
|
@SerialName("categories") val categories: List<String> = emptyList(),
|
||||||
|
@SerialName("notes") val notes: List<AddRecipeNoteV1> = emptyList(),
|
||||||
|
@SerialName("extras") val extras: Map<String, String> = emptyMap(),
|
||||||
|
@SerialName("assets") val assets: List<String> = emptyList(),
|
||||||
|
@SerialName("settings") val settings: AddRecipeSettingsV1 = AddRecipeSettingsV1(),
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AddRecipeIngredientV1(
|
||||||
|
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||||
|
@SerialName("food") val food: String? = null,
|
||||||
|
@SerialName("note") val note: String = "",
|
||||||
|
@SerialName("quantity") val quantity: Int = 1,
|
||||||
|
@SerialName("title") val title: String? = null,
|
||||||
|
@SerialName("unit") val unit: String? = null,
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AddRecipeInstructionV1(
|
||||||
|
@SerialName("title") val title: String = "",
|
||||||
|
@SerialName("text") val text: String = "",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AddRecipeNoteV1(
|
||||||
|
@SerialName("title") val title: String = "",
|
||||||
|
@SerialName("text") val text: String = "",
|
||||||
|
)
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class AddRecipeSettingsV1(
|
||||||
|
@SerialName("disableAmount") val disableAmount: Boolean = true,
|
||||||
|
@SerialName("disableComments") val disableComments: Boolean = false,
|
||||||
|
@SerialName("landscapeView") val landscapeView: Boolean = true,
|
||||||
|
@SerialName("public") val public: Boolean = true,
|
||||||
|
@SerialName("showAssets") val showAssets: Boolean = true,
|
||||||
|
@SerialName("showNutrition") val showNutrition: Boolean = true,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user