From 1bee855766b6ea575d9aeb4c376495ed717dcd7e Mon Sep 17 00:00:00 2001 From: Deltashot Date: Sun, 9 Oct 2022 22:27:44 +0200 Subject: [PATCH] logic and a bit of cleanup --- .../presentation/library/LibraryScreen.kt | 1 + .../library/components/LibraryContent.kt | 3 +- .../tachiyomi/ui/library/LibraryPresenter.kt | 29 ++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt index 23f3507358..4b9505341b 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt @@ -70,6 +70,7 @@ fun LibraryScreen( onChangeCurrentPage = { presenter.activeCategory = it }, onMangaClicked = onMangaClicked, onToggleSelection = { presenter.toggleSelection(it) }, + onToggleMultiSelection = { presenter.toggleMultiSelection(it) }, onRefresh = onClickRefresh, onGlobalSearchClicked = onGlobalSearchClicked, getNumberOfMangaForCategory = { presenter.getMangaCountForCategory(it) }, diff --git a/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt b/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt index 6dce831f73..2cd7a6bcc7 100644 --- a/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt +++ b/app/src/main/java/eu/kanade/presentation/library/components/LibraryContent.kt @@ -45,6 +45,7 @@ fun LibraryContent( onChangeCurrentPage: (Int) -> Unit, onMangaClicked: (Long) -> Unit, onToggleSelection: (LibraryManga) -> Unit, + onToggleMultiSelection: (LibraryManga) -> Unit, onRefresh: (Category?) -> Boolean, onGlobalSearchClicked: () -> Unit, getNumberOfMangaForCategory: @Composable (Long) -> State, @@ -85,7 +86,7 @@ fun LibraryContent( } } val onLongClickManga = { manga: LibraryManga -> - onToggleSelection(manga) + onToggleMultiSelection(manga) } SwipeRefresh( 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 0451d55b03..09d06fed29 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 @@ -667,10 +667,37 @@ class LibraryPresenter( val mutableList = state.selection.toMutableList() if (selection.fastAny { it.manga.id == manga.manga.id }) { mutableList.remove(manga) + state.selection = mutableList } else { mutableList.add(manga) + state.selection = mutableList + } + } + + fun toggleMultiSelection(manga: LibraryManga) { + val mutableList = state.selection.toMutableList() + if (selection.fastAny { it.manga.id == manga.manga.id }) { + mutableList.remove(manga) + state.selection = mutableList + } else if (mutableList.fastAny{ it.category == manga.category }) { + val items = (loadedManga[manga.category] ?: emptyList()).map { it.libraryManga } + + val lastMangaIndex = + items.indexOf(mutableList.findLast { it.category == manga.category }) + val curMangaIndex = items.indexOf(manga) + + val newList = when (lastMangaIndex >= curMangaIndex + 1) { + true -> items.subList(curMangaIndex, lastMangaIndex) + false -> items.subList(lastMangaIndex, curMangaIndex + 1) + } + + state.selection = state.selection.toMutableList().apply { + addAll(newList.filterNot { it in selection }.map { it }) + } + } else { + mutableList.add(manga) + state.selection = mutableList } - state.selection = mutableList } fun selectAll(index: Int) {