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