Implement long hold selection for Manga Chapters and library

Co-Authored-By: zhuoyang <zhuoyang@users.noreply.github.com>
This commit is contained in:
Jay 2019-12-05 21:50:36 -08:00
parent 73b1ee5e15
commit b73ae950ad
2 changed files with 50 additions and 2 deletions

View File

@ -63,6 +63,8 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
private var lastTouchUpY = 0f private var lastTouchUpY = 0f
private var lastClickPosition = -1
fun onCreate(controller: LibraryController) { fun onCreate(controller: LibraryController) {
this.controller = controller this.controller = controller
@ -188,6 +190,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
} }
is LibrarySelectionEvent.Unselected -> { is LibrarySelectionEvent.Unselected -> {
findAndToggleSelection(event.manga) findAndToggleSelection(event.manga)
if (adapter.indexOf(event.manga) != -1) lastClickPosition = -1
if (controller.selectedMangas.isEmpty()) { if (controller.selectedMangas.isEmpty()) {
adapter.mode = SelectableAdapter.Mode.SINGLE adapter.mode = SelectableAdapter.Mode.SINGLE
} }
@ -195,6 +198,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
is LibrarySelectionEvent.Cleared -> { is LibrarySelectionEvent.Cleared -> {
adapter.mode = SelectableAdapter.Mode.SINGLE adapter.mode = SelectableAdapter.Mode.SINGLE
adapter.clearSelection() adapter.clearSelection()
lastClickPosition = -1
} }
} }
} }
@ -222,6 +226,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
// If the action mode is created and the position is valid, toggle the selection. // If the action mode is created and the position is valid, toggle the selection.
val item = adapter.getItem(position) ?: return false val item = adapter.getItem(position) ?: return false
if (adapter.mode == SelectableAdapter.Mode.MULTI) { if (adapter.mode == SelectableAdapter.Mode.MULTI) {
lastClickPosition = position
toggleSelection(position) toggleSelection(position)
return true return true
} else { } else {
@ -244,7 +249,15 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
*/ */
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
controller.createActionModeIfNeeded() controller.createActionModeIfNeeded()
toggleSelection(position) when {
lastClickPosition == -1 -> setSelection(position)
lastClickPosition > position -> for (i in position until lastClickPosition)
setSelection(i)
lastClickPosition < position -> for (i in lastClickPosition + 1..position)
setSelection(i)
else -> setSelection(position)
}
lastClickPosition = position
} }
/** /**
@ -268,4 +281,17 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
controller.invalidateActionMode() controller.invalidateActionMode()
} }
/**
* Tells the presenter to set the selection for the given position.
*
* @param position the position to toggle.
*/
private fun setSelection(position: Int) {
val item = adapter.getItem(position) ?: return
controller.setSelection(item.manga, true)
controller.invalidateActionMode()
}
} }

View File

@ -70,6 +70,8 @@ class ChaptersController() : NucleusController<ChaptersPresenter>(),
*/ */
private val selectedItems = mutableSetOf<ChapterItem>() private val selectedItems = mutableSetOf<ChapterItem>()
private var lastClickPosition = -1
init { init {
setHasOptionsMenu(true) setHasOptionsMenu(true)
setOptionsMenuHidden(true) setOptionsMenuHidden(true)
@ -294,6 +296,7 @@ class ChaptersController() : NucleusController<ChaptersPresenter>(),
val adapter = adapter ?: return false val adapter = adapter ?: return false
val item = adapter.getItem(position) ?: return false val item = adapter.getItem(position) ?: return false
if (actionMode != null && adapter.mode == SelectableAdapter.Mode.MULTI) { if (actionMode != null && adapter.mode == SelectableAdapter.Mode.MULTI) {
lastClickPosition = position
toggleSelection(position) toggleSelection(position)
return true return true
} else { } else {
@ -304,7 +307,15 @@ class ChaptersController() : NucleusController<ChaptersPresenter>(),
override fun onItemLongClick(position: Int) { override fun onItemLongClick(position: Int) {
createActionModeIfNeeded() createActionModeIfNeeded()
toggleSelection(position) when {
lastClickPosition == -1 -> setSelection(position)
lastClickPosition > position -> for (i in position until lastClickPosition)
setSelection(i)
lastClickPosition < position -> for (i in lastClickPosition + 1..position)
setSelection(i)
else -> setSelection(position)
}
lastClickPosition = position
} }
// SELECTIONS & ACTION MODE // SELECTIONS & ACTION MODE
@ -321,6 +332,16 @@ class ChaptersController() : NucleusController<ChaptersPresenter>(),
actionMode?.invalidate() actionMode?.invalidate()
} }
private fun setSelection(position: Int) {
val adapter = adapter ?: return
val item = adapter.getItem(position) ?: return
if (!adapter.isSelected(position)) {
adapter.toggleSelection(position)
selectedItems.add(item)
actionMode?.invalidate()
}
}
private fun getSelectedChapters(): List<ChapterItem> { private fun getSelectedChapters(): List<ChapterItem> {
val adapter = adapter ?: return emptyList() val adapter = adapter ?: return emptyList()
return adapter.selectedPositions.mapNotNull { adapter.getItem(it) } return adapter.selectedPositions.mapNotNull { adapter.getItem(it) }
@ -333,6 +354,7 @@ class ChaptersController() : NucleusController<ChaptersPresenter>(),
} }
private fun destroyActionModeIfNeeded() { private fun destroyActionModeIfNeeded() {
lastClickPosition = -1
actionMode?.finish() actionMode?.finish()
} }