Save reader state on idle
This commit is contained in:
@@ -40,6 +40,7 @@ import org.koitharu.kotatsu.reader.ui.thumbnails.OnPageSelectListener
|
||||
import org.koitharu.kotatsu.reader.ui.thumbnails.PagesThumbnailsSheet
|
||||
import org.koitharu.kotatsu.settings.SettingsActivity
|
||||
import org.koitharu.kotatsu.utils.GridTouchHelper
|
||||
import org.koitharu.kotatsu.utils.IdlingDetector
|
||||
import org.koitharu.kotatsu.utils.ShareHelper
|
||||
import org.koitharu.kotatsu.utils.ext.*
|
||||
|
||||
@@ -51,11 +52,14 @@ class ReaderActivity :
|
||||
OnPageSelectListener,
|
||||
ReaderConfigBottomSheet.Callback,
|
||||
ReaderControlDelegate.OnInteractionListener,
|
||||
OnApplyWindowInsetsListener {
|
||||
OnApplyWindowInsetsListener,
|
||||
IdlingDetector.Callback {
|
||||
|
||||
@Inject
|
||||
lateinit var viewModelFactory: ReaderViewModel.Factory
|
||||
|
||||
private val idlingDetector = IdlingDetector(TimeUnit.SECONDS.toMillis(10), this)
|
||||
|
||||
val viewModel by assistedViewModels {
|
||||
viewModelFactory.create(
|
||||
intent = MangaIntent(intent),
|
||||
@@ -89,6 +93,7 @@ class ReaderActivity :
|
||||
binding.slider.setLabelFormatter(PageLabelFormatter())
|
||||
ReaderSliderListener(this, viewModel).attachToSlider(binding.slider)
|
||||
insetsDelegate.interceptingWindowInsetsListener = this
|
||||
idlingDetector.bindToLifecycle(this)
|
||||
|
||||
viewModel.onError.observe(this, this::onError)
|
||||
viewModel.readerMode.observe(this, this::onInitReader)
|
||||
@@ -111,6 +116,11 @@ class ReaderActivity :
|
||||
override fun onUserInteraction() {
|
||||
super.onUserInteraction()
|
||||
pageSwitchTimer.onUserInteraction()
|
||||
idlingDetector.onUserInteraction()
|
||||
}
|
||||
|
||||
override fun onIdle() {
|
||||
viewModel.saveCurrentState(readerManager.currentReader?.getCurrentState())
|
||||
}
|
||||
|
||||
private fun onInitReader(mode: ReaderMode) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package org.koitharu.kotatsu.utils
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
|
||||
class IdlingDetector(
|
||||
private val timeoutMs: Long,
|
||||
private val callback: Callback,
|
||||
) : DefaultLifecycleObserver {
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val idleRunnable = Runnable {
|
||||
callback.onIdle()
|
||||
}
|
||||
|
||||
fun bindToLifecycle(owner: LifecycleOwner) {
|
||||
owner.lifecycle.addObserver(this)
|
||||
}
|
||||
|
||||
fun onUserInteraction() {
|
||||
handler.removeCallbacks(idleRunnable)
|
||||
handler.postDelayed(idleRunnable, timeoutMs)
|
||||
}
|
||||
|
||||
override fun onDestroy(owner: LifecycleOwner) {
|
||||
super.onDestroy(owner)
|
||||
owner.lifecycle.removeObserver(this)
|
||||
handler.removeCallbacks(idleRunnable)
|
||||
}
|
||||
|
||||
fun interface Callback {
|
||||
|
||||
fun onIdle()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user