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,20 +109,24 @@ 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,
db.withTransaction { TABLE_FAVOURITE_CATEGORIES,
val res = LinkedHashMap<FavouriteCategory, List<Cover>>() emitInitialState = true,
for (entity in it) { ).mapLatest {
val cat = entity.toFavouriteCategory() db.withTransaction {
res[cat] = db.getFavouritesDao().findCovers( val categories = db.getFavouriteCategoriesDao().findAll()
categoryId = cat.id, val res = LinkedHashMap<FavouriteCategory, List<Cover>>(categories.size)
order = cat.order, for (entity in categories) {
) val cat = entity.toFavouriteCategory()
} res[cat] = db.getFavouritesDao().findCovers(
res categoryId = cat.id,
order = cat.order,
)
} }
res
} }
}.distinctUntilChanged()
} }
suspend fun getAllFavoritesCovers(order: ListSortOrder, limit: Int): List<Cover> { suspend fun getAllFavoritesCovers(order: ListSortOrder, limit: Int): List<Cover> {

Loading…
Cancel
Save