mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
@@ -58,7 +58,6 @@ import io.legado.app.ui.widget.recycler.LoadMoreView
|
||||
import io.legado.app.utils.GSON
|
||||
import io.legado.app.utils.NetworkUtils
|
||||
import io.legado.app.utils.StartActivityContract
|
||||
import io.legado.app.utils.buildMainHandler
|
||||
import io.legado.app.utils.fastBinarySearch
|
||||
import io.legado.app.utils.findCenterViewPosition
|
||||
import io.legado.app.utils.fromJsonObject
|
||||
@@ -101,13 +100,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
private lateinit var mMangaFooterConfig: MangaFooterConfig
|
||||
private val mLabelBuilder by lazy { StringBuilder() }
|
||||
|
||||
private val autoScrollHandler = buildMainHandler()
|
||||
private val autoScrollRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
scrollToNext()
|
||||
autoScrollHandler.postDelayed(this, mMangaAutoPageSpeed.times(1000L)) // 每3秒滑动一次
|
||||
}
|
||||
}
|
||||
|
||||
private var mMenu: Menu? = null
|
||||
|
||||
private var mRecyclerViewPreloader: RecyclerViewPreloader<Any>? = null
|
||||
@@ -119,7 +112,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
private var justInitData: Boolean = false
|
||||
private var syncDialog: AlertDialog? = null
|
||||
private val mScrollTimer by lazy {
|
||||
ScrollTimer(this, binding.mRecyclerManga).apply {
|
||||
ScrollTimer(this, binding.mRecyclerManga, lifecycleScope).apply {
|
||||
setSpeed(AppConfig.mangaAutoPageSpeed)
|
||||
}
|
||||
}
|
||||
@@ -359,7 +352,9 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
ReadManga.syncProgress({ progress -> sureNewProgress(progress) })
|
||||
}
|
||||
}
|
||||
startAutoPage()
|
||||
if (isAutoScrollPage) {
|
||||
mScrollTimer.isEnabledPage = true
|
||||
}
|
||||
if (isAutoScroll) {
|
||||
mScrollTimer.isEnabled = true
|
||||
}
|
||||
@@ -380,7 +375,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
}
|
||||
ReadManga.cancelPreDownloadTask()
|
||||
networkChangedListener.unRegister()
|
||||
stopAutoPage()
|
||||
mScrollTimer.isEnabledPage = false
|
||||
mScrollTimer.isEnabled = false
|
||||
}
|
||||
|
||||
@@ -439,6 +434,10 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
)
|
||||
}
|
||||
|
||||
override fun scrollPage() {
|
||||
scrollToNext()
|
||||
}
|
||||
|
||||
override val oldBook: Book?
|
||||
get() = ReadManga.book
|
||||
|
||||
@@ -517,13 +516,8 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
item.isChecked = !item.isChecked
|
||||
val menuMangaAutoPageSpeed = mMenu?.findItem(R.id.menu_manga_auto_page_speed)
|
||||
isAutoScrollPage = item.isChecked
|
||||
if (item.isChecked) {
|
||||
startAutoPage()
|
||||
menuMangaAutoPageSpeed?.isVisible = true
|
||||
} else {
|
||||
stopAutoPage()
|
||||
menuMangaAutoPageSpeed?.isVisible = false
|
||||
}
|
||||
mScrollTimer.isEnabledPage = item.isChecked
|
||||
menuMangaAutoPageSpeed?.isVisible = item.isChecked
|
||||
enableAutoPageScroll = item.isChecked
|
||||
enableAutoScroll = false
|
||||
mScrollTimer.isEnabled = false
|
||||
@@ -533,7 +527,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
|
||||
R.id.menu_manga_auto_page_speed -> {
|
||||
val mangaAutoPage = mMenu?.findItem(R.id.menu_enable_auto_page)
|
||||
val mangaAutoScroll = mMenu?.findItem(R.id.menu_enable_auto_scroll)
|
||||
// val mangaAutoScroll = mMenu?.findItem(R.id.menu_enable_auto_scroll)
|
||||
showNumberPickerDialog(
|
||||
1, getString(R.string.setting_manga_auto_page_speed),
|
||||
AppConfig.mangaAutoPageSpeed
|
||||
@@ -541,11 +535,9 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
AppConfig.mangaAutoPageSpeed = it
|
||||
mMangaAutoPageSpeed = it
|
||||
item.title = getString(R.string.manga_auto_page_speed, it)
|
||||
if (mangaAutoScroll?.isChecked == true) {
|
||||
mScrollTimer.setSpeed(it)
|
||||
}
|
||||
mScrollTimer.setSpeed(it)
|
||||
if (mangaAutoPage?.isChecked == true) {
|
||||
startAutoPage()
|
||||
mScrollTimer.isEnabledPage = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -574,7 +566,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
enableAutoPageScroll = false
|
||||
isAutoScrollPage = false
|
||||
isAutoScroll = item.isChecked
|
||||
stopAutoPage()
|
||||
mScrollTimer.isEnabledPage = false
|
||||
mMenu?.findItem(R.id.menu_manga_auto_page_speed)?.isVisible = item.isChecked
|
||||
mPagerSnapHelper.attachToRecyclerView(null)
|
||||
}
|
||||
@@ -597,11 +589,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
mScrollTimer.isEnabled = !menuIsVisible
|
||||
}
|
||||
if (enableAutoPageScroll) {
|
||||
if (menuIsVisible) {
|
||||
stopAutoPage()
|
||||
} else {
|
||||
startAutoPage()
|
||||
}
|
||||
mScrollTimer.isEnabledPage = !menuIsVisible
|
||||
}
|
||||
}
|
||||
|
||||
@@ -724,17 +712,6 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, ReadMangaViewMode
|
||||
binding.mRecyclerManga.smoothScrollBy(-dx, -dy)
|
||||
}
|
||||
|
||||
private fun startAutoPage() {
|
||||
stopAutoPage()
|
||||
if (isAutoScrollPage) {
|
||||
autoScrollHandler.postDelayed(autoScrollRunnable, mMangaAutoPageSpeed.times(1000L))
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopAutoPage() {
|
||||
autoScrollHandler.removeCallbacks(autoScrollRunnable)
|
||||
}
|
||||
|
||||
private fun showNumberPickerDialog(
|
||||
min: Int,
|
||||
title: String,
|
||||
|
||||
@@ -1,13 +1,24 @@
|
||||
package io.legado.app.ui.book.manga.recyclerview
|
||||
|
||||
import androidx.lifecycle.LifecycleCoroutineScope
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.coroutineScope
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.isActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ScrollTimer(
|
||||
private val callback: ScrollCallback,
|
||||
private val recyclerView: RecyclerView
|
||||
private val recyclerView: RecyclerView,
|
||||
private val coroutine: LifecycleCoroutineScope,
|
||||
) : RecyclerView.OnScrollListener() {
|
||||
private var distance = 1
|
||||
private var mScrollPageJob: Job? = null
|
||||
var isEnabled: Boolean = false
|
||||
set(value) {
|
||||
if (field != value) {
|
||||
@@ -21,9 +32,22 @@ class ScrollTimer(
|
||||
}
|
||||
}
|
||||
|
||||
var isEnabledPage: Boolean = false
|
||||
set(value) {
|
||||
if (field != value) {
|
||||
field = value
|
||||
if (value) {
|
||||
mScrollPageJob?.cancel()
|
||||
startScrollPage()
|
||||
} else {
|
||||
mScrollPageJob?.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onScrollStateChanged(
|
||||
recyclerView: RecyclerView,
|
||||
newState: Int
|
||||
newState: Int,
|
||||
) {
|
||||
if (newState == SCROLL_STATE_IDLE) {
|
||||
startScroll()
|
||||
@@ -38,7 +62,19 @@ class ScrollTimer(
|
||||
callback.scrollBy(distance)
|
||||
}
|
||||
|
||||
private fun startScrollPage() {
|
||||
mScrollPageJob = coroutine.launch(Dispatchers.Default) {
|
||||
while (isActive) {
|
||||
delay(distance.times(1000L))
|
||||
withContext(Dispatchers.Main) {
|
||||
callback.scrollPage()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface ScrollCallback {
|
||||
fun scrollBy(distance: Int)
|
||||
fun scrollPage()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user