Fix library categories

pull/189/head
Koitharu 4 years ago
parent 089e3dc209
commit d69f4bbcaf
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

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

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