Fix empty categories
This commit is contained in:
@@ -15,17 +15,6 @@ abstract class FavouriteCategoriesDao {
|
||||
@Query("SELECT * FROM favourite_categories WHERE deleted_at = 0 ORDER BY sort_key")
|
||||
abstract fun observeAll(): Flow<List<FavouriteCategoryEntity>>
|
||||
|
||||
@MapInfo(valueColumn = "cover")
|
||||
@Query(
|
||||
"""
|
||||
SELECT favourite_categories.*, manga.cover_url AS cover
|
||||
FROM favourite_categories JOIN manga ON manga.manga_id IN
|
||||
(SELECT manga_id FROM favourites WHERE favourites.category_id == favourite_categories.category_id)
|
||||
ORDER BY favourite_categories.sort_key
|
||||
"""
|
||||
) // FIXME empty categories are ignored
|
||||
abstract fun observeAllWithDetails(): Flow<Map<FavouriteCategoryEntity, List<String>>>
|
||||
|
||||
@Query("SELECT * FROM favourite_categories WHERE category_id = :id AND deleted_at = 0")
|
||||
abstract fun observe(id: Long): Flow<FavouriteCategoryEntity?>
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ abstract class FavouritesDao {
|
||||
"SELECT * FROM favourites LEFT JOIN manga ON favourites.manga_id = manga.manga_id " +
|
||||
"WHERE favourites.deleted_at = 0 GROUP BY favourites.manga_id ORDER BY $orderBy",
|
||||
)
|
||||
return observeAllRaw(query)
|
||||
return observeAllImpl(query)
|
||||
}
|
||||
|
||||
@Transaction
|
||||
@@ -45,7 +45,7 @@ abstract class FavouritesDao {
|
||||
"WHERE category_id = ? AND deleted_at = 0 GROUP BY favourites.manga_id ORDER BY $orderBy",
|
||||
arrayOf<Any>(categoryId),
|
||||
)
|
||||
return observeAllRaw(query)
|
||||
return observeAllImpl(query)
|
||||
}
|
||||
|
||||
@Transaction
|
||||
@@ -61,6 +61,16 @@ abstract class FavouritesDao {
|
||||
)
|
||||
abstract suspend fun findAllManga(categoryId: Int): List<MangaEntity>
|
||||
|
||||
suspend fun findCovers(categoryId: Long, order: SortOrder, limit: Int): List<String> {
|
||||
val orderBy = getOrderBy(order)
|
||||
@Language("RoomSql") val query = SimpleSQLiteQuery(
|
||||
"SELECT m.cover_url FROM favourites AS f LEFT JOIN manga AS m ON f.manga_id = m.manga_id " +
|
||||
"WHERE f.category_id = ? AND deleted_at = 0 ORDER BY $orderBy LIMIT ?",
|
||||
arrayOf<Any>(categoryId, limit),
|
||||
)
|
||||
return findCoversImpl(query)
|
||||
}
|
||||
|
||||
@Query("SELECT * FROM manga WHERE manga_id IN (SELECT manga_id FROM favourites WHERE deleted_at = 0)")
|
||||
abstract suspend fun findAllManga(): List<MangaEntity>
|
||||
|
||||
@@ -103,7 +113,10 @@ abstract class FavouritesDao {
|
||||
|
||||
@Transaction
|
||||
@RawQuery(observedEntities = [FavouriteEntity::class])
|
||||
protected abstract fun observeAllRaw(query: SupportSQLiteQuery): Flow<List<FavouriteManga>>
|
||||
protected abstract fun observeAllImpl(query: SupportSQLiteQuery): Flow<List<FavouriteManga>>
|
||||
|
||||
@RawQuery
|
||||
protected abstract suspend fun findCoversImpl(query: SupportSQLiteQuery): List<String>
|
||||
|
||||
private fun getOrderBy(sortOrder: SortOrder) = when (sortOrder) {
|
||||
SortOrder.RATING -> "rating DESC"
|
||||
@@ -112,4 +125,4 @@ abstract class FavouritesDao {
|
||||
SortOrder.ALPHABETICAL -> "title ASC"
|
||||
else -> throw IllegalArgumentException("Sort order $sortOrder is not supported")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.koitharu.kotatsu.favourites.domain
|
||||
|
||||
import android.util.ArrayMap
|
||||
import androidx.room.withTransaction
|
||||
import kotlinx.coroutines.flow.*
|
||||
import org.koitharu.kotatsu.base.domain.ReversibleHandle
|
||||
@@ -50,10 +51,21 @@ class FavouritesRepository(
|
||||
}.distinctUntilChanged()
|
||||
}
|
||||
|
||||
fun observeCategoriesWithDetails(): Flow<Map<FavouriteCategory, List<String>>> {
|
||||
return db.favouriteCategoriesDao.observeAllWithDetails()
|
||||
fun observeCategoriesWithCovers(coversLimit: Int): Flow<Map<FavouriteCategory, List<String>>> {
|
||||
return db.favouriteCategoriesDao.observeAll()
|
||||
.map {
|
||||
it.mapKeys { (k, _) -> k.toFavouriteCategory() }
|
||||
db.withTransaction {
|
||||
val res = ArrayMap<FavouriteCategory, List<String>>()
|
||||
for (entity in it) {
|
||||
val cat = entity.toFavouriteCategory()
|
||||
res[cat] = db.favouritesDao.findCovers(
|
||||
categoryId = cat.id,
|
||||
order = cat.order,
|
||||
limit = coversLimit,
|
||||
)
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,13 +39,13 @@ class FavouritesCategoriesViewModel(
|
||||
}.asLiveDataDistinct(viewModelScope.coroutineContext + Dispatchers.Default, emptyList())
|
||||
|
||||
val detalizedCategories = combine(
|
||||
repository.observeCategoriesWithDetails(),
|
||||
repository.observeCategoriesWithCovers(coversLimit = 3),
|
||||
isReorder,
|
||||
) { list, reordering ->
|
||||
list.map { (category, covers) ->
|
||||
CategoryListModel(
|
||||
mangaCount = covers.size,
|
||||
covers = covers.take(3),
|
||||
covers = covers,
|
||||
category = category,
|
||||
isReorderMode = reordering,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user