Cleanup Autoscroll coroutine code
This commit is contained in:
parent
a903a48718
commit
1b90590260
@ -75,10 +75,10 @@ import kotlinx.coroutines.Job
|
|||||||
import kotlinx.coroutines.cancel
|
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.flow
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
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
|
||||||
@ -87,7 +87,8 @@ import timber.log.Timber
|
|||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.roundToLong
|
import kotlin.time.ExperimentalTime
|
||||||
|
import kotlin.time.seconds
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Activity containing the reader of Tachiyomi. This activity is mostly a container of the
|
* Activity containing the reader of Tachiyomi. This activity is mostly a container of the
|
||||||
@ -120,7 +121,8 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
|||||||
// SY -->
|
// SY -->
|
||||||
private var ehUtilsVisible = false
|
private var ehUtilsVisible = false
|
||||||
|
|
||||||
private var autoscrollScope: CoroutineScope? = null
|
private var autoscrollScope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Main)
|
||||||
|
private var autoScrollJob: Job? = 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")
|
||||||
@ -210,21 +212,25 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupAutoscroll(interval: Float) {
|
@OptIn(ExperimentalTime::class)
|
||||||
autoscrollScope?.cancel()
|
private fun setupAutoscroll(interval: Double) {
|
||||||
if (interval == -1f) return
|
autoScrollJob?.cancel()
|
||||||
|
if (interval == -1.0) return
|
||||||
|
|
||||||
val intervalMs = (interval * 1000).roundToLong()
|
val duration = interval.seconds
|
||||||
autoscrollScope = CoroutineScope(Job() + Dispatchers.Main)
|
autoScrollJob =
|
||||||
autoscrollScope?.launch {
|
flow {
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(intervalMs)
|
delay(duration)
|
||||||
|
emit(Unit)
|
||||||
|
}
|
||||||
|
}.onEach {
|
||||||
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
.launchIn(autoscrollScope)
|
||||||
}
|
}
|
||||||
// SY <--
|
// SY <--
|
||||||
|
|
||||||
@ -241,8 +247,10 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
|||||||
config = null
|
config = null
|
||||||
progressDialog?.dismiss()
|
progressDialog?.dismiss()
|
||||||
progressDialog = null
|
progressDialog = null
|
||||||
autoscrollScope?.cancel()
|
// SY -->
|
||||||
autoscrollScope = null
|
autoScrollJob?.cancel()
|
||||||
|
autoScrollJob = null
|
||||||
|
// SY <--
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -425,9 +433,9 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
|||||||
.onEach {
|
.onEach {
|
||||||
setupAutoscroll(
|
setupAutoscroll(
|
||||||
if (it) {
|
if (it) {
|
||||||
preferences.eh_utilAutoscrollInterval().get()
|
preferences.eh_utilAutoscrollInterval().get().toDouble()
|
||||||
} else {
|
} else {
|
||||||
-1f
|
-1.0
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -435,18 +443,18 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
|
|||||||
|
|
||||||
binding.ehAutoscrollFreq.textChanges()
|
binding.ehAutoscrollFreq.textChanges()
|
||||||
.onEach {
|
.onEach {
|
||||||
val parsed = it.toString().toFloatOrNull()
|
val parsed = it.toString().toDoubleOrNull()
|
||||||
|
|
||||||
if (parsed == null || parsed <= 0 || parsed > 9999) {
|
if (parsed == null || parsed <= 0 || parsed > 9999) {
|
||||||
binding.ehAutoscrollFreq.error = "Invalid frequency"
|
binding.ehAutoscrollFreq.error = "Invalid frequency"
|
||||||
preferences.eh_utilAutoscrollInterval().set(-1f)
|
preferences.eh_utilAutoscrollInterval().set(-1f)
|
||||||
binding.ehAutoscroll.isEnabled = false
|
binding.ehAutoscroll.isEnabled = false
|
||||||
setupAutoscroll(-1f)
|
setupAutoscroll(-1.0)
|
||||||
} else {
|
} else {
|
||||||
binding.ehAutoscrollFreq.error = null
|
binding.ehAutoscrollFreq.error = null
|
||||||
preferences.eh_utilAutoscrollInterval().set(parsed)
|
preferences.eh_utilAutoscrollInterval().set(parsed.toFloat())
|
||||||
binding.ehAutoscroll.isEnabled = true
|
binding.ehAutoscroll.isEnabled = true
|
||||||
setupAutoscroll(if (binding.ehAutoscroll.isChecked) parsed else -1f)
|
setupAutoscroll(if (binding.ehAutoscroll.isChecked) parsed else -1.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.launchIn(scope)
|
.launchIn(scope)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user