From ccdebf6789248a6c198fa5658d897b2fc419c2eb Mon Sep 17 00:00:00 2001 From: Zakhar Timoshenko Date: Sat, 27 Jan 2024 13:19:22 +0300 Subject: [PATCH] Initial support of long press in reader --- .../kotatsu/core/util/GridTouchHelper.kt | 19 +++++++++++++++++++ .../kotatsu/reader/ui/ReaderActivity.kt | 13 +++++++++++++ .../reader/ui/ReaderControlDelegate.kt | 11 +++++++++++ 3 files changed, 43 insertions(+) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/GridTouchHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/GridTouchHelper.kt index 6608c719e..a5b49a0c0 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/GridTouchHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/GridTouchHelper.kt @@ -52,6 +52,23 @@ class GridTouchHelper( return true } + override fun onLongPress(event: MotionEvent) { + super.onLongPress(event) + val xIndex = (event.rawX * 2f / width).roundToInt() + val yIndex = (event.rawY * 2f / height).roundToInt() + listener.onGridLongTouch( + when(xIndex) { + 1 -> { + when (yIndex) { + 1 -> AREA_CENTER + else -> -1 + } + } + else -> -1 + } + ) + } + companion object { const val AREA_CENTER = 1 @@ -65,6 +82,8 @@ class GridTouchHelper( fun onGridTouch(area: Int) + fun onGridLongTouch(area: Int) + fun onProcessTouch(rawX: Int, rawY: Int): Boolean } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt index d65fc2062..a1c47e6f0 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt @@ -24,6 +24,7 @@ import androidx.core.view.updateLayoutParams import androidx.core.view.updatePadding import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers @@ -43,6 +44,7 @@ import org.koitharu.kotatsu.core.ui.widgets.ZoomControl import org.koitharu.kotatsu.core.util.GridTouchHelper import org.koitharu.kotatsu.core.util.IdlingDetector import org.koitharu.kotatsu.core.util.ShareHelper +import org.koitharu.kotatsu.core.util.ext.DIALOG_THEME_CENTERED import org.koitharu.kotatsu.core.util.ext.hasGlobalPoint import org.koitharu.kotatsu.core.util.ext.isAnimationsEnabled import org.koitharu.kotatsu.core.util.ext.isRtl @@ -204,6 +206,10 @@ class ReaderActivity : controlDelegate.onGridTouch(area, viewBinding.container) } + override fun onGridLongTouch(area: Int) { + controlDelegate.onGridLongTouch(area, viewBinding.container) + } + override fun onProcessTouch(rawX: Int, rawY: Int): Boolean { return if ( rawX <= gestureInsets.left || @@ -336,6 +342,13 @@ class ReaderActivity : setUiIsVisible(!viewBinding.appbarTop.isVisible) } + override fun viewDialog() { + MaterialAlertDialogBuilder(this, DIALOG_THEME_CENTERED) + .setMessage("Called dialog on long press") + .setPositiveButton(R.string.got_it, null) + .show() + } + override fun isReaderResumed(): Boolean { val reader = readerManager.currentReader ?: return false return reader.isResumed && supportFragmentManager.fragments.lastOrNull() === reader diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderControlDelegate.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderControlDelegate.kt index a936c8bb2..d8fc27512 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderControlDelegate.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderControlDelegate.kt @@ -69,6 +69,15 @@ class ReaderControlDelegate( } } + fun onGridLongTouch(area: Int, view: View) { + when (area) { + GridTouchHelper.AREA_CENTER -> { + listener.viewDialog() + view.playSoundEffect(SoundEffectConstants.CLICK) + } + } + } + fun onKeyDown(keyCode: Int, @Suppress("UNUSED_PARAMETER") event: KeyEvent?): Boolean = when (keyCode) { KeyEvent.KEYCODE_VOLUME_UP -> if (isVolumeKeysSwitchEnabled) { listener.switchPageBy(-1) @@ -155,6 +164,8 @@ class ReaderControlDelegate( fun switchPageBy(delta: Int) + fun viewDialog() + fun scrollBy(delta: Int, smooth: Boolean): Boolean fun toggleUiVisibility()