From 985bb44559082360927554cee34409f8e8c1f032 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 5 Apr 2020 15:55:35 -0400 Subject: [PATCH] Add select inverse action --- .../ui/library/LibraryCategoryView.kt | 9 +++++++ .../tachiyomi/ui/library/LibraryController.kt | 25 +++++++++++++++++++ .../ui/manga/chapter/ChaptersController.kt | 12 ++++++++- .../ui/recent/updates/UpdatesController.kt | 10 ++++++++ .../res/drawable/ic_flip_to_back_24dp.xml | 9 +++++++ app/src/main/res/menu/generic_selection.xml | 7 ++++++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/ic_flip_to_back_24dp.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt index c258f43a8..f73f14b15 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryView.kt @@ -133,6 +133,15 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att } controller.invalidateActionMode() } + + subscriptions += controller.selectInverseRelay + .filter { it == category.id } + .subscribe { + adapter.currentItems.forEach { item -> + controller.toggleSelection(item.manga) + } + controller.invalidateActionMode() + } } fun onRecycle() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 806178c87..8ac2e31b0 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -100,6 +100,11 @@ class LibraryController( */ val selectAllRelay: PublishRelay = PublishRelay.create() + /** + * Relay to notify the library's viewpager to select the inverse + */ + val selectInverseRelay: PublishRelay = PublishRelay.create() + /** * Number of manga per row in grid mode. */ @@ -407,6 +412,7 @@ class LibraryController( R.id.action_move_to_category -> showChangeMangaCategoriesDialog() R.id.action_delete -> showDeleteMangaDialog() R.id.action_select_all -> selectAllCategoryManga() + R.id.action_select_inverse -> selectInverseCategoryManga() else -> return false } return true @@ -445,6 +451,19 @@ class LibraryController( } } + /** + * Toggles the current selection state for a given manga. + * + * @param manga the manga whose selection to change. + */ + fun toggleSelection(manga: Manga) { + if (selectedMangas.add(manga)) { + selectionRelay.call(LibrarySelectionEvent.Selected(manga)) + } else if (selectedMangas.remove(manga)) { + selectionRelay.call(LibrarySelectionEvent.Unselected(manga)) + } + } + /** * Move the selected manga to a list of categories. */ @@ -501,6 +520,12 @@ class LibraryController( } } + private fun selectInverseCategoryManga() { + adapter?.categories?.getOrNull(library_pager.currentItem)?.id?.let { + selectInverseRelay.call(it) + } + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == REQUEST_IMAGE_OPEN) { if (data == null || resultCode != Activity.RESULT_OK) return diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt index 3e02954a6..fe6742581 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersController.kt @@ -399,6 +399,8 @@ class ChaptersController : NucleusController(), override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { + R.id.action_select_all -> selectAll() + R.id.action_select_inverse -> selectInverse() R.id.action_download -> downloadChapters(getSelectedChapters()) R.id.action_delete -> showDeleteChaptersConfirmationDialog() R.id.action_bookmark -> bookmarkChapters(getSelectedChapters(), true) @@ -406,7 +408,6 @@ class ChaptersController : NucleusController(), R.id.action_mark_as_read -> markAsRead(getSelectedChapters()) R.id.action_mark_as_unread -> markAsUnread(getSelectedChapters()) R.id.action_mark_previous_as_read -> markPreviousAsRead(getSelectedChapters()) - R.id.action_select_all -> selectAll() else -> return false } return true @@ -436,6 +437,15 @@ class ChaptersController : NucleusController(), actionMode?.invalidate() } + private fun selectInverse() { + val adapter = adapter ?: return + for (i in 0..adapter.itemCount) { + adapter.toggleSelection(i) + } + actionMode?.invalidate() + adapter.notifyDataSetChanged() + } + private fun markAsRead(chapters: List) { presenter.markChaptersRead(chapters, true) if (presenter.preferences.removeAfterMarkedAsRead()) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index ae277acdc..2494381b4 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -338,6 +338,7 @@ class UpdatesController : NucleusController(), override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.action_select_all -> selectAll() + R.id.action_select_inverse -> selectInverse() R.id.action_download -> downloadChapters(getSelectedChapters()) R.id.action_delete -> ConfirmDeleteChaptersDialog(this, getSelectedChapters()) .showDialog(router) @@ -364,4 +365,13 @@ class UpdatesController : NucleusController(), adapter.selectAll() actionMode?.invalidate() } + + private fun selectInverse() { + val adapter = adapter ?: return + for (i in 0..adapter.itemCount) { + adapter.toggleSelection(i) + } + actionMode?.invalidate() + adapter.notifyDataSetChanged() + } } diff --git a/app/src/main/res/drawable/ic_flip_to_back_24dp.xml b/app/src/main/res/drawable/ic_flip_to_back_24dp.xml new file mode 100644 index 000000000..30fe85acd --- /dev/null +++ b/app/src/main/res/drawable/ic_flip_to_back_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/generic_selection.xml b/app/src/main/res/menu/generic_selection.xml index 6da5ebdac..644acf4b3 100644 --- a/app/src/main/res/menu/generic_selection.xml +++ b/app/src/main/res/menu/generic_selection.xml @@ -9,4 +9,11 @@ app:iconTint="?attr/colorOnPrimary" app:showAsAction="ifRoom" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b5e91adf2..85c8d7e19 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,6 +44,7 @@ Search Global search Select all + Select inverse Mark as read Mark as unread Mark previous as read