From 2a3cc1172843d200bc94663247f3f48e68658247 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 19 Aug 2022 10:22:16 +0300 Subject: [PATCH] Save reader state on idle --- .../kotatsu/reader/ui/ReaderActivity.kt | 12 +++++- .../koitharu/kotatsu/utils/IdlingDetector.kt | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/IdlingDetector.kt diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt index 9986fde88..ecee19b44 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt @@ -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) { diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/IdlingDetector.kt b/app/src/main/java/org/koitharu/kotatsu/utils/IdlingDetector.kt new file mode 100644 index 000000000..0501a3da6 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/utils/IdlingDetector.kt @@ -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() + } +}