More cleanup for drag and drop

This commit is contained in:
Jobobby04 2020-08-08 16:50:36 -04:00
parent f8c4bbdfd8
commit 0e43234c23
2 changed files with 17 additions and 18 deletions

View File

@ -86,12 +86,14 @@ class LibraryCategoryAdapter(view: LibraryCategoryView, val controller: LibraryC
return currentItems.indexOfFirst { it.manga.id == manga.id } return currentItems.indexOfFirst { it.manga.id == manga.id }
} }
fun canDrag() = (mode != Mode.MULTI || (mode == Mode.MULTI && selectedItemCount == 1)) && searchText.isBlank()
// EXH --> // EXH -->
// Note that we cannot use FlexibleAdapter's built in filtering system as we cannot cancel it // Note that we cannot use FlexibleAdapter's built in filtering system as we cannot cancel it
// (well technically we can cancel it by invoking filterItems again but that doesn't work when // (well technically we can cancel it by invoking filterItems again but that doesn't work when
// we want to perform a no-op filter) // we want to perform a no-op filter)
suspend fun performFilter(scope: CoroutineScope) { suspend fun performFilter(scope: CoroutineScope) {
isLongPressDragEnabled = mode != Mode.MULTI && searchText.isBlank() isLongPressDragEnabled = canDrag()
lastFilterJob?.cancel() lastFilterJob?.cancel()
if (mangas.isNotEmpty() && searchText.isNotBlank()) { if (mangas.isNotEmpty() && searchText.isNotBlank()) {
val savedSearchText = searchText val savedSearchText = searchText

View File

@ -37,7 +37,6 @@ import reactivecircus.flowbinding.recyclerview.scrollStateChanges
import reactivecircus.flowbinding.swiperefreshlayout.refreshes import reactivecircus.flowbinding.swiperefreshlayout.refreshes
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription import rx.subscriptions.CompositeSubscription
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -57,6 +56,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
private val db: DatabaseHelper by injectLazy()
/** /**
* The fragment containing this view. * The fragment containing this view.
*/ */
@ -146,7 +147,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
SelectableAdapter.Mode.SINGLE SelectableAdapter.Mode.SINGLE
} }
// SY --> // SY -->
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
// SY <-- // SY <--
// EXH --> // EXH -->
@ -237,18 +238,16 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
// Get the manga list for this category. // Get the manga list for this category.
// SY --> // SY -->
val sortingMode = preferences.librarySortingMode().get() val sortingMode = preferences.librarySortingMode().get()
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
var mangaForCategory = event.getMangaForCategory(category).orEmpty() var mangaForCategory = event.getMangaForCategory(category).orEmpty()
if (sortingMode == LibrarySort.DRAG_AND_DROP) { if (sortingMode == LibrarySort.DRAG_AND_DROP) {
if (category.name == "Default") { if (category.id == 0) {
category.mangaOrder = preferences.defaultMangaOrder().get().split("/") category.mangaOrder = preferences.defaultMangaOrder().get()
.split("/")
.mapNotNull { it.toLongOrNull() } .mapNotNull { it.toLongOrNull() }
} }
mangaForCategory = mangaForCategory.sortedBy { mangaForCategory = mangaForCategory.sortedBy {
category.mangaOrder.indexOf( category.mangaOrder.indexOf(it.manga.id)
it.manga
.id
)
} }
} }
// SY <-- // SY <--
@ -280,7 +279,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
if (adapter.mode != SelectableAdapter.Mode.MULTI) { if (adapter.mode != SelectableAdapter.Mode.MULTI) {
adapter.mode = SelectableAdapter.Mode.MULTI adapter.mode = SelectableAdapter.Mode.MULTI
// SY --> // SY -->
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
// SY <-- // SY <--
} }
findAndToggleSelection(event.manga) findAndToggleSelection(event.manga)
@ -291,7 +290,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
if (controller.selectedMangas.isEmpty()) { if (controller.selectedMangas.isEmpty()) {
adapter.mode = SelectableAdapter.Mode.SINGLE adapter.mode = SelectableAdapter.Mode.SINGLE
// SY --> // SY -->
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
// SY <-- // SY <--
} }
} }
@ -300,14 +299,12 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
adapter.clearSelection() adapter.clearSelection()
lastClickPosition = -1 lastClickPosition = -1
// SY --> // SY -->
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
// SY <-- // SY <--
} }
} }
} }
private fun canDrag() = ((adapter.mode == SelectableAdapter.Mode.MULTI && adapter.selectedItemCount == 1) || adapter.mode != SelectableAdapter.Mode.MULTI) && adapter.searchText.isBlank()
/** /**
* Toggles the selection for the given manga and updates the view if needed. * Toggles the selection for the given manga and updates the view if needed.
* *
@ -348,7 +345,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
controller.createActionModeIfNeeded() controller.createActionModeIfNeeded()
// SY --> // SY -->
adapter.isLongPressDragEnabled = canDrag() adapter.isLongPressDragEnabled = adapter.canDrag()
// SY <-- // SY <--
when { when {
lastClickPosition == -1 -> setSelection(position) lastClickPosition == -1 -> setSelection(position)
@ -367,10 +364,10 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
controller.invalidateActionMode() controller.invalidateActionMode()
val mangaIds = adapter.currentItems.mapNotNull { it.manga.id } val mangaIds = adapter.currentItems.mapNotNull { it.manga.id }
category.mangaOrder = mangaIds category.mangaOrder = mangaIds
if (category.name == "Default") { if (category.id == 0) {
preferences.defaultMangaOrder().set(mangaIds.joinToString("/")) preferences.defaultMangaOrder().set(mangaIds.joinToString("/"))
} else { } else {
Injekt.get<DatabaseHelper>().insertCategory(category).asRxObservable().subscribe() db.insertCategory(category).asRxObservable().subscribe()
} }
if (preferences.librarySortingMode().get() != LibrarySort.DRAG_AND_DROP) { if (preferences.librarySortingMode().get() != LibrarySort.DRAG_AND_DROP) {
preferences.librarySortingAscending().set(true) preferences.librarySortingAscending().set(true)