Implement shopping lists screen (#129)
* Initialize shopping lists feature * Start shopping lists screen with Compose * Add icon to shopping list name * Add shopping lists to menu * Set max size for the list * Replace compose-adapter with accompanist * Remove unused fields from shopping lists response * Show list of shopping lists from BE * Hide shopping lists if Mealie is 0.5.6 * Add shopping list item click listener * Create material app theme for Compose * Use shorter names * Load shopping lists by pages and save to db * Make page handling logic match recipes * Add swipe to refresh to shopping lists * Extract SwipeToRefresh Composable * Make LazyPagingColumn generic * Show refresh only when mediator is refreshing * Do not refresh automatically * Allow controlling Activity state from modules * Implement navigating to shopping list screen * Move Compose libraries setup to a plugin * Implement loading full shopping list info * Move Storage classes to database module * Save shopping list items to DB * Use separate names for separate ids * Do only one DB version update * Use unique names for all columns * Display shopping list items * Move OperationUiState to ui module * Subscribe to shopping lists updates * Indicate progress with progress bar * Use strings from resources * Format shopping list item quantities * Hide unit/food/note/quantity if they are not set * Implement updating shopping list item checked state * Remove unnecessary null checks * Disable checkbox when it is being updated * Split shopping list screen into composables * Show items immediately if they are saved * Fix showing "list is empty" before the items * Show Snackbar when error happens * Reduce shopping list items paddings * Remove shopping lists when URL is changed * Add error/empty state handling to shopping lists * Fix empty error state * Fix tests compilation * Add margin between text and button * Add divider between checked and unchecked items * Move divider to the item * Refresh the shopping lists on authentication * Use retry when necessary * Remove excessive logging * Fix pages bounds check * Move FlowExtensionsTest * Update Compose version * Fix showing loading indicator for shopping lists * Add Russian translation * Fix SDK version lint check * Rename parameter to match interface * Add DB migration TODO * Get rid of DB migrations * Do not use pagination with shopping lists * Cleanup after the pagination removal * Load shopping list items * Remove shopping lists storage * Rethrow CancellationException in LoadingHelper * Add pull-to-refresh on shopping list screen * Extract LazyColumnWithLoadingState * Split refresh errors and loading state * Reuse LazyColumnWithLoadingState for shopping list items * Remove paging-compose dependency * Refresh shopping list items on authentication * Disable missing translation lint check * Update Compose and Kotlin versions * Fix order of checked items * Hide useless information from a shopping list item
This commit is contained in:
@@ -1,45 +1,20 @@
|
||||
package gq.kirmanak.mealient.database
|
||||
|
||||
import androidx.room.*
|
||||
import androidx.room.migration.AutoMigrationSpec
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeDao
|
||||
import gq.kirmanak.mealient.database.recipe.entity.*
|
||||
|
||||
@Database(
|
||||
version = 8,
|
||||
version = 10,
|
||||
entities = [
|
||||
RecipeSummaryEntity::class,
|
||||
RecipeEntity::class,
|
||||
RecipeIngredientEntity::class,
|
||||
RecipeInstructionEntity::class,
|
||||
],
|
||||
exportSchema = true,
|
||||
autoMigrations = [
|
||||
AutoMigration(from = 1, to = 2),
|
||||
AutoMigration(from = 3, to = 4),
|
||||
AutoMigration(from = 4, to = 5, spec = AppDb.From4To5Migration::class),
|
||||
AutoMigration(from = 5, to = 6, spec = AppDb.From5To6Migration::class),
|
||||
AutoMigration(from = 6, to = 7),
|
||||
AutoMigration(from = 7, to = 8),
|
||||
]
|
||||
)
|
||||
@TypeConverters(RoomTypeConverters::class)
|
||||
abstract class AppDb : RoomDatabase() {
|
||||
internal abstract class AppDb : RoomDatabase() {
|
||||
|
||||
abstract fun recipeDao(): RecipeDao
|
||||
|
||||
@DeleteColumn(tableName = "recipe_instruction", columnName = "title")
|
||||
@DeleteColumn(tableName = "recipe_ingredient", columnName = "title")
|
||||
@DeleteColumn(tableName = "recipe_ingredient", columnName = "unit")
|
||||
@DeleteColumn(tableName = "recipe_ingredient", columnName = "food")
|
||||
@DeleteColumn(tableName = "recipe_ingredient", columnName = "disable_amount")
|
||||
@DeleteColumn(tableName = "recipe_ingredient", columnName = "quantity")
|
||||
class From4To5Migration : AutoMigrationSpec
|
||||
|
||||
@DeleteColumn(tableName = "recipe_summaries", columnName = "image")
|
||||
@DeleteColumn(tableName = "recipe_summaries", columnName = "rating")
|
||||
@DeleteTable(tableName = "tag_recipe")
|
||||
@DeleteTable(tableName = "tags")
|
||||
@DeleteTable(tableName = "categories")
|
||||
@DeleteTable(tableName = "category_recipe")
|
||||
class From5To6Migration : AutoMigrationSpec
|
||||
}
|
||||
@@ -2,23 +2,35 @@ package gq.kirmanak.mealient.database
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeDao
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeStorage
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeStorageImpl
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
interface DatabaseModule {
|
||||
internal interface DatabaseModule {
|
||||
|
||||
companion object {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun createDb(@ApplicationContext context: Context): AppDb =
|
||||
Room.databaseBuilder(context, AppDb::class.java, "app.db")
|
||||
.fallbackToDestructiveMigrationFrom(2)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRecipeDao(db: AppDb): RecipeDao = db.recipeDao()
|
||||
}
|
||||
|
||||
@Binds
|
||||
@Singleton
|
||||
fun provideRecipeStorage(recipeStorageImpl: RecipeStorageImpl): RecipeStorage
|
||||
}
|
||||
@@ -5,25 +5,30 @@ import androidx.room.*
|
||||
import gq.kirmanak.mealient.database.recipe.entity.*
|
||||
|
||||
@Dao
|
||||
interface RecipeDao {
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY date_added DESC")
|
||||
internal interface RecipeDao {
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY recipe_summaries_date_added DESC")
|
||||
fun queryRecipesByPages(): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
@Query("SELECT * FROM recipe_summaries WHERE recipe_summaries.name LIKE '%' || :query || '%' ORDER BY date_added DESC")
|
||||
@Query("SELECT * FROM recipe_summaries WHERE recipe_summaries_name LIKE '%' || :query || '%' ORDER BY recipe_summaries_date_added DESC")
|
||||
fun queryRecipesByPages(query: String): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
@Transaction
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertRecipes(recipeSummaryEntity: Iterable<RecipeSummaryEntity>)
|
||||
suspend fun insertRecipeSummaries(recipeSummaryEntity: Iterable<RecipeSummaryEntity>)
|
||||
|
||||
@Transaction
|
||||
@Query("DELETE FROM recipe_summaries")
|
||||
suspend fun removeAllRecipes()
|
||||
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY date_updated DESC")
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY recipe_summaries_date_added DESC")
|
||||
suspend fun queryAllRecipes(): List<RecipeSummaryEntity>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertRecipe(recipe: RecipeEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertRecipes(recipe: List<RecipeEntity>)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertRecipeInstructions(instructions: List<RecipeInstructionEntity>)
|
||||
|
||||
@@ -32,19 +37,25 @@ interface RecipeDao {
|
||||
|
||||
@Transaction
|
||||
@SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) // The lint is wrong, the columns are actually used
|
||||
@Query("SELECT * FROM recipe JOIN recipe_summaries ON recipe.remote_id = recipe_summaries.remote_id JOIN recipe_ingredient ON recipe_ingredient.recipe_id = recipe.remote_id JOIN recipe_instruction ON recipe_instruction.recipe_id = recipe.remote_id WHERE recipe.remote_id = :recipeId")
|
||||
suspend fun queryFullRecipeInfo(recipeId: String): FullRecipeEntity?
|
||||
@Query(
|
||||
"SELECT * FROM recipe " +
|
||||
"JOIN recipe_summaries USING(recipe_id) " +
|
||||
"JOIN recipe_ingredient USING(recipe_id) " +
|
||||
"JOIN recipe_instruction USING(recipe_id) " +
|
||||
"WHERE recipe.recipe_id = :recipeId"
|
||||
)
|
||||
suspend fun queryFullRecipeInfo(recipeId: String): RecipeWithSummaryAndIngredientsAndInstructions?
|
||||
|
||||
@Query("DELETE FROM recipe_ingredient WHERE recipe_id = :recipeId")
|
||||
suspend fun deleteRecipeIngredients(recipeId: String)
|
||||
@Query("DELETE FROM recipe_ingredient WHERE recipe_id IN (:recipeIds)")
|
||||
suspend fun deleteRecipeIngredients(vararg recipeIds: String)
|
||||
|
||||
@Query("DELETE FROM recipe_instruction WHERE recipe_id = :recipeId")
|
||||
suspend fun deleteRecipeInstructions(recipeId: String)
|
||||
@Query("DELETE FROM recipe_instruction WHERE recipe_id IN (:recipeIds)")
|
||||
suspend fun deleteRecipeInstructions(vararg recipeIds: String)
|
||||
|
||||
@Query("UPDATE recipe_summaries SET is_favorite = 1 WHERE slug IN (:favorites)")
|
||||
@Query("UPDATE recipe_summaries SET recipe_summaries_is_favorite = 1 WHERE recipe_summaries_slug IN (:favorites)")
|
||||
suspend fun setFavorite(favorites: List<String>)
|
||||
|
||||
@Query("UPDATE recipe_summaries SET is_favorite = 0 WHERE slug NOT IN (:favorites)")
|
||||
@Query("UPDATE recipe_summaries SET recipe_summaries_is_favorite = 0 WHERE recipe_summaries_slug NOT IN (:favorites)")
|
||||
suspend fun setNonFavorite(favorites: List<String>)
|
||||
|
||||
@Delete
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package gq.kirmanak.mealient.database.recipe
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeWithSummaryAndIngredientsAndInstructions
|
||||
|
||||
interface RecipeStorage {
|
||||
suspend fun saveRecipes(recipes: List<RecipeSummaryEntity>)
|
||||
|
||||
fun queryRecipes(query: String?): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
suspend fun refreshAll(recipes: List<RecipeSummaryEntity>)
|
||||
|
||||
suspend fun clearAllLocalData()
|
||||
|
||||
suspend fun saveRecipeInfo(
|
||||
recipe: RecipeEntity,
|
||||
ingredients: List<RecipeIngredientEntity>,
|
||||
instructions: List<RecipeInstructionEntity>
|
||||
)
|
||||
|
||||
suspend fun queryRecipeInfo(recipeId: String): RecipeWithSummaryAndIngredientsAndInstructions?
|
||||
|
||||
suspend fun updateFavoriteRecipes(favorites: List<String>)
|
||||
|
||||
suspend fun deleteRecipe(entity: RecipeSummaryEntity)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package gq.kirmanak.mealient.database.recipe
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.withTransaction
|
||||
import gq.kirmanak.mealient.database.AppDb
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeIngredientEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeInstructionEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealient.database.recipe.entity.RecipeWithSummaryAndIngredientsAndInstructions
|
||||
import gq.kirmanak.mealient.logging.Logger
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
internal class RecipeStorageImpl @Inject constructor(
|
||||
private val db: AppDb,
|
||||
private val logger: Logger,
|
||||
private val recipeDao: RecipeDao,
|
||||
) : RecipeStorage {
|
||||
|
||||
override suspend fun saveRecipes(recipes: List<RecipeSummaryEntity>) {
|
||||
logger.v { "saveRecipes() called with $recipes" }
|
||||
recipeDao.insertRecipeSummaries(recipes)
|
||||
}
|
||||
|
||||
override fun queryRecipes(query: String?): PagingSource<Int, RecipeSummaryEntity> {
|
||||
logger.v { "queryRecipes() called with: query = $query" }
|
||||
return if (query == null) recipeDao.queryRecipesByPages()
|
||||
else recipeDao.queryRecipesByPages(query)
|
||||
}
|
||||
|
||||
override suspend fun refreshAll(recipes: List<RecipeSummaryEntity>) {
|
||||
logger.v { "refreshAll() called with: recipes = $recipes" }
|
||||
db.withTransaction {
|
||||
recipeDao.removeAllRecipes()
|
||||
saveRecipes(recipes)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun clearAllLocalData() {
|
||||
logger.v { "clearAllLocalData() called" }
|
||||
recipeDao.removeAllRecipes()
|
||||
}
|
||||
|
||||
override suspend fun saveRecipeInfo(
|
||||
recipe: RecipeEntity,
|
||||
ingredients: List<RecipeIngredientEntity>,
|
||||
instructions: List<RecipeInstructionEntity>
|
||||
) {
|
||||
logger.v { "saveRecipeInfo() called with: recipe = $recipe" }
|
||||
db.withTransaction {
|
||||
recipeDao.insertRecipe(recipe)
|
||||
|
||||
recipeDao.deleteRecipeIngredients(recipe.remoteId)
|
||||
recipeDao.insertRecipeIngredients(ingredients)
|
||||
|
||||
recipeDao.deleteRecipeInstructions(recipe.remoteId)
|
||||
recipeDao.insertRecipeInstructions(instructions)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun queryRecipeInfo(recipeId: String): RecipeWithSummaryAndIngredientsAndInstructions? {
|
||||
logger.v { "queryRecipeInfo() called with: recipeId = $recipeId" }
|
||||
val fullRecipeInfo = recipeDao.queryFullRecipeInfo(recipeId)
|
||||
logger.v { "queryRecipeInfo() returned: $fullRecipeInfo" }
|
||||
return fullRecipeInfo
|
||||
}
|
||||
|
||||
override suspend fun updateFavoriteRecipes(favorites: List<String>) {
|
||||
logger.v { "updateFavoriteRecipes() called with: favorites = $favorites" }
|
||||
db.withTransaction {
|
||||
recipeDao.setFavorite(favorites)
|
||||
recipeDao.setNonFavorite(favorites)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteRecipe(entity: RecipeSummaryEntity) {
|
||||
logger.v { "deleteRecipeBySlug() called with: entity = $entity" }
|
||||
recipeDao.deleteRecipe(entity)
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "recipe")
|
||||
data class RecipeEntity(
|
||||
@PrimaryKey @ColumnInfo(name = "remote_id") val remoteId: String,
|
||||
@PrimaryKey @ColumnInfo(name = "recipe_id") val remoteId: String,
|
||||
@ColumnInfo(name = "recipe_yield") val recipeYield: String,
|
||||
@ColumnInfo(name = "disable_amounts", defaultValue = "true") val disableAmounts: Boolean,
|
||||
@ColumnInfo(name = "recipe_disable_amounts", defaultValue = "true") val disableAmounts: Boolean,
|
||||
)
|
||||
|
||||
@@ -2,17 +2,28 @@ package gq.kirmanak.mealient.database.recipe.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "recipe_ingredient")
|
||||
@Entity(
|
||||
tableName = "recipe_ingredient",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = RecipeEntity::class,
|
||||
parentColumns = ["recipe_id"],
|
||||
childColumns = ["recipe_id"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
]
|
||||
)
|
||||
data class RecipeIngredientEntity(
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "local_id") val localId: Long = 0,
|
||||
@ColumnInfo(name = "recipe_id") val recipeId: String,
|
||||
@ColumnInfo(name = "note") val note: String,
|
||||
@ColumnInfo(name = "food") val food: String?,
|
||||
@ColumnInfo(name = "unit") val unit: String?,
|
||||
@ColumnInfo(name = "quantity") val quantity: Double?,
|
||||
@ColumnInfo(name = "title") val title: String?,
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "recipe_ingredient_local_id") val localId: Long = 0,
|
||||
@ColumnInfo(name = "recipe_id", index = true) val recipeId: String,
|
||||
@ColumnInfo(name = "recipe_ingredient_note") val note: String,
|
||||
@ColumnInfo(name = "recipe_ingredient_food") val food: String?,
|
||||
@ColumnInfo(name = "recipe_ingredient_unit") val unit: String?,
|
||||
@ColumnInfo(name = "recipe_ingredient_quantity") val quantity: Double?,
|
||||
@ColumnInfo(name = "recipe_ingredient_title") val title: String?,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
@@ -2,13 +2,24 @@ package gq.kirmanak.mealient.database.recipe.entity
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "recipe_instruction")
|
||||
@Entity(
|
||||
tableName = "recipe_instruction",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = RecipeEntity::class,
|
||||
parentColumns = ["recipe_id"],
|
||||
childColumns = ["recipe_id"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
]
|
||||
)
|
||||
data class RecipeInstructionEntity(
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "local_id") val localId: Long = 0,
|
||||
@ColumnInfo(name = "recipe_id") val recipeId: String,
|
||||
@ColumnInfo(name = "text") val text: String,
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "recipe_instruction_local_id") val localId: Long = 0,
|
||||
@ColumnInfo(name = "recipe_id", index = true) val recipeId: String,
|
||||
@ColumnInfo(name = "recipe_instruction_text") val text: String,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
@@ -8,12 +8,15 @@ import kotlinx.datetime.LocalDateTime
|
||||
|
||||
@Entity(tableName = "recipe_summaries")
|
||||
data class RecipeSummaryEntity(
|
||||
@PrimaryKey @ColumnInfo(name = "remote_id") val remoteId: String,
|
||||
@ColumnInfo(name = "name") val name: String,
|
||||
@ColumnInfo(name = "slug") val slug: String,
|
||||
@ColumnInfo(name = "description") val description: String,
|
||||
@ColumnInfo(name = "date_added") val dateAdded: LocalDate,
|
||||
@ColumnInfo(name = "date_updated") val dateUpdated: LocalDateTime,
|
||||
@ColumnInfo(name = "image_id") val imageId: String?,
|
||||
@ColumnInfo(name = "is_favorite", defaultValue = "false") val isFavorite: Boolean,
|
||||
@PrimaryKey @ColumnInfo(name = "recipe_id") val remoteId: String,
|
||||
@ColumnInfo(name = "recipe_summaries_name") val name: String,
|
||||
@ColumnInfo(name = "recipe_summaries_slug") val slug: String,
|
||||
@ColumnInfo(name = "recipe_summaries_description") val description: String,
|
||||
@ColumnInfo(name = "recipe_summaries_date_added") val dateAdded: LocalDate,
|
||||
@ColumnInfo(name = "recipe_summaries_date_updated") val dateUpdated: LocalDateTime,
|
||||
@ColumnInfo(name = "recipe_summaries_image_id") val imageId: String?,
|
||||
@ColumnInfo(
|
||||
name = "recipe_summaries_is_favorite",
|
||||
defaultValue = "false"
|
||||
) val isFavorite: Boolean,
|
||||
)
|
||||
@@ -3,20 +3,20 @@ package gq.kirmanak.mealient.database.recipe.entity
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Relation
|
||||
|
||||
data class FullRecipeEntity(
|
||||
data class RecipeWithSummaryAndIngredientsAndInstructions(
|
||||
@Embedded val recipeEntity: RecipeEntity,
|
||||
@Relation(
|
||||
parentColumn = "remote_id",
|
||||
entityColumn = "remote_id"
|
||||
parentColumn = "recipe_id",
|
||||
entityColumn = "recipe_id"
|
||||
)
|
||||
val recipeSummaryEntity: RecipeSummaryEntity,
|
||||
@Relation(
|
||||
parentColumn = "remote_id",
|
||||
parentColumn = "recipe_id",
|
||||
entityColumn = "recipe_id"
|
||||
)
|
||||
val recipeIngredients: List<RecipeIngredientEntity>,
|
||||
@Relation(
|
||||
parentColumn = "remote_id",
|
||||
parentColumn = "recipe_id",
|
||||
entityColumn = "recipe_id"
|
||||
)
|
||||
val recipeInstructions: List<RecipeInstructionEntity>,
|
||||
@@ -0,0 +1,113 @@
|
||||
package gq.kirmanak.mealient.database
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import dagger.hilt.android.testing.HiltAndroidTest
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeDao
|
||||
import gq.kirmanak.mealient.database.recipe.RecipeStorageImpl
|
||||
import gq.kirmanak.mealient.test.HiltRobolectricTest
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltAndroidTest
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
internal class RecipeStorageImplTest : HiltRobolectricTest() {
|
||||
|
||||
@Inject
|
||||
lateinit var subject: RecipeStorageImpl
|
||||
|
||||
@Inject
|
||||
lateinit var recipeDao: RecipeDao
|
||||
|
||||
@Test
|
||||
fun `when saveRecipes then saves recipes`() = runTest {
|
||||
subject.saveRecipes(TEST_RECIPE_SUMMARY_ENTITIES)
|
||||
val actualTags = recipeDao.queryAllRecipes()
|
||||
assertThat(actualTags).containsExactly(
|
||||
CAKE_RECIPE_SUMMARY_ENTITY,
|
||||
PORRIDGE_RECIPE_SUMMARY_ENTITY
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when refreshAll then old recipes aren't preserved`() = runTest {
|
||||
subject.saveRecipes(TEST_RECIPE_SUMMARY_ENTITIES)
|
||||
subject.refreshAll(listOf(CAKE_RECIPE_SUMMARY_ENTITY))
|
||||
val actual = recipeDao.queryAllRecipes()
|
||||
assertThat(actual).containsExactly(CAKE_RECIPE_SUMMARY_ENTITY)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when clearAllLocalData then recipes aren't preserved`() = runTest {
|
||||
subject.saveRecipes(TEST_RECIPE_SUMMARY_ENTITIES)
|
||||
subject.clearAllLocalData()
|
||||
val actual = recipeDao.queryAllRecipes()
|
||||
assertThat(actual).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when saveRecipeInfo then saves recipe info`() = runTest {
|
||||
subject.saveRecipes(listOf(CAKE_RECIPE_SUMMARY_ENTITY))
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY, CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY, BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY)
|
||||
)
|
||||
val actual = recipeDao.queryFullRecipeInfo("1")
|
||||
assertThat(actual).isEqualTo(FULL_CAKE_INFO_ENTITY)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when saveRecipeInfo with two then saves second`() = runTest {
|
||||
subject.saveRecipes(TEST_RECIPE_SUMMARY_ENTITIES)
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY, CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY, BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
subject.saveRecipeInfo(
|
||||
PORRIDGE_RECIPE_ENTITY_FULL,
|
||||
listOf(PORRIDGE_SUGAR_RECIPE_INGREDIENT_ENTITY, PORRIDGE_MILK_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(PORRIDGE_MIX_RECIPE_INSTRUCTION_ENTITY, PORRIDGE_BOIL_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
val actual = recipeDao.queryFullRecipeInfo("2")
|
||||
assertThat(actual).isEqualTo(FULL_PORRIDGE_INFO_ENTITY)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when saveRecipeInfo twice then overwrites ingredients`() = runTest {
|
||||
subject.saveRecipes(listOf(CAKE_RECIPE_SUMMARY_ENTITY))
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY, CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY, BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY, BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
val actual = recipeDao.queryFullRecipeInfo("1")?.recipeIngredients
|
||||
val expected = listOf(CAKE_BREAD_RECIPE_INGREDIENT_ENTITY.copy(localId = 3))
|
||||
assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `when saveRecipeInfo twice then overwrites instructions`() = runTest {
|
||||
subject.saveRecipes(listOf(CAKE_RECIPE_SUMMARY_ENTITY))
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY, CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY, BAKE_CAKE_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
subject.saveRecipeInfo(
|
||||
CAKE_RECIPE_ENTITY,
|
||||
listOf(CAKE_SUGAR_RECIPE_INGREDIENT_ENTITY, CAKE_BREAD_RECIPE_INGREDIENT_ENTITY),
|
||||
listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY),
|
||||
)
|
||||
val actual = recipeDao.queryFullRecipeInfo("1")?.recipeInstructions
|
||||
val expected = listOf(MIX_CAKE_RECIPE_INSTRUCTION_ENTITY.copy(localId = 3))
|
||||
assertThat(actual).isEqualTo(expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user