Skip to content

feat(reader): one return stack for explicit jumps and a book-wide page count - #90

Merged
dyphire merged 1 commit into
mainfrom
feat/epub-reader-navigation-progress
Nov 28, 2026
Merged

dyphire merged 1 commit into
mainfrom
feat/epub-reader-navigation-progress

Conversation

@dyphire

@dyphire dyphire commented Nov 28, 2026

Copy link
Copy Markdown
Member

Problem

Three gaps against the iOS reader (kmworks/kmreader#1351), all in the reading model rather than the chrome:

  1. Return only worked after a link. linkBackStack lived 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.
  2. The reader had no idea where it was in the book. A chapter's page count came from the paged measure pass, so "Chapter 3 / 12" was as good as it got; a scrolled chapter had no page at all, and reopening a book restarted any total from zero.
  3. Split chapters lost their names. Books routinely split one chapter across several spine files (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 readingOrder link 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 (pendingScroll in onPageReady). The numbers moved while the page stood still — and the queued value also cleared restorePositioned, 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).
  • Every explicit jump pushes: table of contents, bookmark, progress slider, in-book link. Ordinary page turns and chapter seams stay out, so "return" means "the place I jumped from", not "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, because selectChapter never reloads a chapter already on screen.
  • core/epub/EpubBookPages.kt — EpubBookPages keeps 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. resolveEpubBookPages mixes measured chapters with estimates: a chapter the server placed N positions in gets N × (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 as EpubBookPageProgress(currentPage, totalPages) in EpubUiState.
  • core/epub/EpubPageCountStore.kt — href → pages per book, in a memory LRU plus cacheDir/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.
  • Overlay gained BOOK_POSITION (bookPosition on 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.
  • seekTo now 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 / onChapterPagination carry 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

  • The persistent top/bottom bar shell, the long-press scrubber with chapter ticks and its callout, and the fixed controls-state labels from the iOS PR are not ported — this is the navigation and page-model half only.
  • The iOS preferencesCacheKey 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 inside the page JS. Nothing serializes per frame.
  • Scrolled mode still reports 1/0 from 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 (EpubBookPagesTest 6, EpubChapterTitlesTest 5, EpubPositionHistoryTest 5), EpubOverlayTest extended for the new slot
  • ./gradlew :app:lintDebug — no new findings (the 4 warnings are pre-existing, baseline untouched)
  • ./gradlew :app:assembleDebug

No 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.

…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.
@dyphire
dyphire merged commit fdd432b into main Nov 28, 2026
1 check passed
@dyphire
dyphire deleted the feat/epub-reader-navigation-progress branch November 28, 2026 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant