Fix group by, filtering, sorting, and a few others

This commit is contained in:
Jobobby04 2022-07-28 21:44:30 -04:00
parent 7aaff12e2c
commit bba4b991e8
3 changed files with 32 additions and 20 deletions

View File

@ -23,6 +23,7 @@ interface LibraryState {
var hasActiveFilters: Boolean var hasActiveFilters: Boolean
// SY --> // SY -->
val ogCategories: List<Category>
val showSyncExh: Boolean val showSyncExh: Boolean
val showCleanTitles: Boolean val showCleanTitles: Boolean
val showAddToMangadex: Boolean val showAddToMangadex: Boolean
@ -42,6 +43,8 @@ class LibraryStateImpl : LibraryState {
override var hasActiveFilters: Boolean by mutableStateOf(false) override var hasActiveFilters: Boolean by mutableStateOf(false)
// SY --> // SY -->
override var ogCategories: List<Category> by mutableStateOf(emptyList())
override var showSyncExh: Boolean by mutableStateOf(true) override var showSyncExh: Boolean by mutableStateOf(true)
override val showCleanTitles: Boolean by derivedStateOf { override val showCleanTitles: Boolean by derivedStateOf {
selection.any { selection.any {

View File

@ -183,7 +183,7 @@ class LibraryController(
} }
fun showSettingsSheet() { fun showSettingsSheet() {
if (presenter.categories.isNotEmpty()) { if (presenter.categories.isNotEmpty() /* SY --> */ && presenter.groupType == LibraryGroup.BY_DEFAULT /* SY <-- */) {
presenter.categories[presenter.activeCategory].let { category -> presenter.categories[presenter.activeCategory].let { category ->
settingsSheet?.show(category.toDbCategory()) settingsSheet?.show(category.toDbCategory())
} }
@ -248,7 +248,7 @@ class LibraryController(
val mangas = presenter.selection.toList() val mangas = presenter.selection.toList()
// Hide the default category because it has a different behavior than the ones from db. // Hide the default category because it has a different behavior than the ones from db.
val categories = presenter.categories.filter { it.id != 0L } val categories = presenter.ogCategories.filter { it.id != 0L } // SY <--
// Get indexes of the common categories to preselect. // Get indexes of the common categories to preselect.
val common = presenter.getCommonCategories(mangas.mapNotNull { it.toDomainManga() }) val common = presenter.getCommonCategories(mangas.mapNotNull { it.toDomainManga() })

View File

@ -177,7 +177,7 @@ class LibraryPresenter(
// SY --> // SY -->
val favoritesSync = FavoritesSyncHelper(context) val favoritesSync = FavoritesSyncHelper(context)
var groupType = preferences.groupLibraryBy().get() val groupType by preferences.groupLibraryBy().asState()
private val loggedServices by lazy { trackManager.services.filter { it.isLogged } } private val loggedServices by lazy { trackManager.services.filter { it.isLogged } }
@ -248,6 +248,9 @@ class LibraryPresenter(
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.asFlow() .asFlow()
.collectLatest { .collectLatest {
// SY -->
state.categories = it.categories
// SY <--
state.isLoading = false state.isLoading = false
loadedManga = it.mangaMap loadedManga = it.mangaMap
} }
@ -588,29 +591,33 @@ class LibraryPresenter(
} }
} }
state.categories = categories // SY -->
state.ogCategories = categories
// SY <--
Library(categories, libraryManga) Library(categories, libraryManga)
}.asObservable() }.asObservable()
} }
// SY --> // SY -->
private fun applyGrouping(map: LibraryMap, categories: List<Category>): Pair<LibraryMap, List<Category>> { private fun applyGrouping(map: LibraryMap, categories: List<Category>): Pair<LibraryMap, List<Category>> {
groupType = preferences.groupLibraryBy().get() val groupType = preferences.groupLibraryBy().get()
var editedCategories = categories var editedCategories = categories
val items = if (groupType == LibraryGroup.BY_DEFAULT) { val items = when (groupType) {
map LibraryGroup.BY_DEFAULT -> map
} else if (groupType == LibraryGroup.UNGROUPED) { LibraryGroup.UNGROUPED -> {
editedCategories = listOf(Category(0, "All", 0, 0)) editedCategories = listOf(Category(0, "All", 0, 0))
mapOf( mapOf(
0L to map.values.flatten().distinctBy { it.manga.id }, 0L to map.values.flatten().distinctBy { it.manga.id },
) )
} else { }
else -> {
val (items, customCategories) = getGroupedMangaItems( val (items, customCategories) = getGroupedMangaItems(
map.values.flatten().distinctBy { it.manga.id }, map.values.flatten().distinctBy { it.manga.id },
) )
editedCategories = customCategories editedCategories = customCategories
items items
} }
}
return items to editedCategories return items to editedCategories
} }
@ -929,7 +936,7 @@ class LibraryPresenter(
} }
val unfiltered = loadedManga[categoryId] ?: emptyList() val unfiltered = loadedManga[categoryId] ?: emptyList()
return produceState(initialValue = unfiltered, searchQuery) { return produceState(initialValue = unfiltered, unfiltered, searchQuery) {
val query = searchQuery val query = searchQuery
value = withIOContext { value = withIOContext {
if (unfiltered.isNotEmpty() && !query.isNullOrBlank()) { if (unfiltered.isNotEmpty() && !query.isNullOrBlank()) {
@ -1072,7 +1079,9 @@ class LibraryPresenter(
fun getDisplayMode(index: Int): androidx.compose.runtime.State<DisplayModeSetting> { fun getDisplayMode(index: Int): androidx.compose.runtime.State<DisplayModeSetting> {
val category = categories[index] val category = categories[index]
return derivedStateOf { return derivedStateOf {
if (isPerCategory.not() || category.id == 0L) { // SY -->
if (groupType != LibraryGroup.BY_DEFAULT || isPerCategory.not() || (category.id == 0L && groupType == LibraryGroup.BY_DEFAULT)) {
// SY <--
currentDisplayMode currentDisplayMode
} else { } else {
DisplayModeSetting.fromFlag(category.displayMode) DisplayModeSetting.fromFlag(category.displayMode)