From 2cbe946e7e9eae14c035001c9e4eb970ede229a4 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:22:12 +0600 Subject: [PATCH] Rework library selection (#8290) --- .../tachiyomi/ui/library/LibraryPresenter.kt | 27 ++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 4e978ef7b6..424b48f6d1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -10,6 +10,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.res.stringResource import androidx.compose.ui.util.fastAny +import androidx.compose.ui.util.fastMap import com.jakewharton.rxrelay.BehaviorRelay import eu.kanade.core.prefs.CheckboxState import eu.kanade.core.prefs.PreferenceMutableState @@ -610,14 +611,14 @@ class LibraryPresenter( fun toggleRangeSelection(manga: LibraryManga) { state.selection = selection.toMutableList().apply { val lastSelected = lastOrNull() - if (lastSelected == null || lastSelected.category != manga.category) { + if (lastSelected?.category != manga.category) { add(manga) return@apply } - val items = (loadedManga[manga.category] ?: emptyList()).map { it.libraryManga } + val items = loadedManga[manga.category].orEmpty().fastMap { it.libraryManga } val lastMangaIndex = items.indexOf(lastSelected) val curMangaIndex = items.indexOf(manga) - val selectedIds = map { it.id } + val selectedIds = fastMap { it.id } val newSelections = when (lastMangaIndex >= curMangaIndex + 1) { true -> items.subList(curMangaIndex, lastMangaIndex) false -> items.subList(lastMangaIndex, curMangaIndex + 1) @@ -627,17 +628,25 @@ class LibraryPresenter( } fun selectAll(index: Int) { - val category = categories[index] - val items = loadedManga[category.id] ?: emptyList() state.selection = state.selection.toMutableList().apply { - addAll(items.filterNot { it.libraryManga in selection }.map { it.libraryManga }) + val categoryId = categories[index].id + val items = loadedManga[categoryId].orEmpty().fastMap { it.libraryManga } + val selectedIds = fastMap { it.id } + val newSelections = items.filterNot { it.id in selectedIds } + addAll(newSelections) } } fun invertSelection(index: Int) { - val category = categories[index] - val items = (loadedManga[category.id] ?: emptyList()).map { it.libraryManga } - state.selection = items.filterNot { it in selection } + state.selection = selection.toMutableList().apply { + val categoryId = categories[index].id + val items = loadedManga[categoryId].orEmpty().fastMap { it.libraryManga } + val selectedIds = fastMap { it.id } + val (toRemove, toAdd) = items.partition { it.id in selectedIds } + val toRemoveIds = toRemove.fastMap { it.id } + removeAll { it.id in toRemoveIds } + addAll(toAdd) + } } private fun Observable.combineLatest(o2: Observable, combineFn: (T, U) -> R): Observable {