From bb8f3c63f1e5aade89ea3375c559c0e5dbef46ef Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 1 Jul 2023 12:47:20 -0400 Subject: [PATCH] Remove usage of savedInstanceState for storing reader menu visibility state --- .../tachiyomi/ui/reader/ReaderActivity.kt | 41 +++++++------------ .../tachiyomi/ui/reader/ReaderViewModel.kt | 5 +++ .../ui/reader/viewer/pager/PagerViewer.kt | 6 +-- .../ui/reader/viewer/webtoon/WebtoonViewer.kt | 8 ++-- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 696e19d02..2824bad65 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -99,7 +99,8 @@ import tachiyomi.core.util.lang.launchNonCancellable import tachiyomi.core.util.lang.withUIContext import tachiyomi.core.util.system.logcat import tachiyomi.domain.manga.model.Manga -import uy.kohesive.injekt.injectLazy +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import kotlin.math.abs class ReaderActivity : BaseActivity() { @@ -114,8 +115,8 @@ class ReaderActivity : BaseActivity() { } } - private val readerPreferences: ReaderPreferences by injectLazy() - private val preferences: BasePreferences by injectLazy() + private val readerPreferences = Injekt.get() + private val preferences = Injekt.get() lateinit var binding: ReaderActivityBinding @@ -124,19 +125,12 @@ class ReaderActivity : BaseActivity() { val hasCutout by lazy { hasDisplayCutout() } - /** - * Whether the menu is currently visible. - */ - var menuVisible = false - private set - /** * Configuration at reader level, like background color or forced orientation. */ private var config: ReaderConfig? = null private var menuToggleToast: Toast? = null - private var readingModeToast: Toast? = null private val windowInsetsController by lazy { WindowInsetsControllerCompat(window, binding.root) } @@ -159,8 +153,8 @@ class ReaderActivity : BaseActivity() { setContentView(binding.root) if (viewModel.needsInit()) { - val manga = intent.extras!!.getLong("manga", -1) - val chapter = intent.extras!!.getLong("chapter", -1) + val manga = intent.extras?.getLong("manga", -1) ?: -1L + val chapter = intent.extras?.getLong("chapter", -1) ?: -1L if (manga == -1L || chapter == -1L) { finish() return @@ -178,10 +172,6 @@ class ReaderActivity : BaseActivity() { } } - if (savedInstanceState != null) { - menuVisible = savedInstanceState.getBoolean(::menuVisible.name) - } - config = ReaderConfig() initializeMenu() @@ -250,7 +240,6 @@ class ReaderActivity : BaseActivity() { * activity isn't changing configurations. */ override fun onSaveInstanceState(outState: Bundle) { - outState.putBoolean(::menuVisible.name, menuVisible) viewModel.onSaveInstanceState() super.onSaveInstanceState(outState) } @@ -267,7 +256,7 @@ class ReaderActivity : BaseActivity() { override fun onResume() { super.onResume() viewModel.setReadStartTime() - setMenuVisibility(menuVisible, animate = false) + setMenuVisibility(viewModel.state.value.menuVisible, animate = false) } /** @@ -277,7 +266,7 @@ class ReaderActivity : BaseActivity() { override fun onWindowFocusChanged(hasFocus: Boolean) { super.onWindowFocusChanged(hasFocus) if (hasFocus) { - setMenuVisibility(menuVisible, animate = false) + setMenuVisibility(viewModel.state.value.menuVisible, animate = false) } } @@ -413,7 +402,7 @@ class ReaderActivity : BaseActivity() { when (state.dialog) { is ReaderViewModel.Dialog.Loading -> { AlertDialog( - onDismissRequest = { /* Non dismissible */ }, + onDismissRequest = {}, confirmButton = {}, text = { Row( @@ -488,7 +477,7 @@ class ReaderActivity : BaseActivity() { } // Set initial visibility - setMenuVisibility(menuVisible) + setMenuVisibility(viewModel.state.value.menuVisible) } private fun initBottomShortcuts() { @@ -614,7 +603,7 @@ class ReaderActivity : BaseActivity() { * [animate] the views. */ fun setMenuVisibility(visible: Boolean, animate: Boolean = true) { - menuVisible = visible + viewModel.showMenus(visible) if (visible) { windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) binding.readerMenu.isVisible = true @@ -844,14 +833,14 @@ class ReaderActivity : BaseActivity() { * viewer because each one implements its own touch and key events. */ fun toggleMenu() { - setMenuVisibility(!menuVisible) + setMenuVisibility(!viewModel.state.value.menuVisible) } /** * Called from the viewer to show the menu. */ fun showMenu() { - if (!menuVisible) { + if (!viewModel.state.value.menuVisible) { setMenuVisibility(true) } } @@ -860,7 +849,7 @@ class ReaderActivity : BaseActivity() { * Called from the viewer to hide the menu. */ fun hideMenu() { - if (menuVisible) { + if (viewModel.state.value.menuVisible) { setMenuVisibility(false) } } @@ -1058,7 +1047,7 @@ class ReaderActivity : BaseActivity() { } // Trigger relayout - setMenuVisibility(menuVisible) + setMenuVisibility(viewModel.state.value.menuVisible) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt index 0c683cecf..a2b9bf081 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt @@ -718,6 +718,10 @@ class ReaderViewModel( ) + filenameSuffix } + fun showMenus(visible: Boolean) { + mutableState.update { it.copy(menuVisible = visible) } + } + fun showLoadingDialog() { mutableState.update { it.copy(dialog = Dialog.Loading) } } @@ -926,6 +930,7 @@ class ReaderViewModel( */ val viewer: Viewer? = null, val dialog: Dialog? = null, + val menuVisible: Boolean = false, ) { val totalPages: Int get() = viewerChapters?.currChapter?.pages?.size ?: -1 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt index 20dfbe0b9..de23ee9e6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt @@ -112,7 +112,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : Viewer { } } pager.longTapListener = f@{ - if (activity.menuVisible || config.longTapEnabled) { + if (activity.viewModel.state.value.menuVisible || config.longTapEnabled) { val item = adapter.items.getOrNull(pager.currentItem) if (item is ReaderPage) { activity.onPageLongTap(item) @@ -374,14 +374,14 @@ abstract class PagerViewer(val activity: ReaderActivity) : Viewer { when (event.keyCode) { KeyEvent.KEYCODE_VOLUME_DOWN -> { - if (!config.volumeKeysEnabled || activity.menuVisible) { + if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) { return false } else if (isUp) { if (!config.volumeKeysInverted) moveDown() else moveUp() } } KeyEvent.KEYCODE_VOLUME_UP -> { - if (!config.volumeKeysEnabled || activity.menuVisible) { + if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) { return false } else if (isUp) { if (!config.volumeKeysInverted) moveUp() else moveDown() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt index 309649c43..d7a0290f3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/webtoon/WebtoonViewer.kt @@ -91,7 +91,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { onScrolled() - if ((dy > threshold || dy < -threshold) && activity.menuVisible) { + if ((dy > threshold || dy < -threshold) && activity.viewModel.state.value.menuVisible) { activity.hideMenu() } @@ -120,7 +120,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr } } recycler.longTapListener = f@{ event -> - if (activity.menuVisible || config.longTapEnabled) { + if (activity.viewModel.state.value.menuVisible || config.longTapEnabled) { val child = recycler.findChildViewUnder(event.x, event.y) if (child != null) { val position = recycler.getChildAdapterPosition(child) @@ -310,14 +310,14 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr when (event.keyCode) { KeyEvent.KEYCODE_VOLUME_DOWN -> { - if (!config.volumeKeysEnabled || activity.menuVisible) { + if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) { return false } else if (isUp) { if (!config.volumeKeysInverted) scrollDown() else scrollUp() } } KeyEvent.KEYCODE_VOLUME_UP -> { - if (!config.volumeKeysEnabled || activity.menuVisible) { + if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) { return false } else if (isUp) { if (!config.volumeKeysInverted) scrollUp() else scrollDown()