Fix(reader): preserve reading progress across library round-trips#77
Merged
Conversation
uiState restore anchors (index, element key, offset fraction) are frozen at chapter-open and never updated during scrolling — scrolling only updates the controller's live progressState. Opening the full library route disposes the reader composition; returning recomposes it and re-runs runScrollRestore, which replayed the stale anchor: it snapped back and then persisted the stale position over the good one. The in-reader drawer never hit this (it keeps the reader composed), and a seek never did (it syncs uiState), which made it intermittent. Gate restore on a monotonic restoreRequestId bumped only at genuine restore points (calculateInitialPosition, seekToProgress). runScrollRestore resolves its anchor via consumeRestoreAnchor(): a genuine load/seek replays the uiState anchor unchanged, a bare recomposition re-applies the live progressState position with the from-bottom one-shot hard-nulled. Also covers rotation and other route pushes, same recomposition surface. Also harden healLibraryItemForChapterList: it wrote a whole row via updateItem (REPLACE outside progressMutex), a lost-update window against a concurrent progress write. It now writes only totalChapters/currentChapter/ hasUpdates via targeted column UPDATEs (healChapterMetadata).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Reading progress was lost when scrolling in a chapter, opening the full library screen, then returning to the reader. Intermittent, because it only triggered via the full library route (which disposes the reader composition), not the in-reader drawer, and never after a seek.
Root cause
uiState's restore anchors (scrollIndex,restoreElementKey,restoreOffsetFraction) are set at chapter-open/seek and never updated during scrolling — scrolling updates only the controller's liveprogressState. On return, the reader composition is recreated andrunScrollRestorere-fires, replaying the frozen anchor: it snapped the view back and a post-restore emission then persisted the stale position over the good one.Fix
restoreRequestId, bumped only at genuine restore points (calculateInitialPosition,seekToProgress).runScrollRestoreresolves its anchor viaconsumeRestoreAnchor():uiStateanchor (behavior unchanged);progressStateposition, with the from-bottom one-shot hard-nulled.Secondary hardening
healLibraryItemForChapterListwrote a whole row viaupdateItem(REPLACE) outsideprogressMutex— a lost-update window against a concurrent progress write. It now writes onlytotalChapters/currentChapter/hasUpdatesvia targeted column UPDATEs (healChapterMetadata), matching the refresh-path pattern from #71.Tests
./gradlew testStandardDebugUnitTest detekt→ 487 tests, 0 failures, detekt clean. Adds 7 tests (anchor-resolution branches, the counter increment, the targeted metadata write); retargets one existing test fromupdateItemtohealChapterMetadata.The unit suite covers the decision logic; the composable's dispose→recompose replay still warrants an on-device text-chapter round-trip check.