Remove unused data
This commit is contained in:
@@ -6,7 +6,8 @@ import gq.kirmanak.mealient.data.recipes.network.FullRecipeInfo
|
||||
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.database.recipe.entity.FullRecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.extensions.recipeEntity
|
||||
import gq.kirmanak.mealient.extensions.toRecipeEntity
|
||||
import gq.kirmanak.mealient.extensions.toRecipeIngredientEntity
|
||||
@@ -27,67 +28,10 @@ class RecipeStorageImpl @Inject constructor(
|
||||
) = db.withTransaction {
|
||||
logger.v { "saveRecipes() called with $recipes" }
|
||||
|
||||
val tagEntities = mutableSetOf<TagEntity>()
|
||||
tagEntities.addAll(recipeDao.queryAllTags())
|
||||
|
||||
val categoryEntities = mutableSetOf<CategoryEntity>()
|
||||
categoryEntities.addAll(recipeDao.queryAllCategories())
|
||||
|
||||
val tagRecipeEntities = mutableSetOf<TagRecipeEntity>()
|
||||
val categoryRecipeEntities = mutableSetOf<CategoryRecipeEntity>()
|
||||
|
||||
for (recipe in recipes) {
|
||||
val recipeSummaryEntity = recipe.recipeEntity()
|
||||
recipeDao.insertRecipe(recipeSummaryEntity)
|
||||
|
||||
for (tag in recipe.tags) {
|
||||
val tagId = getIdOrInsert(tagEntities, tag)
|
||||
tagRecipeEntities += TagRecipeEntity(tagId, recipeSummaryEntity.remoteId)
|
||||
}
|
||||
|
||||
for (category in recipe.recipeCategories) {
|
||||
val categoryId = getOrInsert(categoryEntities, category)
|
||||
categoryRecipeEntities += CategoryRecipeEntity(
|
||||
categoryId,
|
||||
recipeSummaryEntity.remoteId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
recipeDao.insertTagRecipeEntities(tagRecipeEntities)
|
||||
recipeDao.insertCategoryRecipeEntities(categoryRecipeEntities)
|
||||
}
|
||||
|
||||
private suspend fun getOrInsert(
|
||||
categoryEntities: MutableSet<CategoryEntity>,
|
||||
category: String
|
||||
): Long {
|
||||
val existingCategory = categoryEntities.find { it.name == category }
|
||||
val categoryId = if (existingCategory == null) {
|
||||
val categoryEntity = CategoryEntity(name = category)
|
||||
val newId = recipeDao.insertCategory(categoryEntity)
|
||||
categoryEntities.add(categoryEntity.copy(localId = newId))
|
||||
newId
|
||||
} else {
|
||||
existingCategory.localId
|
||||
}
|
||||
return categoryId
|
||||
}
|
||||
|
||||
private suspend fun getIdOrInsert(
|
||||
tagEntities: MutableSet<TagEntity>,
|
||||
tag: String
|
||||
): Long {
|
||||
val existingTag = tagEntities.find { it.name == tag }
|
||||
val tagId = if (existingTag == null) {
|
||||
val tagEntity = TagEntity(name = tag)
|
||||
val newId = recipeDao.insertTag(tagEntity)
|
||||
tagEntities.add(tagEntity.copy(localId = newId))
|
||||
newId
|
||||
} else {
|
||||
existingTag.localId
|
||||
}
|
||||
return tagId
|
||||
}
|
||||
|
||||
|
||||
@@ -108,8 +52,6 @@ class RecipeStorageImpl @Inject constructor(
|
||||
logger.v { "clearAllLocalData() called" }
|
||||
db.withTransaction {
|
||||
recipeDao.removeAllRecipes()
|
||||
recipeDao.removeAllCategories()
|
||||
recipeDao.removeAllTags()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,8 @@ 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,
|
||||
val dateAdded: LocalDate,
|
||||
val dateUpdated: LocalDateTime
|
||||
)
|
||||
|
||||
@@ -36,11 +36,7 @@ fun GetRecipeSummaryResponseV0.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,
|
||||
@@ -50,11 +46,7 @@ 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,
|
||||
@@ -64,9 +56,7 @@ fun RecipeSummaryInfo.recipeEntity() = RecipeSummaryEntity(
|
||||
remoteId = remoteId,
|
||||
name = name,
|
||||
slug = slug,
|
||||
image = image,
|
||||
description = description,
|
||||
rating = rating,
|
||||
dateAdded = dateAdded,
|
||||
dateUpdated = dateUpdated,
|
||||
imageId = imageId,
|
||||
|
||||
Reference in New Issue
Block a user