Remove usage of savedInstanceState for storing reader menu visibility state

(cherry picked from commit bb8f3c63f1e5aade89ea3375c559c0e5dbef46ef)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderViewModel.kt
#	app/src/main/java/eu/kanade/tachiyomi/ui/reader/viewer/pager/PagerViewer.kt
This commit is contained in:
arkon 2023-07-01 12:47:20 -04:00 committed by Jobobby04
parent d2017081e2
commit 51752f497f
4 changed files with 59 additions and 73 deletions

View File

@ -131,7 +131,8 @@ 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 tachiyomi.domain.source.service.SourceManager import tachiyomi.domain.source.service.SourceManager
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import kotlin.math.abs import kotlin.math.abs
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
@ -155,8 +156,8 @@ class ReaderActivity : BaseActivity() {
const val SHIFTED_CHAP_INDEX = "shiftedChapterIndex" const val SHIFTED_CHAP_INDEX = "shiftedChapterIndex"
} }
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
@ -165,20 +166,8 @@ class ReaderActivity : BaseActivity() {
val hasCutout by lazy { hasDisplayCutout() } val hasCutout by lazy { hasDisplayCutout() }
/**
* Whether the menu is currently visible.
*/
var menuVisible = false
private set
// SY --> // SY -->
private var ehUtilsVisible = false private val sourceManager = Injekt.get<SourceManager>()
private val sourceManager: SourceManager by injectLazy()
private var lastShiftDoubleState: Boolean? = null
private var indexPageToShift: Int? = null
private var indexChapterToShift: Long? = null
// SY <-- // SY <--
/** /**
@ -187,7 +176,6 @@ class ReaderActivity : BaseActivity() {
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) }
@ -210,10 +198,10 @@ 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
// SY --> // SY -->
val page = intent.extras!!.getInt("page", -1).takeUnless { it == -1 } val page = intent.extras?.getInt("page", -1).takeUnless { it == -1 }
// SY <-- // SY <--
if (manga == -1L || chapter == -1L) { if (manga == -1L || chapter == -1L) {
finish() finish()
@ -232,18 +220,6 @@ class ReaderActivity : BaseActivity() {
} }
} }
if (savedInstanceState != null) {
menuVisible = savedInstanceState.getBoolean(::menuVisible.name)
// --> EH
ehUtilsVisible = savedInstanceState.getBoolean(::ehUtilsVisible.name)
// <-- EH
// SY -->
lastShiftDoubleState = savedInstanceState.get(SHIFT_DOUBLE_PAGES) as? Boolean
indexPageToShift = savedInstanceState.get(SHIFTED_PAGE_INDEX) as? Int
indexChapterToShift = savedInstanceState.get(SHIFTED_CHAP_INDEX) as? Long
// SY <--
}
config = ReaderConfig() config = ReaderConfig()
initializeMenu() initializeMenu()
@ -298,6 +274,7 @@ class ReaderActivity : BaseActivity() {
// SY --> // SY -->
private fun setEhUtilsVisibility(visible: Boolean) { private fun setEhUtilsVisibility(visible: Boolean) {
viewModel.showEhUtils(visible)
if (visible) { if (visible) {
binding.ehUtils.isVisible = true binding.ehUtils.isVisible = true
binding.expandEhButton.setImageResource(R.drawable.ic_keyboard_arrow_up_white_32dp) binding.expandEhButton.setImageResource(R.drawable.ic_keyboard_arrow_up_white_32dp)
@ -324,22 +301,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)
// EXH -->
outState.putBoolean(::ehUtilsVisible.name, ehUtilsVisible)
// EXH <--
// SY -->
(viewModel.state.value.viewer as? PagerViewer)?.let { pViewer ->
val config = pViewer.config
outState.putBoolean(SHIFT_DOUBLE_PAGES, config.shiftDoublePage)
if (config.shiftDoublePage && config.doublePages) {
pViewer.getShiftedPage()?.let {
outState.putInt(SHIFTED_PAGE_INDEX, it.index)
outState.putLong(SHIFTED_CHAP_INDEX, it.chapter.chapter.id ?: 0L)
}
}
}
// SY <--
viewModel.onSaveInstanceState() viewModel.onSaveInstanceState()
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
} }
@ -356,7 +317,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)
} }
/** /**
@ -366,7 +327,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)
} }
} }
@ -534,7 +495,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(
@ -635,10 +596,10 @@ class ReaderActivity : BaseActivity() {
// SY <-- // SY <--
// Set initial visibility // Set initial visibility
setMenuVisibility(menuVisible) setMenuVisibility(viewModel.state.value.menuVisible)
// --> EH // --> EH
setEhUtilsVisibility(ehUtilsVisible) setEhUtilsVisibility(viewModel.state.value.ehUtilsVisible)
// <-- EH // <-- EH
} }
@ -801,8 +762,9 @@ class ReaderActivity : BaseActivity() {
fun initDropdownMenu() { fun initDropdownMenu() {
binding.expandEhButton.setOnClickListener { binding.expandEhButton.setOnClickListener {
ehUtilsVisible = !ehUtilsVisible val newValue = !viewModel.state.value.ehUtilsVisible
setEhUtilsVisibility(ehUtilsVisible) viewModel.showEhUtils(newValue)
setEhUtilsVisibility(newValue)
} }
binding.ehAutoscrollFreq.setText( binding.ehAutoscrollFreq.setText(
@ -1058,7 +1020,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
@ -1169,7 +1131,7 @@ class ReaderActivity : BaseActivity() {
if (readerPreferences.pageLayout().get() == PagerConfig.PageLayout.AUTOMATIC) { if (readerPreferences.pageLayout().get() == PagerConfig.PageLayout.AUTOMATIC) {
setDoublePageMode(newViewer) setDoublePageMode(newViewer)
} }
lastShiftDoubleState?.let { newViewer.config.shiftDoublePage = it } viewModel.state.value.lastShiftDoubleState?.let { newViewer.config.shiftDoublePage = it }
} }
val defaultReaderType = manga.defaultReaderType(manga.mangaType(sourceName = sourceManager.get(manga.source)?.name)) val defaultReaderType = manga.defaultReaderType(manga.mangaType(sourceName = sourceManager.get(manga.source)?.name))
@ -1252,13 +1214,14 @@ class ReaderActivity : BaseActivity() {
private fun setChapters(viewerChapters: ViewerChapters) { private fun setChapters(viewerChapters: ViewerChapters) {
binding.readerContainer.removeView(loadingIndicator) binding.readerContainer.removeView(loadingIndicator)
// SY --> // SY -->
if (indexChapterToShift != null && indexPageToShift != null) { val state = viewModel.state.value
viewerChapters.currChapter.pages?.find { it.index == indexPageToShift && it.chapter.chapter.id == indexChapterToShift }?.let { if (state.indexChapterToShift != null && state.indexPageToShift != null) {
viewerChapters.currChapter.pages?.find { it.index == state.indexPageToShift && it.chapter.chapter.id == state.indexChapterToShift }?.let {
(viewModel.state.value.viewer as? PagerViewer)?.updateShifting(it) (viewModel.state.value.viewer as? PagerViewer)?.updateShifting(it)
} }
indexChapterToShift = null viewModel.setIndexChapterToShift(null)
indexPageToShift = null viewModel.setIndexPageToShift(null)
} else if (lastShiftDoubleState != null) { } else if (state.lastShiftDoubleState != null) {
val currentChapter = viewerChapters.currChapter val currentChapter = viewerChapters.currChapter
(viewModel.state.value.viewer as? PagerViewer)?.config?.shiftDoublePage = ( (viewModel.state.value.viewer as? PagerViewer)?.config?.shiftDoublePage = (
currentChapter.requestedPage + currentChapter.requestedPage +
@ -1385,14 +1348,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)
} }
} }
@ -1401,7 +1364,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)
} }
} }
@ -1630,7 +1593,7 @@ class ReaderActivity : BaseActivity() {
} }
// Trigger relayout // Trigger relayout
setMenuVisibility(menuVisible) setMenuVisibility(viewModel.state.value.menuVisible)
} }
/** /**

View File

@ -862,6 +862,24 @@ class ReaderViewModel(
) + filenameSuffix ) + filenameSuffix
} }
fun showMenus(visible: Boolean) {
mutableState.update { it.copy(menuVisible = visible) }
}
// SY -->
fun showEhUtils(visible: Boolean) {
mutableState.update { it.copy(ehUtilsVisible = visible) }
}
fun setIndexChapterToShift(index: Long?) {
mutableState.update { it.copy(indexChapterToShift = index) }
}
fun setIndexPageToShift(index: Int?) {
mutableState.update { it.copy(indexPageToShift = index) }
}
// SY <--
fun showLoadingDialog() { fun showLoadingDialog() {
mutableState.update { it.copy(dialog = Dialog.Loading) } mutableState.update { it.copy(dialog = Dialog.Loading) }
} }
@ -1199,11 +1217,16 @@ class ReaderViewModel(
*/ */
val viewer: Viewer? = null, val viewer: Viewer? = null,
val dialog: Dialog? = null, val dialog: Dialog? = null,
val menuVisible: Boolean = false,
// SY --> // SY -->
val currentPageText: String = "", val currentPageText: String = "",
val meta: RaisedSearchMetadata? = null, val meta: RaisedSearchMetadata? = null,
val mergedManga: Map<Long, Manga>? = null, val mergedManga: Map<Long, Manga>? = null,
val ehUtilsVisible: Boolean = false,
val lastShiftDoubleState: Boolean? = null,
val indexPageToShift: Int? = null,
val indexChapterToShift: Long? = null,
// SY <-- // SY <--
) { ) {
val totalPages: Int val totalPages: Int

View File

@ -121,7 +121,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.joinedItems.getOrNull(pager.currentItem) val item = adapter.joinedItems.getOrNull(pager.currentItem)
val firstPage = item?.first as? ReaderPage val firstPage = item?.first as? ReaderPage
val secondPage = item?.second as? ReaderPage val secondPage = item?.second as? ReaderPage
@ -398,14 +398,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

@ -94,7 +94,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()
} }
@ -123,7 +123,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)
@ -348,14 +348,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()