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
This commit is contained in:
Jays2Kings 2021-04-19 02:38:14 -04:00
parent a0dba53128
commit d4a754b92d
7 changed files with 48 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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.
*

View File

@ -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<LibraryItem>()
val mangaIds = libraryItems.mapNotNull { (it as? LibraryItem)?.manga?.id }
presenter.rearrangeCategory(catId, mangaIds)
} else {
presenter.sortCategory(catId, sortBy)
}
}
override fun selectAll(position: Int) {

View File

@ -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,

View File

@ -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)
}
}
}

View File

@ -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 {