Skip to content

Commit

Permalink
Long press back to go to previous position (#524)
Browse files Browse the repository at this point in the history
When navigating a grid page, you can long press the back button to go
back to the previous spot.

For example, short pressing back jumps to the top, you can long press
back to go back where you were.

Dev: adds a pre-commit check to check if `DEBUG` is true anywhere since
it should only be when debugging a class.
  • Loading branch information
damontecres authored Jan 21, 2025
1 parent ca12e31 commit dd17b5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ repos:
hooks:
- id: pretty-format-kotlin
args: [ --autofix, --ktlint-version=1.4.1 ]
- repo: local
hooks:
- id: check-debug-enabled
name: Check debug enabled
entry: val DEBUG = true
language: pygrep
files: '.+\.kt'
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class StashDataGridFragment :
private var remoteButtonPaging: Boolean = true

// State
private var previousPosition = -1
private var selectedPosition = -1

private var onBackPressedCallback: OnBackPressedCallback? = null
Expand Down Expand Up @@ -157,7 +158,8 @@ class StashDataGridFragment :

private fun onSelectedOrJump(position: Int) {
if (position != selectedPosition) {
if (DEBUG) Log.v(TAG, "gridOnItemSelected=$position")
if (DEBUG) Log.v(TAG, "newPosition=$position, previousPosition=$previousPosition")
previousPosition = selectedPosition
selectedPosition = position
viewModel.position = position
positionTextView.text = formatNumber(position + 1, false)
Expand Down Expand Up @@ -522,6 +524,23 @@ class StashDataGridFragment :
}
}

override fun onKeyLongPress(
keyCode: Int,
event: KeyEvent,
): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (DEBUG) Log.d(TAG, "Long press back, maybe $selectedPosition=>$previousPosition")
if (previousPosition >= 0 &&
previousPosition != selectedPosition &&
requireActivity().currentFocus is StashImageCardView
) {
jumpTo(previousPosition)
return true
}
}
return super.onKeyLongPress(keyCode, event)
}

override fun onKeyUp(
keyCode: Int,
event: KeyEvent,
Expand Down Expand Up @@ -573,7 +592,7 @@ class StashDataGridFragment :
companion object {
private const val TAG = "StashDataGridFragment"

private const val DEBUG = true
private const val DEBUG = false
}

inner class VerticalGridItemBridgeAdapter : ItemBridgeAdapter() {
Expand Down

0 comments on commit dd17b5e

Please # to comment.