Fix library categories

This commit is contained in:
Koitharu
2022-07-21 19:39:33 +03:00
parent 089e3dc209
commit d69f4bbcaf
2 changed files with 25 additions and 15 deletions

View File

@@ -1,13 +1,11 @@
package org.koitharu.kotatsu.library.domain
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.*
import org.koitharu.kotatsu.core.db.MangaDatabase
import org.koitharu.kotatsu.core.db.entity.toManga
import org.koitharu.kotatsu.core.db.entity.toMangaTags
import org.koitharu.kotatsu.core.model.FavouriteCategory
import org.koitharu.kotatsu.favourites.data.FavouriteCategoryEntity
import org.koitharu.kotatsu.favourites.data.toFavouriteCategory
import org.koitharu.kotatsu.parsers.model.Manga
@@ -18,13 +16,22 @@ class LibraryRepository(
fun observeFavourites(): Flow<Map<FavouriteCategory, List<Manga>>> {
return db.favouriteCategoriesDao.observeAll()
.flatMapLatest { categories ->
combine(
categories.map { cat ->
val category = cat.toFavouriteCategory()
db.favouritesDao.observeAll(category.id, category.order)
.map { category to it.map { x -> x.manga.toManga(x.tags.toMangaTags()) } }
},
) { array -> array.toMap() }
val cats = categories.filter { it.isVisibleInLibrary }
if (cats.isEmpty()) {
flowOf(emptyMap())
} else {
observeCategoriesContent(cats)
}
}
}
private fun observeCategoriesContent(
categories: List<FavouriteCategoryEntity>,
) = combine<Pair<FavouriteCategory, List<Manga>>, Map<FavouriteCategory, List<Manga>>>(
categories.map { cat ->
val category = cat.toFavouriteCategory()
db.favouritesDao.observeAll(category.id, category.order)
.map { category to it.map { x -> x.manga.toManga(x.tags.toMangaTags()) } }
},
) { array -> array.toMap() }
}

View File

@@ -27,9 +27,8 @@ fun libraryGroupAD(
selectionController: SectionedSelectionController<LibrarySectionModel>,
listener: LibraryListEventListener,
) = adapterDelegateViewBinding<LibrarySectionModel, ListModel, ItemListGroupBinding>(
{ layoutInflater, parent -> ItemListGroupBinding.inflate(layoutInflater, parent, false) }
{ layoutInflater, parent -> ItemListGroupBinding.inflate(layoutInflater, parent, false) },
) {
val listenerAdapter = object : OnListItemClickListener<Manga>, View.OnClickListener {
override fun onItemClick(item: Manga, view: View) {
listener.onItemClick(item, this@adapterDelegateViewBinding.item, view)
@@ -46,7 +45,7 @@ fun libraryGroupAD(
val adapter = AsyncListDifferDelegationAdapter(
MangaItemDiffCallback(),
mangaGridItemAD(coil, lifecycleOwner, listenerAdapter, sizeResolver)
mangaGridItemAD(coil, lifecycleOwner, listenerAdapter, sizeResolver),
)
binding.recyclerView.setRecycledViewPool(sharedPool)
binding.recyclerView.adapter = adapter
@@ -64,4 +63,8 @@ fun libraryGroupAD(
binding.buttonMore.setTextAndVisible(item.showAllButtonText)
adapter.items = item.items
}
}
onViewRecycled {
adapter.items = emptyList()
}
}