From d4a754b92d4ac710a076a0eccc880d2a302cc037 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Mon, 19 Apr 2021 02:38:14 -0400 Subject: [PATCH] Disabling drag and drop in library unless selected as the sort option So the option for DnD now appears in sort list, otherwise categories not in DnD mode are locked --- .../tachiyomi/data/database/models/Category.kt | 7 +++++-- .../kanade/tachiyomi/data/database/models/Manga.kt | 1 + .../tachiyomi/ui/library/LibraryCategoryAdapter.kt | 11 +++++++++++ .../tachiyomi/ui/library/LibraryController.kt | 11 ++++++++++- .../tachiyomi/ui/library/LibraryHeaderHolder.kt | 10 +++++++++- .../eu/kanade/tachiyomi/ui/library/LibraryHolder.kt | 13 +++++++++++-- .../eu/kanade/tachiyomi/ui/library/LibraryItem.kt | 2 +- 7 files changed, 48 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt index a63fa37b55..93430df3ae 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Category.kt @@ -34,7 +34,7 @@ interface Category : Serializable { return ((mangaSort?.minus('a') ?: 0) % 2) != 1 } - fun sortingMode(): Int? = when (mangaSort) { + fun sortingMode(nullAsDND: Boolean = false): Int? = when (mangaSort) { ALPHA_ASC, ALPHA_DSC -> LibrarySort.ALPHA UPDATED_ASC, UPDATED_DSC -> LibrarySort.LATEST_CHAPTER UNREAD_ASC, UNREAD_DSC -> LibrarySort.UNREAD @@ -42,9 +42,12 @@ interface Category : Serializable { TOTAL_ASC, TOTAL_DSC -> LibrarySort.TOTAL DRAG_AND_DROP -> LibrarySort.DRAG_AND_DROP DATE_ADDED_ASC, DATE_ADDED_DSC -> LibrarySort.DATE_ADDED - else -> null + else -> if (nullAsDND && !isDynamic) LibrarySort.DRAG_AND_DROP else null } + val isDragAndDrop + get() = (mangaSort == null || mangaSort == DRAG_AND_DROP) && !isDynamic + fun sortRes(): Int = when (mangaSort) { ALPHA_ASC, ALPHA_DSC -> R.string.title UPDATED_ASC, UPDATED_DSC -> R.string.latest_chapter diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt index 290f392a06..1f72ec81ee 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/models/Manga.kt @@ -30,6 +30,7 @@ interface Manga : SManga { var hide_title: Boolean fun isBlank() = id == Long.MIN_VALUE + fun isHidden() = status == -1 fun setChapterOrder(order: Int) { setFlags(order, SORT_MASK) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index b1b81df851..927d4db647 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -64,6 +64,17 @@ class LibraryCategoryAdapter(val controller: LibraryController) : } } + /** + * Returns the position in the adapter for the given manga. + * + * @param manga the manga to find. + */ + fun findCategoryHeader(catId: Int): LibraryHeaderItem? { + return currentItems.find { + (it is LibraryHeaderItem) && it.category.id == catId + } as LibraryHeaderItem + } + /** * Returns the position in the adapter for the given manga. * 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 a16ee26611..8f159fb62e 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 @@ -1402,7 +1402,16 @@ class LibraryController( } override fun sortCategory(catId: Int, sortBy: Int) { - presenter.sortCategory(catId, sortBy) + val category = presenter.categories.find { it.id == catId } + if (category?.isDynamic == false && ('a' + (sortBy - 1)) == Category.DRAG_AND_DROP) { + val item = adapter.findCategoryHeader(catId) ?: return + val libraryItems = adapter.getSectionItems(item) + .filterIsInstance() + val mangaIds = libraryItems.mapNotNull { (it as? LibraryItem)?.manga?.id } + presenter.rearrangeCategory(catId, mangaIds) + } else { + presenter.sortCategory(catId, sortBy) + } } override fun selectAll(position: Int) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt index be50223739..e48e4a4d92 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHeaderHolder.kt @@ -184,8 +184,16 @@ class LibraryHeaderHolder(val view: View, private val adapter: LibraryCategoryAd R.string.category ) ) + } else { + items.add( + MaterialMenuSheet.MenuSheetItem( + LibrarySort.DRAG_AND_DROP, + R.drawable.ic_swap_vert_24dp, + R.string.drag_and_drop + ) + ) } - val sortingMode = category.sortingMode() + val sortingMode = category.sortingMode(true) val sheet = MaterialMenuSheet( activity, items, diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt index 8edfdba647..03d3ff643a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryHolder.kt @@ -61,8 +61,17 @@ abstract class LibraryHolder( override fun onLongClick(view: View?): Boolean { return if (adapter.isLongPressDragEnabled) { + val manga = (adapter.getItem(flexibleAdapterPosition) as LibraryItem).manga + if (!isDraggable && !manga.isBlank() && !manga.isHidden()) { + adapter.mItemLongClickListener.onItemLongClick(flexibleAdapterPosition) + toggleActivation() + true + } else { + super.onLongClick(view) + false + } + } else { super.onLongClick(view) - false - } else super.onLongClick(view) + } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt index bbd37f5d1e..6b60468173 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryItem.kt @@ -125,7 +125,7 @@ class LibraryItem( * Returns true if this item is draggable. */ override fun isDraggable(): Boolean { - return !manga.isBlank() + return !manga.isBlank() && header.category.isDragAndDrop } override fun isEnabled(): Boolean {