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 e6e645a2f..790075a1e 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt @@ -96,6 +96,7 @@ fun LibraryScreen( onChangeCurrentPage = { presenter.activeCategory = it }, onMangaClicked = onMangaClicked, onToggleSelection = { presenter.toggleSelection(it) }, + onToggleRangeSelection = { presenter.toggleRangeSelection(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 821be17d3..0dc752a73 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 @@ -40,6 +40,7 @@ fun LibraryContent( onChangeCurrentPage: (Int) -> Unit, onMangaClicked: (Long) -> Unit, onToggleSelection: (LibraryManga) -> Unit, + onToggleRangeSelection: (LibraryManga) -> Unit, onRefresh: (Category?) -> Boolean, onGlobalSearchClicked: () -> Unit, getNumberOfMangaForCategory: @Composable (Long) -> State, @@ -80,7 +81,7 @@ fun LibraryContent( } } val onLongClickManga = { manga: LibraryManga -> - onToggleSelection(manga) + onToggleRangeSelection(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 f6974ac3d..b3e17b893 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 @@ -662,10 +662,38 @@ class LibraryPresenter( state.selection = emptyList() } + private fun removeSelected(mutableList: MutableList, manga: LibraryManga): Boolean { + if (selection.fastAny { it.manga.id == manga.manga.id }) { + return mutableList.remove(manga) + } + return false + } + fun toggleSelection(manga: LibraryManga) { val mutableList = state.selection.toMutableList() - if (selection.fastAny { it.manga.id == manga.manga.id }) { - mutableList.remove(manga) + if (!removeSelected(mutableList, manga)) { + mutableList.add(manga) + } + state.selection = mutableList + } + + /** + * Selects all mangas between and including the given manga and the last pressed manga from the + * same category as the given manga + */ + fun toggleRangeSelection(manga: LibraryManga) { + val mutableList = state.selection.toMutableList() + if (!removeSelected(mutableList, manga) && 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) + } + mutableList.addAll(newList.filterNot { it in selection }) } else { mutableList.add(manga) }