This commit is contained in:
Horis
2025-03-21 17:37:23 +08:00
parent 5983d1df46
commit 8218577bff
3 changed files with 26 additions and 28 deletions
@@ -119,7 +119,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
private var justInitData: Boolean = false private var justInitData: Boolean = false
private var syncDialog: AlertDialog? = null private var syncDialog: AlertDialog? = null
private val mScrollTimer by lazy { private val mScrollTimer by lazy {
ScrollTimer(lifecycleScope, this).apply { ScrollTimer(this, binding.mRecyclerManga).apply {
setSpeed(AppConfig.mangaAutoPageSpeed) setSpeed(AppConfig.mangaAutoPageSpeed)
} }
} }
@@ -1,46 +1,44 @@
package io.legado.app.ui.book.manga.recyclerview package io.legado.app.ui.book.manga.recyclerview
import kotlinx.coroutines.CoroutineScope import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.Job import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
class ScrollTimer( class ScrollTimer(
private val coroutineScope: CoroutineScope, private val callback: ScrollCallback,
private val callback: ScrollCallback private val recyclerView: RecyclerView
) { ) : RecyclerView.OnScrollListener() {
private var job: Job? = null
private var delayMs: Long = 20L
private var distance = 1 private var distance = 1
var isEnabled: Boolean = false var isEnabled: Boolean = false
set(value) { set(value) {
if (field != value) { if (field != value) {
field = value field = value
restartJob() if (value) {
recyclerView.addOnScrollListener(this)
startScroll()
} else {
recyclerView.removeOnScrollListener(this)
}
} }
} }
override fun onScrollStateChanged(
recyclerView: RecyclerView,
newState: Int
) {
if (newState == SCROLL_STATE_IDLE) {
startScroll()
}
}
fun setSpeed(distance: Int) { fun setSpeed(distance: Int) {
this.distance = distance this.distance = distance
restartJob()
} }
private fun restartJob() { private fun startScroll() {
job?.cancel() callback.scrollBy(distance)
if (!isEnabled || delayMs == 0L) {
job = null
return
}
job = coroutineScope.launch {
while (isActive) {
callback.scrollBy(distance)
delay(delayMs)
}
}
} }
interface ScrollCallback{ interface ScrollCallback {
fun scrollBy(distance:Int) fun scrollBy(distance: Int)
} }
} }
@@ -233,7 +233,7 @@ class MangaMenu @JvmOverloads constructor(
interface CallBack { interface CallBack {
fun openBookInfoActivity() fun openBookInfoActivity()
fun upSystemUiVisibility(menuIsVisible: Boolean) fun upSystemUiVisibility(menuIsVisible: Boolean)
fun skipToPage(pos: Int) fun skipToPage(index: Int)
} }
} }