From 5f879f6c833f196e3f2b01e8bd96850d7ea4ce48 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 6 Oct 2025 09:39:53 +0000 Subject: [PATCH] Fix page position loss when switching reader modes - Compare content.state with getCurrentState() to detect configuration changes vs intentional updates - Use content.state when they match (mode switch case) to preserve explicit state updates - Use getCurrentState() when they differ (rotation case) to restore saved position - This ensures both screen rotation and mode switching work correctly Co-authored-by: NathanBap <101987516+NathanBap@users.noreply.github.com> --- .../reader/ui/pager/BaseReaderFragment.kt | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt index c1acaeda0..7b296129c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/pager/BaseReaderFragment.kt @@ -25,18 +25,16 @@ abstract class BaseReaderFragment : BaseFragment(), ZoomCont readerAdapter = onCreateAdapter() viewModel.content.observe(viewLifecycleOwner) { - // Use getCurrentState() to handle configuration changes (e.g., screen rotation) properly. - // getCurrentState() always has the most recent reading position, while content.state - // may contain the initial state from when content was first loaded. + // Determine which state to use for restoring position: + // - content.state: explicitly set state (e.g., after mode switch or chapter change) + // - getCurrentState(): current reading position saved in SavedStateHandle val currentState = viewModel.getCurrentState() - if (it.state == null && it.pages.isNotEmpty() && readerAdapter?.hasItems != true) { - onPagesChanged(it.pages, currentState) - } else if (currentState != null) { - // If we have a current state, use it instead of content.state - onPagesChanged(it.pages, currentState) - } else { - onPagesChanged(it.pages, it.state) + val pendingState = when { + it.state == null && it.pages.isNotEmpty() && readerAdapter?.hasItems != true -> currentState + readerAdapter?.hasItems != true && it.state != currentState && currentState != null -> currentState + else -> it.state } + onPagesChanged(it.pages, pendingState) } }