Repository navigation
feat(reader): one return stack for explicit jumps and a book-wide page count - #90
Merged
Merged
Conversation
…e count Ports the navigation half of the iOS reader redesign (kmworks/kmreader#1351) without its chrome rework: the reader now remembers where every explicit jump started, names its position in pages across the whole book, and keeps split chapters under one title. Return stack. `linkBackStack` lived in the EPUB screen and carried a (chapterIndex, fraction) pair per in-book link, so the return affordance existed for links only: a jump through the table of contents, a bookmark or the progress slider was one-way, and the stack died with the composition. It is now `EpubPositionHistory` — pure push/pop over an immutable list, 50 entries, a 0.005 fraction tolerance so a tap repeated on the same page does not stack — with the state owned by the view model. Every explicit jump pushes (toc, bookmark, slider, in-book link) and nothing else does: page turns and chapter seams stay out, so "return" always means "the place I jumped from" rather than "undo my last page turn". `goToChapter` records only when the chapter actually changes; a same-chapter anchor jump records in the UI, where the page genuinely travels. Whole-book page count. The reader knew a chapter's page count from the paged measure pass and nothing else: "Chapter 3 / 12" was as good as it got, and a scrolled chapter had no page at all. `EpubBookPages` turns per-chapter counts into a running offset list; `resolveEpubBookPages` mixes measured chapters with estimates — a chapter the server placed N positions in gets N x (pages this session has measured / positions those same chapters carried), which absorbs the reader's font size without needing a text-density model — and the result lands in `EpubBookPageProgress(currentPage, totalPages)`. The overlay gained a `BOOK_POSITION` item ("Book 421 / 1,204", all three locales) and the chrome-visible footer now defaults to it, which is where iOS puts its "X of Y". Page-count cache. Reopening a book restarted the total from zero, because only the visited chapters had counts. `EpubPageCountStore` persists href -> pages per book (memory LRU plus cacheDir/epub-page-counts, the same atomic-rename shape as the positions index). Its identity is the spine AND a layout signature (flow style, font size, line height, margins, columns, viewport width, page insets) pushed in by the UI: a page count is a property of the layout that produced it, so a font-size change re-measures instead of printing a stale total. Chapter titles. A chapter split across several spine files named only its head; the tails fell back to "Chapter 13", "Chapter 14" in the contents. Titles now resolve as manifest link -> toc entry for that file (deeper entry wins) -> nearest preceding titled spine item -> positional fallback, and inheritance stops at chapters that carry a title of their own, so front matter cannot borrow the chapter that follows it. Also fixes a pothole this work exposed: a seek that landed inside the current chapter queued `scrollToFraction`, which only a chapter load consumes. The numbers moved while the page stood still, and the queued value also hid the WebView behind its 12s reveal backstop. `seekTo` now returns the fraction for the caller to scroll the live page with and leaves the reload path to chapter switches. Deliberately not ported from the iOS PR: the persistent top/bottom bar shell, the long-press scrubber with chapter ticks and callouts, and the fixed controls labels. The iOS preference-key precomputation has no counterpart here and was not added — overlay preferences serialize only on write, the page theme converts to JS once per chapter load or on a real theme change, and metrics are throttled to 200ms in the page JS, so nothing serializes per frame. Validated with :app:compileDebugKotlin, :app:testDebugUnitTest (16 new cases), :app:assembleDebug and :app:lintDebug. No device run yet: the jump/return behaviour and the page numbers on a real book still need eyes.
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
Three gaps against the iOS reader (kmworks/kmreader#1351), all in the reading model rather than the chrome:
linkBackStacklived in the EPUB screen and carried one(chapterIndex, fraction)pair per in-book link. A jump through the table of contents, a bookmark or the progress slider was one-way, and the stack died with the composition.ch12a/b/c.xhtml) and only the head carries the toc entry, so the tails were numbered — the contents read "Chapter 12, Chapter 13, Chapter 14" for what is one chapter.Root cause
The return stack was a screen-local convenience for the one jump path that needed it, not a property of navigation; page counts were treated as a per-chapter display value, never as a book-wide offset list with a cache; and chapter titles were read off the manifest's
readingOrderlink only, with no toc lookup and no notion that consecutive spine items can belong to the same chapter.A fourth defect surfaced while wiring the slider into the stack: a seek that landed inside the chapter already on screen queued
scrollToFraction, which only a chapter load consumes (pendingScrollinonPageReady). The numbers moved while the page stood still — and the queued value also clearedrestorePositioned, hiding the WebView behind its 12s reveal backstop.Fix
ui/reader/EpubPositionHistory.kt— the return stack as pure push/pop over an immutable list: 50 entries, and a 0.005 fraction tolerance so a tap repeated on the same page does not stack a second entry the return button would take the reader to pointlessly. The state now lives in the view model (positionStack: StateFlow).goToChapterrecords only when the chapter actually changes; a same-chapter anchor jump records in the UI, where the page genuinely travels, becauseselectChapternever reloads a chapter already on screen.core/epub/EpubBookPages.kt—EpubBookPageskeeps per-chapter counts and their prefix sums, so "chapter 7, page 3" becomes "page 421 of 8,431" in O(1), clamped inside its own chapter.resolveEpubBookPagesmixes measured chapters with estimates: a chapter the server placed N positions in getsN × (pages this session has measured ÷ positions those same chapters carried), which absorbs font size, margins and viewport without a text-density model; without positions a chapter gets the average of the measured ones. Exposed asEpubBookPageProgress(currentPage, totalPages)inEpubUiState.core/epub/EpubPageCountStore.kt— href → pages per book, in a memory LRU pluscacheDir/epub-page-counts(same atomic-rename shape as the positions index). Its identity is the spine and a layout signature (flow style, font size, line height, margins, columns, viewport width, page insets) pushed in by the UI: a page count is a property of the layout that produced it, so a font-size change re-measures instead of printing a stale total.BOOK_POSITION(bookPositionon the wire) — "Book 421 / 1,204" in all three locales. The chrome-visible footer now defaults to it, which is where the iOS reader puts its "X of Y" counter. It has no fraction fallback and hides while counts are missing: a page number is a claim.core/epub/EpubChapterTitles.kt— titles resolve as manifest link → toc entry for that file (a deeper entry beats its container) → nearest preceding titled spine item → positional fallback. Inheritance only advances on explicit titles, so front matter cannot borrow the chapter that follows it.seekTonow returns the in-chapter fraction to scroll the live page with when the seek stays in the current chapter, and leaves the state-restore path to chapter switches — fixing the no-op seek described above.onMetrics/onChapterPaginationcarry the flow style, so a scrolled chapter derives its book page from the fraction rather than claiming one page per chapter.What this deliberately does not change
preferencesCacheKeyprecomputation has no counterpart here and was not added: overlay preferences serialize only on write, the page theme converts to JS once per chapter load or on a real theme change, and metrics are throttled to 200ms inside the page JS. Nothing serializes per frame.1/0from the measure pass and is ignored by the page-count cache; those books get estimated counts from the server's positions instead.Validation
./gradlew :app:compileDebugKotlin./gradlew :app:testDebugUnitTest— 16 new cases (EpubBookPagesTest6,EpubChapterTitlesTest5,EpubPositionHistoryTest5),EpubOverlayTestextended for the new slot./gradlew :app:lintDebug— no new findings (the 4 warnings are pre-existing, baseline untouched)./gradlew :app:assembleDebugNo device run yet. Still to confirm on hardware: the return chip after each jump path (including a same-chapter toc anchor), the "X of Y" number settling as chapters are visited, and that a font-size change re-measures rather than showing the previous total.