Remove usage of savedInstanceState for storing reader menu visibility state

This commit is contained in:
arkon 2023-07-01 12:47:20 -04:00
parent 20faaaa908
commit bb8f3c63f1
4 changed files with 27 additions and 33 deletions

View File

@ -99,7 +99,8 @@ import tachiyomi.core.util.lang.launchNonCancellable
import tachiyomi.core.util.lang.withUIContext import tachiyomi.core.util.lang.withUIContext
import tachiyomi.core.util.system.logcat import tachiyomi.core.util.system.logcat
import tachiyomi.domain.manga.model.Manga 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 import kotlin.math.abs
class ReaderActivity : BaseActivity() { class ReaderActivity : BaseActivity() {
@ -114,8 +115,8 @@ class ReaderActivity : BaseActivity() {
} }
} }
private val readerPreferences: ReaderPreferences by injectLazy() private val readerPreferences = Injekt.get<ReaderPreferences>()
private val preferences: BasePreferences by injectLazy() private val preferences = Injekt.get<BasePreferences>()
lateinit var binding: ReaderActivityBinding lateinit var binding: ReaderActivityBinding
@ -124,19 +125,12 @@ class ReaderActivity : BaseActivity() {
val hasCutout by lazy { hasDisplayCutout() } 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. * Configuration at reader level, like background color or forced orientation.
*/ */
private var config: ReaderConfig? = null private var config: ReaderConfig? = null
private var menuToggleToast: Toast? = null private var menuToggleToast: Toast? = null
private var readingModeToast: Toast? = null private var readingModeToast: Toast? = null
private val windowInsetsController by lazy { WindowInsetsControllerCompat(window, binding.root) } private val windowInsetsController by lazy { WindowInsetsControllerCompat(window, binding.root) }
@ -159,8 +153,8 @@ class ReaderActivity : BaseActivity() {
setContentView(binding.root) setContentView(binding.root)
if (viewModel.needsInit()) { if (viewModel.needsInit()) {
val manga = intent.extras!!.getLong("manga", -1) val manga = intent.extras?.getLong("manga", -1) ?: -1L
val chapter = intent.extras!!.getLong("chapter", -1) val chapter = intent.extras?.getLong("chapter", -1) ?: -1L
if (manga == -1L || chapter == -1L) { if (manga == -1L || chapter == -1L) {
finish() finish()
return return
@ -178,10 +172,6 @@ class ReaderActivity : BaseActivity() {
} }
} }
if (savedInstanceState != null) {
menuVisible = savedInstanceState.getBoolean(::menuVisible.name)
}
config = ReaderConfig() config = ReaderConfig()
initializeMenu() initializeMenu()
@ -250,7 +240,6 @@ class ReaderActivity : BaseActivity() {
* activity isn't changing configurations. * activity isn't changing configurations.
*/ */
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
outState.putBoolean(::menuVisible.name, menuVisible)
viewModel.onSaveInstanceState() viewModel.onSaveInstanceState()
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
} }
@ -267,7 +256,7 @@ class ReaderActivity : BaseActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
viewModel.setReadStartTime() 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) { override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus) super.onWindowFocusChanged(hasFocus)
if (hasFocus) { if (hasFocus) {
setMenuVisibility(menuVisible, animate = false) setMenuVisibility(viewModel.state.value.menuVisible, animate = false)
} }
} }
@ -413,7 +402,7 @@ class ReaderActivity : BaseActivity() {
when (state.dialog) { when (state.dialog) {
is ReaderViewModel.Dialog.Loading -> { is ReaderViewModel.Dialog.Loading -> {
AlertDialog( AlertDialog(
onDismissRequest = { /* Non dismissible */ }, onDismissRequest = {},
confirmButton = {}, confirmButton = {},
text = { text = {
Row( Row(
@ -488,7 +477,7 @@ class ReaderActivity : BaseActivity() {
} }
// Set initial visibility // Set initial visibility
setMenuVisibility(menuVisible) setMenuVisibility(viewModel.state.value.menuVisible)
} }
private fun initBottomShortcuts() { private fun initBottomShortcuts() {
@ -614,7 +603,7 @@ class ReaderActivity : BaseActivity() {
* [animate] the views. * [animate] the views.
*/ */
fun setMenuVisibility(visible: Boolean, animate: Boolean = true) { fun setMenuVisibility(visible: Boolean, animate: Boolean = true) {
menuVisible = visible viewModel.showMenus(visible)
if (visible) { if (visible) {
windowInsetsController.show(WindowInsetsCompat.Type.systemBars()) windowInsetsController.show(WindowInsetsCompat.Type.systemBars())
binding.readerMenu.isVisible = true binding.readerMenu.isVisible = true
@ -844,14 +833,14 @@ class ReaderActivity : BaseActivity() {
* viewer because each one implements its own touch and key events. * viewer because each one implements its own touch and key events.
*/ */
fun toggleMenu() { fun toggleMenu() {
setMenuVisibility(!menuVisible) setMenuVisibility(!viewModel.state.value.menuVisible)
} }
/** /**
* Called from the viewer to show the menu. * Called from the viewer to show the menu.
*/ */
fun showMenu() { fun showMenu() {
if (!menuVisible) { if (!viewModel.state.value.menuVisible) {
setMenuVisibility(true) setMenuVisibility(true)
} }
} }
@ -860,7 +849,7 @@ class ReaderActivity : BaseActivity() {
* Called from the viewer to hide the menu. * Called from the viewer to hide the menu.
*/ */
fun hideMenu() { fun hideMenu() {
if (menuVisible) { if (viewModel.state.value.menuVisible) {
setMenuVisibility(false) setMenuVisibility(false)
} }
} }
@ -1058,7 +1047,7 @@ class ReaderActivity : BaseActivity() {
} }
// Trigger relayout // Trigger relayout
setMenuVisibility(menuVisible) setMenuVisibility(viewModel.state.value.menuVisible)
} }
/** /**

View File

@ -718,6 +718,10 @@ class ReaderViewModel(
) + filenameSuffix ) + filenameSuffix
} }
fun showMenus(visible: Boolean) {
mutableState.update { it.copy(menuVisible = visible) }
}
fun showLoadingDialog() { fun showLoadingDialog() {
mutableState.update { it.copy(dialog = Dialog.Loading) } mutableState.update { it.copy(dialog = Dialog.Loading) }
} }
@ -926,6 +930,7 @@ class ReaderViewModel(
*/ */
val viewer: Viewer? = null, val viewer: Viewer? = null,
val dialog: Dialog? = null, val dialog: Dialog? = null,
val menuVisible: Boolean = false,
) { ) {
val totalPages: Int val totalPages: Int
get() = viewerChapters?.currChapter?.pages?.size ?: -1 get() = viewerChapters?.currChapter?.pages?.size ?: -1

View File

@ -112,7 +112,7 @@ abstract class PagerViewer(val activity: ReaderActivity) : Viewer {
} }
} }
pager.longTapListener = f@{ pager.longTapListener = f@{
if (activity.menuVisible || config.longTapEnabled) { if (activity.viewModel.state.value.menuVisible || config.longTapEnabled) {
val item = adapter.items.getOrNull(pager.currentItem) val item = adapter.items.getOrNull(pager.currentItem)
if (item is ReaderPage) { if (item is ReaderPage) {
activity.onPageLongTap(item) activity.onPageLongTap(item)
@ -374,14 +374,14 @@ abstract class PagerViewer(val activity: ReaderActivity) : Viewer {
when (event.keyCode) { when (event.keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> { KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (!config.volumeKeysEnabled || activity.menuVisible) { if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) {
return false return false
} else if (isUp) { } else if (isUp) {
if (!config.volumeKeysInverted) moveDown() else moveUp() if (!config.volumeKeysInverted) moveDown() else moveUp()
} }
} }
KeyEvent.KEYCODE_VOLUME_UP -> { KeyEvent.KEYCODE_VOLUME_UP -> {
if (!config.volumeKeysEnabled || activity.menuVisible) { if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) {
return false return false
} else if (isUp) { } else if (isUp) {
if (!config.volumeKeysInverted) moveUp() else moveDown() if (!config.volumeKeysInverted) moveUp() else moveDown()

View File

@ -91,7 +91,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
onScrolled() onScrolled()
if ((dy > threshold || dy < -threshold) && activity.menuVisible) { if ((dy > threshold || dy < -threshold) && activity.viewModel.state.value.menuVisible) {
activity.hideMenu() activity.hideMenu()
} }
@ -120,7 +120,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
} }
} }
recycler.longTapListener = f@{ event -> 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) val child = recycler.findChildViewUnder(event.x, event.y)
if (child != null) { if (child != null) {
val position = recycler.getChildAdapterPosition(child) val position = recycler.getChildAdapterPosition(child)
@ -310,14 +310,14 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
when (event.keyCode) { when (event.keyCode) {
KeyEvent.KEYCODE_VOLUME_DOWN -> { KeyEvent.KEYCODE_VOLUME_DOWN -> {
if (!config.volumeKeysEnabled || activity.menuVisible) { if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) {
return false return false
} else if (isUp) { } else if (isUp) {
if (!config.volumeKeysInverted) scrollDown() else scrollUp() if (!config.volumeKeysInverted) scrollDown() else scrollUp()
} }
} }
KeyEvent.KEYCODE_VOLUME_UP -> { KeyEvent.KEYCODE_VOLUME_UP -> {
if (!config.volumeKeysEnabled || activity.menuVisible) { if (!config.volumeKeysEnabled || activity.viewModel.state.value.menuVisible) {
return false return false
} else if (isUp) { } else if (isUp) {
if (!config.volumeKeysInverted) scrollUp() else scrollDown() if (!config.volumeKeysInverted) scrollUp() else scrollDown()