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 4b9505341b..7d01e255b2 100644 --- a/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/library/LibraryScreen.kt @@ -70,7 +70,7 @@ fun LibraryScreen( onChangeCurrentPage = { presenter.activeCategory = it }, onMangaClicked = onMangaClicked, onToggleSelection = { presenter.toggleSelection(it) }, - onToggleMultiSelection = { presenter.toggleMultiSelection(it) }, + onToggleSelectBetween = { presenter.toggleSelectBetween(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 2cd7a6bcc7..44699f5751 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,7 +45,7 @@ fun LibraryContent( onChangeCurrentPage: (Int) -> Unit, onMangaClicked: (Long) -> Unit, onToggleSelection: (LibraryManga) -> Unit, - onToggleMultiSelection: (LibraryManga) -> Unit, + onToggleSelectBetween: (LibraryManga) -> Unit, onRefresh: (Category?) -> Boolean, onGlobalSearchClicked: () -> Unit, getNumberOfMangaForCategory: @Composable (Long) -> State, @@ -86,7 +86,7 @@ fun LibraryContent( } } val onLongClickManga = { manga: LibraryManga -> - onToggleMultiSelection(manga) + onToggleSelectBetween(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 09d06fed29..42834f86b9 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 @@ -663,41 +663,41 @@ class LibraryPresenter( state.selection = emptyList() } - fun toggleSelection(manga: LibraryManga) { - 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 removeSelected(mutableList: MutableList, manga: LibraryManga): Boolean { + if(selection.fastAny { it.manga.id == manga.manga.id }) + return mutableList.remove(manga); + return false } - fun toggleMultiSelection(manga: LibraryManga) { + fun toggleSelection(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 }) { + 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 toggleSelectBetween(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 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 }) - } + mutableList.apply { addAll(newList.filterNot { it in selection }.map { it }) } } else { mutableList.add(manga) - state.selection = mutableList } + state.selection = mutableList } fun selectAll(index: Int) {