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>
This commit is contained in:
copilot-swe-agent[bot]
2025-10-06 09:39:53 +00:00
committed by Koitharu
parent aeb3732d75
commit 5f879f6c83

View File

@@ -25,18 +25,16 @@ abstract class BaseReaderFragment<B : ViewBinding> : BaseFragment<B>(), ZoomCont
readerAdapter = onCreateAdapter() readerAdapter = onCreateAdapter()
viewModel.content.observe(viewLifecycleOwner) { viewModel.content.observe(viewLifecycleOwner) {
// Use getCurrentState() to handle configuration changes (e.g., screen rotation) properly. // Determine which state to use for restoring position:
// getCurrentState() always has the most recent reading position, while content.state // - content.state: explicitly set state (e.g., after mode switch or chapter change)
// may contain the initial state from when content was first loaded. // - getCurrentState(): current reading position saved in SavedStateHandle
val currentState = viewModel.getCurrentState() val currentState = viewModel.getCurrentState()
if (it.state == null && it.pages.isNotEmpty() && readerAdapter?.hasItems != true) { val pendingState = when {
onPagesChanged(it.pages, currentState) it.state == null && it.pages.isNotEmpty() && readerAdapter?.hasItems != true -> currentState
} else if (currentState != null) { readerAdapter?.hasItems != true && it.state != currentState && currentState != null -> currentState
// If we have a current state, use it instead of content.state else -> it.state
onPagesChanged(it.pages, currentState)
} else {
onPagesChanged(it.pages, it.state)
} }
onPagesChanged(it.pages, pendingState)
} }
} }