Autoscroll in reader #204
parent
57929f62ad
commit
6c43881cf4
@ -0,0 +1,74 @@
|
|||||||
|
package org.koitharu.kotatsu.reader.ui.config
|
||||||
|
|
||||||
|
import android.content.res.Resources
|
||||||
|
import androidx.lifecycle.Lifecycle
|
||||||
|
import androidx.lifecycle.LifecycleOwner
|
||||||
|
import androidx.lifecycle.coroutineScope
|
||||||
|
import androidx.lifecycle.repeatOnLifecycle
|
||||||
|
import com.google.android.material.slider.LabelFormatter
|
||||||
|
import kotlin.math.roundToLong
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.isActive
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koitharu.kotatsu.R
|
||||||
|
import org.koitharu.kotatsu.parsers.util.format
|
||||||
|
import org.koitharu.kotatsu.reader.ui.ReaderControlDelegate
|
||||||
|
|
||||||
|
class PageSwitchTimer(
|
||||||
|
private val listener: ReaderControlDelegate.OnInteractionListener,
|
||||||
|
private val lifecycleOwner: LifecycleOwner,
|
||||||
|
) {
|
||||||
|
|
||||||
|
var delaySec: Float = 0f
|
||||||
|
set(value) {
|
||||||
|
field = value
|
||||||
|
delayMs = mapDelay(value)
|
||||||
|
restartJob()
|
||||||
|
}
|
||||||
|
private var delayMs = 0L
|
||||||
|
|
||||||
|
fun onUserInteraction() {
|
||||||
|
restartJob()
|
||||||
|
}
|
||||||
|
|
||||||
|
private var job: Job? = null
|
||||||
|
|
||||||
|
private fun restartJob() {
|
||||||
|
job?.cancel()
|
||||||
|
if (delayMs == 0L) {
|
||||||
|
job = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
job = lifecycleOwner.lifecycle.coroutineScope.launch {
|
||||||
|
// FIXME: pause when bs is opened
|
||||||
|
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
|
||||||
|
while (isActive) {
|
||||||
|
delay(delayMs)
|
||||||
|
listener.switchPageBy(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DelayLabelFormatter(resources: Resources) : LabelFormatter {
|
||||||
|
|
||||||
|
private val textOff = resources.getString(R.string.off_short)
|
||||||
|
private val textSec = resources.getString(R.string.seconds_pattern)
|
||||||
|
|
||||||
|
override fun getFormattedValue(value: Float): String {
|
||||||
|
val ms = mapDelay(value)
|
||||||
|
return if (ms == 0L) textOff else textSec.format((ms / 1000.0).format(1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
private const val DELAY_MIN = 2000L
|
||||||
|
|
||||||
|
fun mapDelay(value: Float): Long {
|
||||||
|
val delay = (value * 1000L).roundToLong()
|
||||||
|
return if (delay < DELAY_MIN) 0L else delay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:tint="?attr/colorControlNormal"
|
||||||
|
android:viewportWidth="24"
|
||||||
|
android:viewportHeight="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="#000"
|
||||||
|
android:pathData="M12,20A7,7 0 0,1 5,13A7,7 0 0,1 12,6A7,7 0 0,1 19,13A7,7 0 0,1 12,20M19.03,7.39L20.45,5.97C20,5.46 19.55,5 19.04,4.56L17.62,6C16.07,4.74 14.12,4 12,4A9,9 0 0,0 3,13A9,9 0 0,0 12,22C17,22 21,17.97 21,13C21,10.88 20.26,8.93 19.03,7.39M11,14H13V8H11M15,1H9V3H15V1Z" />
|
||||||
|
</vector>
|
||||||
Loading…
Reference in New Issue