Convert autoscroll to a coroutine

This commit is contained in:
Jobobby04 2020-08-13 23:02:25 -04:00
parent 84b9b4db55
commit 7f7b2901cb

View File

@ -65,25 +65,24 @@ import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.widget.SimpleAnimationListener import eu.kanade.tachiyomi.widget.SimpleAnimationListener
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import exh.util.defaultReaderType import exh.util.defaultReaderType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.observeOn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.launch
import nucleus.factory.RequiresPresenter import nucleus.factory.RequiresPresenter
import reactivecircus.flowbinding.android.view.clicks import reactivecircus.flowbinding.android.view.clicks
import reactivecircus.flowbinding.android.widget.checkedChanges import reactivecircus.flowbinding.android.widget.checkedChanges
import reactivecircus.flowbinding.android.widget.textChanges import reactivecircus.flowbinding.android.widget.textChanges
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.roundToLong import kotlin.math.roundToLong
@ -115,16 +114,14 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
var menuVisible = false var menuVisible = false
private set private set
// --> EH // SY -->
private var ehUtilsVisible = false private var ehUtilsVisible = false
private val exhSubscriptions = CompositeSubscription() private var autoscrollScope: CoroutineScope? = null
private var autoscrollSubscription: Subscription? = null
private val sourceManager: SourceManager by injectLazy() private val sourceManager: SourceManager by injectLazy()
private val logger = XLog.tag("ReaderActivity") private val logger = XLog.tag("ReaderActivity")
// <-- EH // SY <--
/** /**
* Configuration at reader level, like background color or forced orientation. * Configuration at reader level, like background color or forced orientation.
@ -197,7 +194,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
} }
} }
// --> EH // SY -->
private fun setEhUtilsVisibility(visible: Boolean) { private fun setEhUtilsVisibility(visible: Boolean) {
if (visible) { if (visible) {
binding.ehUtils.isVisible = true binding.ehUtils.isVisible = true
@ -207,30 +204,24 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
binding.expandEhButton.setImageResource(R.drawable.ic_keyboard_arrow_down_white_32dp) binding.expandEhButton.setImageResource(R.drawable.ic_keyboard_arrow_down_white_32dp)
} }
} }
// <-- EH
// --> EH
private fun setupAutoscroll(interval: Float) { private fun setupAutoscroll(interval: Float) {
exhSubscriptions.remove(autoscrollSubscription) autoscrollScope?.cancel()
autoscrollSubscription = null
if (interval == -1f) return if (interval == -1f) return
val intervalMs = (interval * 1000).roundToLong() val intervalMs = (interval * 1000).roundToLong()
val sub = Observable.interval(intervalMs, intervalMs, TimeUnit.MILLISECONDS) autoscrollScope = CoroutineScope(Job() + Dispatchers.Main)
.onBackpressureDrop() autoscrollScope?.launch {
.observeOn(AndroidSchedulers.mainThread()) while (true) {
.subscribe { delay(intervalMs)
viewer.let { v -> viewer.let { v ->
if (v is PagerViewer) v.moveToNext() if (v is PagerViewer) v.moveToNext()
else if (v is WebtoonViewer) v.scrollDown() else if (v is WebtoonViewer) v.scrollDown()
} }
} }
}
autoscrollSubscription = sub
exhSubscriptions += sub
} }
// <-- EH // SY <--
/** /**
* Called when the activity is destroyed. Cleans up the viewer, configuration and any view. * Called when the activity is destroyed. Cleans up the viewer, configuration and any view.
@ -242,6 +233,8 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
config = null config = null
progressDialog?.dismiss() progressDialog?.dismiss()
progressDialog = null progressDialog = null
autoscrollScope?.cancel()
autoscrollScope = null
} }
/** /**
@ -417,7 +410,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
) )
binding.ehAutoscroll.checkedChanges() binding.ehAutoscroll.checkedChanges()
// .observeOn(AndroidSchedulers.mainThread())
.onEach { .onEach {
setupAutoscroll( setupAutoscroll(
if (it) { if (it) {
@ -430,7 +422,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
.launchIn(scope) .launchIn(scope)
binding.ehAutoscrollFreq.textChanges() binding.ehAutoscrollFreq.textChanges()
// .observeOn(AndroidSchedulers.mainThread())
.onEach { .onEach {
val parsed = it.toString().toFloatOrNull() val parsed = it.toString().toFloatOrNull()