Manual fix for multi-selection

This commit is contained in:
Jobobby04 2022-11-16 14:39:12 -05:00
parent 0752790690
commit f8caecb546

View File

@ -845,9 +845,16 @@ class LibraryPresenter(
}
val items = produceState(initialValue = unfiltered, unfiltered, searchQuery) {
val query = searchQuery
value = withIOContext {
if (unfiltered.isNotEmpty() && !query.isNullOrBlank()) {
filterLibrary(unfiltered, searchQuery)
}
}
return items.value
}
suspend fun filterLibrary(unfiltered: List<LibraryItem>, query: String?): List<LibraryItem> {
return if (unfiltered.isNotEmpty() && !query.isNullOrBlank()) {
// Prepare filter object
val parsedQuery = searchEngine.parseQuery(query)
val mangaWithMetaIds = getIdsOfFavoriteMangaWithMetadata.await()
@ -898,10 +905,6 @@ class LibraryPresenter(
unfiltered
}
}
}
return items.value
}
private fun filterManga(
queries: List<QueryComponent>,
@ -1025,14 +1028,15 @@ class LibraryPresenter(
* same category as the given manga
*/
fun toggleRangeSelection(manga: LibraryManga) {
presenterScope.launchIO {
state.selection = selection.toMutableList().apply {
val lastSelected = lastOrNull()
if (lastSelected?.category != manga.category) {
add(manga)
return@apply
}
val items = loadedManga[manga.category].orEmpty().apply {
if (searchQuery.isNullOrBlank()) toList() else filter { it.filter(searchQuery!!) }
val items = loadedManga[manga.category].orEmpty().run {
filterLibrary(this, searchQuery)
}.fastMap { it.libraryManga }
val lastMangaIndex = items.indexOf(lastSelected)
val curMangaIndex = items.indexOf(manga)
@ -1044,24 +1048,28 @@ class LibraryPresenter(
addAll(newSelections)
}
}
}
fun selectAll(index: Int) {
presenterScope.launchIO {
state.selection = state.selection.toMutableList().apply {
val categoryId = categories.getOrNull(index)?.id ?: -1
val items = loadedManga[categoryId].orEmpty().apply {
if (searchQuery.isNullOrBlank()) toList() else filter { it.filter(searchQuery!!) }
val items = loadedManga[categoryId].orEmpty().run {
filterLibrary(this, searchQuery)
}.fastMap { it.libraryManga }
val selectedIds = fastMap { it.id }
val newSelections = items.filterNot { it.id in selectedIds }
addAll(newSelections)
}
}
}
fun invertSelection(index: Int) {
presenterScope.launchIO {
state.selection = selection.toMutableList().apply {
val categoryId = categories[index].id
val items = loadedManga[categoryId].orEmpty().apply {
if (searchQuery.isNullOrBlank()) toList() else filter { it.filter(searchQuery!!) }
val items = loadedManga[categoryId].orEmpty().run {
filterLibrary(this, searchQuery)
}.fastMap { it.libraryManga }
val selectedIds = fastMap { it.id }
val (toRemove, toAdd) = items.partition { it.id in selectedIds }
@ -1070,6 +1078,7 @@ class LibraryPresenter(
addAll(toAdd)
}
}
}
sealed class Dialog {
data class ChangeCategory(val manga: List<Manga>, val initialSelection: List<CheckboxState<Category>>) : Dialog()