Remove unused data

This commit is contained in:
Kirill Kamakin
2022-10-30 13:01:06 +01:00
parent 31ccf8822d
commit 7d64215b63
15 changed files with 177 additions and 322 deletions

View File

@@ -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()
}
}

View File

@@ -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
)

View File

@@ -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,

View File

@@ -3,10 +3,6 @@ package gq.kirmanak.mealient.data.recipes.db
import com.google.common.truth.Truth.assertThat
import dagger.hilt.android.testing.HiltAndroidTest
import gq.kirmanak.mealient.database.AppDb
import gq.kirmanak.mealient.database.recipe.entity.CategoryEntity
import gq.kirmanak.mealient.database.recipe.entity.CategoryRecipeEntity
import gq.kirmanak.mealient.database.recipe.entity.TagEntity
import gq.kirmanak.mealient.database.recipe.entity.TagRecipeEntity
import gq.kirmanak.mealient.test.HiltRobolectricTest
import gq.kirmanak.mealient.test.RecipeImplTestData.BREAD_INGREDIENT
import gq.kirmanak.mealient.test.RecipeImplTestData.CAKE_BREAD_RECIPE_INGREDIENT_ENTITY
@@ -36,28 +32,6 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
@Inject
lateinit var appDb: AppDb
@Test
fun `when saveRecipes then saves tags`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
val actualTags = appDb.recipeDao().queryAllTags()
assertThat(actualTags).containsExactly(
TagEntity(localId = 1, name = "gluten"),
TagEntity(localId = 2, name = "allergic"),
TagEntity(localId = 3, name = "milk")
)
}
@Test
fun `when saveRecipes then saves categories`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
val actual = appDb.recipeDao().queryAllCategories()
assertThat(actual).containsExactly(
CategoryEntity(localId = 1, name = "dessert"),
CategoryEntity(localId = 2, name = "tasty"),
CategoryEntity(localId = 3, name = "porridge")
)
}
@Test
fun `when saveRecipes then saves recipes`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
@@ -68,30 +42,6 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
)
}
@Test
fun `when saveRecipes then saves category recipes`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
val actual = appDb.recipeDao().queryAllCategoryRecipes()
assertThat(actual).containsExactly(
CategoryRecipeEntity(categoryId = 1, recipeId = "1"),
CategoryRecipeEntity(categoryId = 2, recipeId = "1"),
CategoryRecipeEntity(categoryId = 3, recipeId = "2"),
CategoryRecipeEntity(categoryId = 2, recipeId = "2")
)
}
@Test
fun `when saveRecipes then saves tag recipes`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
val actual = appDb.recipeDao().queryAllTagRecipes()
assertThat(actual).containsExactly(
TagRecipeEntity(tagId = 1, recipeId = "1"),
TagRecipeEntity(tagId = 2, recipeId = "1"),
TagRecipeEntity(tagId = 3, recipeId = "2"),
TagRecipeEntity(tagId = 1, recipeId = "2"),
)
}
@Test
fun `when refreshAll then old recipes aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
@@ -100,28 +50,6 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
assertThat(actual).containsExactly(CAKE_RECIPE_SUMMARY_ENTITY)
}
@Test
fun `when refreshAll then old category recipes aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
subject.refreshAll(listOf(RECIPE_SUMMARY_CAKE))
val actual = appDb.recipeDao().queryAllCategoryRecipes()
assertThat(actual).containsExactly(
CategoryRecipeEntity(categoryId = 1, recipeId = "1"),
CategoryRecipeEntity(categoryId = 2, recipeId = "1"),
)
}
@Test
fun `when refreshAll then old tag recipes aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
subject.refreshAll(listOf(RECIPE_SUMMARY_CAKE))
val actual = appDb.recipeDao().queryAllTagRecipes()
assertThat(actual).containsExactly(
TagRecipeEntity(tagId = 1, recipeId = "1"),
TagRecipeEntity(tagId = 2, recipeId = "1"),
)
}
@Test
fun `when clearAllLocalData then recipes aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
@@ -130,22 +58,6 @@ class RecipeStorageImplTest : HiltRobolectricTest() {
assertThat(actual).isEmpty()
}
@Test
fun `when clearAllLocalData then categories aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
subject.clearAllLocalData()
val actual = appDb.recipeDao().queryAllCategories()
assertThat(actual).isEmpty()
}
@Test
fun `when clearAllLocalData then tags aren't preserved`() = runTest {
subject.saveRecipes(TEST_RECIPE_SUMMARIES)
subject.clearAllLocalData()
val actual = appDb.recipeDao().queryAllTags()
assertThat(actual).isEmpty()
}
@Test
fun `when saveRecipeInfo then saves recipe info`() = runTest {
subject.saveRecipes(listOf(RECIPE_SUMMARY_CAKE))

View File

@@ -13,11 +13,7 @@ object RecipeImplTestData {
remoteId = "1",
name = "Cake",
slug = "cake",
image = "86",
description = "A tasty cake",
recipeCategories = listOf("dessert", "tasty"),
tags = listOf("gluten", "allergic"),
rating = 4,
dateAdded = LocalDate.parse("2021-11-13"),
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"),
imageId = "cake",
@@ -27,11 +23,7 @@ object RecipeImplTestData {
remoteId = "2",
name = "Porridge",
slug = "porridge",
image = "89",
description = "A tasty porridge",
recipeCategories = listOf("porridge", "tasty"),
tags = listOf("gluten", "milk"),
rating = 5,
dateAdded = LocalDate.parse("2021-11-12"),
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
imageId = "porridge",
@@ -43,9 +35,7 @@ object RecipeImplTestData {
remoteId = "1",
name = "Cake",
slug = "cake",
image = "86",
description = "A tasty cake",
rating = 4,
dateAdded = LocalDate.parse("2021-11-13"),
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13"),
imageId = "cake",
@@ -55,9 +45,7 @@ object RecipeImplTestData {
remoteId = "2",
name = "Porridge",
slug = "porridge",
image = "89",
description = "A tasty porridge",
rating = 5,
dateAdded = LocalDate.parse("2021-11-12"),
dateUpdated = LocalDateTime.parse("2021-10-13T17:35:23"),
imageId = "porridge",