Repository navigation
ADFA-4893 refactor: converge ZIM + Books onto one content-download engine - #476
Conversation
…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.
|
ADFA-4893: converge ZIM + Books onto one content-download engine Both streams now run on a single ContentDownloadSession (sequential queue, The session also owns the per-item snapshot (labels/sizes/bodies), set with Device-verified on ZIM + Books: normal/pause-resume/cancel/reconnect/retry/ |
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,
ZimDownloadServiceandBooksDownloadServicecarried two near-identical copies of the same queue + RESTclient + 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,RestContentClientwiring, and all observed state (progress, paused, reconnect, heartbeat). One instance
per service, so their static state never collides (ZIM and Books run concurrently).
ZimDownloadService/BooksDownloadServiceare now thin foreground shells: eachkeeps only its specifics (its arrays, its
{ids}/{items}body, byte- vs item-countpercent) 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⇄Retrymorph + always-Cancel), driven by a small
Controller; both fragments bind and render it.1.2.7(drops the-devthat shipped in ADFA-4893 — Books: homologate to the ZIM download contract #474; no code change).Not changed
its own mechanism (out of scope).
Verification
./gradlew :app:testDebugUnitTest :app:lintDebuggreen.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.