Rename RecipeEntity to RecipeSummaryEntity
This commit is contained in:
@@ -9,7 +9,7 @@ import javax.inject.Singleton
|
||||
|
||||
@Database(
|
||||
version = 1,
|
||||
entities = [CategoryEntity::class, CategoryRecipeEntity::class, TagEntity::class, TagRecipeEntity::class, RecipeEntity::class],
|
||||
entities = [CategoryEntity::class, CategoryRecipeEntity::class, TagEntity::class, TagRecipeEntity::class, RecipeSummaryEntity::class],
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(RoomTypeConverters::class)
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package gq.kirmanak.mealie.data.recipes
|
||||
|
||||
import androidx.paging.Pager
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
|
||||
interface RecipeRepo {
|
||||
fun createPager(): Pager<Int, RecipeEntity>
|
||||
fun createPager(): Pager<Int, RecipeSummaryEntity>
|
||||
|
||||
suspend fun clearLocalData()
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import androidx.room.Index
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
onUpdate = ForeignKey.CASCADE
|
||||
), ForeignKey(
|
||||
entity = RecipeEntity::class,
|
||||
entity = RecipeSummaryEntity::class,
|
||||
parentColumns = ["local_id"],
|
||||
childColumns = ["recipe_id"],
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
|
||||
@@ -14,11 +14,11 @@ interface RecipeDao {
|
||||
@Query("SELECT * FROM categories")
|
||||
suspend fun queryAllCategories(): List<CategoryEntity>
|
||||
|
||||
@Query("SELECT * FROM recipes ORDER BY date_added DESC")
|
||||
fun queryRecipesByPages(): PagingSource<Int, RecipeEntity>
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY date_added DESC")
|
||||
fun queryRecipesByPages(): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertRecipe(recipeEntity: RecipeEntity): Long
|
||||
suspend fun insertRecipe(recipeSummaryEntity: RecipeSummaryEntity): Long
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insertTag(tagEntity: TagEntity): Long
|
||||
@@ -38,7 +38,7 @@ interface RecipeDao {
|
||||
@Insert(onConflict = OnConflictStrategy.IGNORE)
|
||||
suspend fun insertCategoryRecipeEntities(categoryRecipeEntities: Set<CategoryRecipeEntity>)
|
||||
|
||||
@Query("DELETE FROM recipes")
|
||||
@Query("DELETE FROM recipe_summaries")
|
||||
suspend fun removeAllRecipes()
|
||||
|
||||
@Query("DELETE FROM tags")
|
||||
@@ -47,8 +47,8 @@ interface RecipeDao {
|
||||
@Query("DELETE FROM categories")
|
||||
suspend fun removeAllCategories()
|
||||
|
||||
@Query("SELECT * FROM recipes ORDER BY date_updated DESC")
|
||||
suspend fun queryAllRecipes(): List<RecipeEntity>
|
||||
@Query("SELECT * FROM recipe_summaries ORDER BY date_updated DESC")
|
||||
suspend fun queryAllRecipes(): List<RecipeSummaryEntity>
|
||||
|
||||
@Query("SELECT * FROM category_recipe")
|
||||
suspend fun queryAllCategoryRecipes(): List<CategoryRecipeEntity>
|
||||
|
||||
@@ -6,7 +6,7 @@ import gq.kirmanak.mealie.data.recipes.network.GetRecipeSummaryResponse
|
||||
interface RecipeStorage {
|
||||
suspend fun saveRecipes(recipes: List<GetRecipeSummaryResponse>)
|
||||
|
||||
fun queryRecipes(): PagingSource<Int, RecipeEntity>
|
||||
fun queryRecipes(): PagingSource<Int, RecipeSummaryEntity>
|
||||
|
||||
suspend fun refreshAll(recipes: List<GetRecipeSummaryResponse>)
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ class RecipeStorageImpl @Inject constructor(
|
||||
return tagId
|
||||
}
|
||||
|
||||
private fun GetRecipeSummaryResponse.recipeEntity() = RecipeEntity(
|
||||
private fun GetRecipeSummaryResponse.recipeEntity() = RecipeSummaryEntity(
|
||||
remoteId = remoteId,
|
||||
name = name,
|
||||
slug = slug,
|
||||
@@ -87,7 +87,7 @@ class RecipeStorageImpl @Inject constructor(
|
||||
dateUpdated = dateUpdated,
|
||||
)
|
||||
|
||||
override fun queryRecipes(): PagingSource<Int, RecipeEntity> {
|
||||
override fun queryRecipes(): PagingSource<Int, RecipeSummaryEntity> {
|
||||
Timber.v("queryRecipes() called")
|
||||
return recipeDao.queryRecipesByPages()
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import androidx.room.PrimaryKey
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
|
||||
@Entity(tableName = "recipes", indices = [Index(value = ["remote_id"], unique = true)])
|
||||
data class RecipeEntity(
|
||||
@Entity(tableName = "recipe_summaries", indices = [Index(value = ["remote_id"], unique = true)])
|
||||
data class RecipeSummaryEntity(
|
||||
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "local_id") val localId: Long = 0,
|
||||
@ColumnInfo(name = "remote_id") val remoteId: Long,
|
||||
@ColumnInfo(name = "name") val name: String,
|
||||
@@ -14,7 +14,7 @@ import androidx.room.ForeignKey
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
onUpdate = ForeignKey.CASCADE
|
||||
), ForeignKey(
|
||||
entity = RecipeEntity::class,
|
||||
entity = RecipeSummaryEntity::class,
|
||||
parentColumns = ["local_id"],
|
||||
childColumns = ["recipe_id"],
|
||||
onDelete = ForeignKey.CASCADE,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package gq.kirmanak.mealie.data.recipes.impl
|
||||
|
||||
import androidx.paging.PagingSource
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeStorage
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -10,10 +10,10 @@ import javax.inject.Singleton
|
||||
@Singleton
|
||||
class RecipePagingSourceFactory @Inject constructor(
|
||||
private val recipeStorage: RecipeStorage
|
||||
) : () -> PagingSource<Int, RecipeEntity> {
|
||||
private val sources: MutableList<PagingSource<Int, RecipeEntity>> = mutableListOf()
|
||||
) : () -> PagingSource<Int, RecipeSummaryEntity> {
|
||||
private val sources: MutableList<PagingSource<Int, RecipeSummaryEntity>> = mutableListOf()
|
||||
|
||||
override fun invoke(): PagingSource<Int, RecipeEntity> {
|
||||
override fun invoke(): PagingSource<Int, RecipeSummaryEntity> {
|
||||
Timber.v("invoke() called")
|
||||
val newSource = recipeStorage.queryRecipes()
|
||||
sources.add(newSource)
|
||||
|
||||
@@ -4,8 +4,8 @@ import androidx.paging.ExperimentalPagingApi
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeStorage
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -15,7 +15,7 @@ class RecipeRepoImpl @Inject constructor(
|
||||
private val storage: RecipeStorage,
|
||||
private val pagingSourceFactory: RecipePagingSourceFactory
|
||||
) : RecipeRepo {
|
||||
override fun createPager(): Pager<Int, RecipeEntity> {
|
||||
override fun createPager(): Pager<Int, RecipeSummaryEntity> {
|
||||
Timber.v("createPager() called")
|
||||
val pagingConfig = PagingConfig(pageSize = 30, enablePlaceholders = true)
|
||||
return Pager(
|
||||
|
||||
@@ -7,8 +7,8 @@ import androidx.paging.LoadType.PREPEND
|
||||
import androidx.paging.LoadType.REFRESH
|
||||
import androidx.paging.PagingState
|
||||
import androidx.paging.RemoteMediator
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeStorage
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealie.data.recipes.network.RecipeDataSource
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import timber.log.Timber
|
||||
@@ -19,14 +19,14 @@ class RecipesRemoteMediator @Inject constructor(
|
||||
private val storage: RecipeStorage,
|
||||
private val network: RecipeDataSource,
|
||||
private val pagingSourceFactory: RecipePagingSourceFactory,
|
||||
) : RemoteMediator<Int, RecipeEntity>() {
|
||||
) : RemoteMediator<Int, RecipeSummaryEntity>() {
|
||||
|
||||
@VisibleForTesting
|
||||
var lastRequestEnd: Int = 0
|
||||
|
||||
override suspend fun load(
|
||||
loadType: LoadType,
|
||||
state: PagingState<Int, RecipeEntity>
|
||||
state: PagingState<Int, RecipeSummaryEntity>
|
||||
): MediatorResult {
|
||||
Timber.v("load() called with: lastRequestEnd = $lastRequestEnd, loadType = $loadType, state = $state")
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ package gq.kirmanak.mealie.ui.recipes
|
||||
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import gq.kirmanak.mealie.R
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealie.databinding.ViewHolderRecipeBinding
|
||||
|
||||
class RecipeViewHolder(
|
||||
@@ -13,7 +13,7 @@ class RecipeViewHolder(
|
||||
binding.root.resources.getString(R.string.view_holder_recipe_text_placeholder)
|
||||
}
|
||||
|
||||
fun bind(item: RecipeEntity?) {
|
||||
fun bind(item: RecipeSummaryEntity?) {
|
||||
binding.name.text = item?.name ?: loadingPlaceholder
|
||||
recipeViewModel.loadRecipeImage(binding.image, item)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeImageLoader
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeRepo
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -17,9 +17,9 @@ class RecipeViewModel @Inject constructor(
|
||||
) : ViewModel() {
|
||||
val recipeFlow = recipeRepo.createPager().flow
|
||||
|
||||
fun loadRecipeImage(view: ImageView, recipe: RecipeEntity?) {
|
||||
fun loadRecipeImage(view: ImageView, recipeSummary: RecipeSummaryEntity?) {
|
||||
viewModelScope.launch {
|
||||
recipeImageLoader.loadRecipeImage(view, recipe?.slug)
|
||||
recipeImageLoader.loadRecipeImage(view, recipeSummary?.slug)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,13 @@ import android.view.LayoutInflater
|
||||
import android.view.ViewGroup
|
||||
import androidx.paging.PagingDataAdapter
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealie.databinding.ViewHolderRecipeBinding
|
||||
import timber.log.Timber
|
||||
|
||||
class RecipesPagingAdapter(
|
||||
private val viewModel: RecipeViewModel
|
||||
) : PagingDataAdapter<RecipeEntity, RecipeViewHolder>(RecipeDiffCallback) {
|
||||
) : PagingDataAdapter<RecipeSummaryEntity, RecipeViewHolder>(RecipeDiffCallback) {
|
||||
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
|
||||
val item = getItem(position)
|
||||
holder.bind(item)
|
||||
@@ -23,12 +23,18 @@ class RecipesPagingAdapter(
|
||||
return RecipeViewHolder(binding, viewModel)
|
||||
}
|
||||
|
||||
private object RecipeDiffCallback : DiffUtil.ItemCallback<RecipeEntity>() {
|
||||
override fun areItemsTheSame(oldItem: RecipeEntity, newItem: RecipeEntity): Boolean {
|
||||
private object RecipeDiffCallback : DiffUtil.ItemCallback<RecipeSummaryEntity>() {
|
||||
override fun areItemsTheSame(
|
||||
oldItem: RecipeSummaryEntity,
|
||||
newItem: RecipeSummaryEntity
|
||||
): Boolean {
|
||||
return oldItem.remoteId == newItem.remoteId
|
||||
}
|
||||
|
||||
override fun areContentsTheSame(oldItem: RecipeEntity, newItem: RecipeEntity): Boolean {
|
||||
override fun areContentsTheSame(
|
||||
oldItem: RecipeSummaryEntity,
|
||||
newItem: RecipeSummaryEntity
|
||||
): Boolean {
|
||||
return oldItem.name == newItem.name && oldItem.slug == newItem.slug
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package gq.kirmanak.mealie.data.recipes
|
||||
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import gq.kirmanak.mealie.data.recipes.network.GetRecipeSummaryResponse
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
@@ -67,7 +67,7 @@ object RecipeImplTestData {
|
||||
{"detail":"Unauthorized"}
|
||||
"""
|
||||
|
||||
val CAKE_RECIPE_ENTITY = RecipeEntity(
|
||||
val CAKE_RECIPE_ENTITY = RecipeSummaryEntity(
|
||||
localId = 1,
|
||||
remoteId = 1,
|
||||
name = "Cake",
|
||||
@@ -79,7 +79,7 @@ object RecipeImplTestData {
|
||||
dateUpdated = LocalDateTime.parse("2021-11-13T15:30:13")
|
||||
)
|
||||
|
||||
val PORRIDGE_RECIPE_ENTITY = RecipeEntity(
|
||||
val PORRIDGE_RECIPE_ENTITY = RecipeSummaryEntity(
|
||||
localId = 2,
|
||||
remoteId = 2,
|
||||
name = "Porridge",
|
||||
|
||||
@@ -15,7 +15,7 @@ import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.PORRIDGE_RECIPE_ENTITY
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.TEST_RECIPE_ENTITIES
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.enqueueSuccessfulRecipeSummaryResponse
|
||||
import gq.kirmanak.mealie.data.recipes.RecipeImplTestData.enqueueUnsuccessfulRecipeSummaryResponse
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeEntity
|
||||
import gq.kirmanak.mealie.data.recipes.db.RecipeSummaryEntity
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
@@ -127,9 +127,9 @@ class RecipesRemoteMediatorTest : MockServerTest() {
|
||||
}
|
||||
|
||||
private fun pagingState(
|
||||
pages: List<PagingSource.LoadResult.Page<Int, RecipeEntity>> = emptyList(),
|
||||
pages: List<PagingSource.LoadResult.Page<Int, RecipeSummaryEntity>> = emptyList(),
|
||||
anchorPosition: Int? = null
|
||||
): PagingState<Int, RecipeEntity> = PagingState(
|
||||
): PagingState<Int, RecipeSummaryEntity> = PagingState(
|
||||
pages = pages,
|
||||
anchorPosition = anchorPosition,
|
||||
config = pagingConfig,
|
||||
|
||||
Reference in New Issue
Block a user