Fix favorite categories observing

master
Koitharu 11 months ago
parent 48a605eeb0
commit e04a877310
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -7,7 +7,10 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITES
import org.koitharu.kotatsu.core.db.TABLE_FAVOURITE_CATEGORIES
import org.koitharu.kotatsu.core.db.entity.toEntities import org.koitharu.kotatsu.core.db.entity.toEntities
import org.koitharu.kotatsu.core.db.entity.toEntity import org.koitharu.kotatsu.core.db.entity.toEntity
import org.koitharu.kotatsu.core.db.entity.toMangaList import org.koitharu.kotatsu.core.db.entity.toMangaList
@ -106,11 +109,15 @@ class FavouritesRepository @Inject constructor(
} }
fun observeCategoriesWithCovers(): Flow<Map<FavouriteCategory, List<Cover>>> { fun observeCategoriesWithCovers(): Flow<Map<FavouriteCategory, List<Cover>>> {
return db.getFavouriteCategoriesDao().observeAll() return db.invalidationTracker.createFlow(
.map { TABLE_FAVOURITES,
TABLE_FAVOURITE_CATEGORIES,
emitInitialState = true,
).mapLatest {
db.withTransaction { db.withTransaction {
val res = LinkedHashMap<FavouriteCategory, List<Cover>>() val categories = db.getFavouriteCategoriesDao().findAll()
for (entity in it) { val res = LinkedHashMap<FavouriteCategory, List<Cover>>(categories.size)
for (entity in categories) {
val cat = entity.toFavouriteCategory() val cat = entity.toFavouriteCategory()
res[cat] = db.getFavouritesDao().findCovers( res[cat] = db.getFavouritesDao().findCovers(
categoryId = cat.id, categoryId = cat.id,
@ -119,7 +126,7 @@ class FavouritesRepository @Inject constructor(
} }
res res
} }
} }.distinctUntilChanged()
} }
suspend fun getAllFavoritesCovers(order: ListSortOrder, limit: Int): List<Cover> { suspend fun getAllFavoritesCovers(order: ListSortOrder, limit: Int): List<Cover> {

Loading…
Cancel
Save