Skip to content

ADFA-4893 refactor: converge ZIM + Books onto one content-download engine - #476

Merged
luisguzman-adfa merged 7 commits into
mainfrom
refactor/ADFA-4893-content-download-convergence
Aug 26, 2026
Merged

luisguzman-adfa merged 7 commits into
mainfrom
refactor/ADFA-4893-content-download-convergence

Conversation

@luisguzman-adfa

Copy link
Copy Markdown
Collaborator

Internal refactor to remove the duplicated download logic between the ZIM and Books
streams. No user-facing change — behavior parity, verified on device.

Why

After Books was homologated to the ZIM contract, ZimDownloadService and
BooksDownloadService carried two near-identical copies of the same queue + REST
client + progress/pause/reconnect logic, and the two fragments carried two copies of
the control row. The cancel-purge fix had to be applied twice; each future change or
control (e.g. the planned "pause all" connection manager) would too. This makes it one
source, so fixes can't drift and there's a single seam to build on.

What

  • ContentDownloadSession — the shared engine: sequential queue, RestContentClient
    wiring, and all observed state (progress, paused, reconnect, heartbeat). One instance
    per service, so their static state never collides (ZIM and Books run concurrently).
  • ZimDownloadService / BooksDownloadService are now thin foreground shells: each
    keeps only its specifics (its arrays, its {ids}/{items} body, byte- vs item-count
    percent) and its notification, attaches as the session Host, and delegates the rest.
    Their public static API is unchanged, so every UI observer is untouched.
  • DownloadControls — one helper for the status-screen control row (Pause⇄Resume⇄Retry
    morph + always-Cancel), driven by a small Controller; both fragments bind and render it.
  • dash-node finalized to 1.2.7 (drops the -dev that shipped in ADFA-4893 — Books: homologate to the ZIM download contract #474; no code change).

Not changed

  • Behavior: same buttons, states, labels, cancel-purge, reconnect wording. Kolibri stays
    its own mechanism (out of scope).

Verification

  • ./gradlew :app:testDebugUnitTest :app:lintDebug green.
  • Device parity test on both streams (dash-node ≥ 1.2.7): normal download, Pause/Resume,
    Cancel (purges → re-select downloads), "Reconnecting… n/5" on a Wi-Fi drop, Retry on
    failure; plus Books idempotency (no duplicate re-upload) and the standalone Books flow.

…s-screen row

ZimPreparingFragment and BooksDownloadsFragment each carried an identical copy of
the control row (the Pause⇄Resume⇄Retry morph + always-Cancel wiring + visibility).
Extract it to one DownloadControls helper driven by a small Controller that
abstracts each stream's service; both fragments now bind it and call render().
No behavior change — same buttons, same states, same labels (reconnect/paused stay
per-fragment on their own detail line).
(package.json + CHANGELOG heading). No behavior change.
…tentDownloadSession

Extract the sequential-queue + RestContentClient wiring + progress/pause/reconnect
state that Zim/Books both carry into one ContentDownloadSession (one instance per
service, so their static state never collides). ZimDownloadService is now a thin
foreground shell: keeps the ZIM specifics (file/label/byte arrays, {ids:[..]} body,
byte-weighted percent) + the notification, attaches itself as the session Host, and
delegates the queue and all observed state. Public static API unchanged (UI
observers untouched). Cancel purges the session (unchanged behavior). Books next.
…nt (name clash)

The Host callback and the static UI getter were both overallPercent(); a static
method can't implement the interface method. Rename the per-type Host hook to
computeOverallPercent(); the static overallPercent() (UI) delegates to it through
the session. No behavior change.
…(converge with ZIM)

BooksDownloadService becomes a thin foreground shell like ZimDownloadService: it
keeps the Books specifics (id/title/url arrays, {items:[..]} body, item-count
percent) + the notification, attaches as the session Host, and delegates the queue
and all observed state to ContentDownloadSession. Removes the last copy of the
queue/client engine — ZIM and Books now share one mechanism. Public static API
unchanged (UI observers untouched); cancel purges the session.
Cancel purged the shared session state (status -> length 0) but left
ZimDownloadService's label/byte arrays populated, so ZimPreparingFragment
derived n from labels and indexed status[] off the end -> AIOOBE, which
killed the app (and took proot/dash-node down with it).

- ContentDownloadSession.purge() now calls Host.onPurged() so the owning
  service clears its per-type arrays together with the session state; the
  snapshot the UI reads is length-consistent after a Cancel.
- ZimPreparingFragment.render() derives n from the shortest of the arrays
  it indexes (labels/bytes/status) as defense in depth.

Books already popped back on empty, so it never tripped this.
Follow-up to the Cancel-crash fix: instead of each service keeping its own
label/size arrays that Host.onPurged() had to clear in lock-step with the
session's status[], the session now owns the whole per-item snapshot
(labels/sizes/bodies), set together with status in begin() and cleared
together in purge(). Length-consistency is now structural — status[] and the
metadata the UI indexes off it cannot diverge — so a future content type
can't reintroduce the AIOOBE by forgetting to clear an array.

- ContentDownloadSession holds labels/sizes/bodies; begin(labels,sizes,bodies);
  overallPercent() computed here (byte-weighted, count fallback for books).
- Host shrinks to notify(label)+stop(); buildBody/label/computeOverallPercent/
  onPurged removed. Services build the start bodies once and pass the snapshot in.
- ZimPreparingFragment keeps the min-length clamp as cheap defense in depth.
@luisguzman-adfa

Copy link
Copy Markdown
Collaborator Author

ADFA-4893: converge ZIM + Books onto one content-download engine

Both streams now run on a single ContentDownloadSession (sequential queue,
RestContentClient wiring, progress/pause/reconnect state) with a shared
DownloadControls (morph Pause/Resume/Retry + always-available Cancel). The
services are thin foreground shells that build their start bodies and the
notification; everything else is shared.

The session also owns the per-item snapshot (labels/sizes/bodies), set with
status in begin() and cleared with it in purge() — so status[] and the
metadata the UI indexes off it can't diverge. This fixes the Cancel-in-ZIM
crash (AIOOBE from a labels>0/status=0 snapshot, which killed the app and took
proot/dash-node down with it) at the structural level: a future content type
can't reintroduce it. overallPercent() is computed once here (byte-weighted,
count fallback for books).

Device-verified on ZIM + Books: normal/pause-resume/cancel/reconnect/retry/
index-bar/idempotency, 0 crashes. (ZIM retry verified via the shared code path
exercised in Books; direct ZIM fail->retry not deterministic under the harness.)

@luisguzman-adfa
luisguzman-adfa merged commit e375208 into main Aug 26, 2026
4 checks passed
@luisguzman-adfa
luisguzman-adfa deleted the refactor/ADFA-4893-content-download-convergence branch August 26, 2026 07:29
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