From beb17ef442585239c449cf9b2f0be13e1a5d7079 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Thu, 26 Oct 2023 16:13:30 +0300 Subject: [PATCH] Pause autoscroll while touch down --- .../kotatsu/reader/ui/ReaderActivity.kt | 1 + .../koitharu/kotatsu/reader/ui/ScrollTimer.kt | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) 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 e6dadab56..c3b7fad89 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 @@ -249,6 +249,7 @@ class ReaderActivity : override fun dispatchTouchEvent(ev: MotionEvent): Boolean { touchHelper.dispatchTouchEvent(ev) + scrollTimer.onTouchEvent(ev) return super.dispatchTouchEvent(ev) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScrollTimer.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScrollTimer.kt index 91c0e826d..ec09b8adb 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScrollTimer.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScrollTimer.kt @@ -1,5 +1,6 @@ package org.koitharu.kotatsu.reader.ui +import android.view.MotionEvent import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import dagger.assisted.Assisted @@ -8,11 +9,14 @@ import dagger.assisted.AssistedInject import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.isActive import kotlinx.coroutines.launch +import kotlinx.coroutines.yield import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.observeAsFlow import kotlin.math.roundToLong @@ -33,6 +37,7 @@ class ScrollTimer @AssistedInject constructor( private var delayMs: Long = 10L private var pageSwitchDelay: Long = 100L private var resumeAt = 0L + private var isTouchDown = MutableStateFlow(false) var isEnabled: Boolean = false set(value) { @@ -55,6 +60,19 @@ class ScrollTimer @AssistedInject constructor( resumeAt = System.currentTimeMillis() + INTERACTION_SKIP_MS } + fun onTouchEvent(event: MotionEvent) { + when (event.actionMasked) { + MotionEvent.ACTION_DOWN -> { + isTouchDown.value = true + } + + MotionEvent.ACTION_UP, + MotionEvent.ACTION_CANCEL -> { + isTouchDown.value = false + } + } + } + private fun onSpeedChanged(speed: Float) { if (speed <= 0f) { delayMs = 0L @@ -108,12 +126,18 @@ class ScrollTimer @AssistedInject constructor( } private fun isPaused(): Boolean { - return resumeAt > System.currentTimeMillis() + return isTouchDown.value || resumeAt > System.currentTimeMillis() } private suspend fun delayUntilResumed() { while (isPaused()) { - delay(resumeAt - System.currentTimeMillis()) + val delayTime = resumeAt - System.currentTimeMillis() + if (delayTime > 0) { + delay(delayTime) + } else { + yield() + } + isTouchDown.first { !it } } }