From aea287c7c257b2958304246c4acb0bbc9eb19018 Mon Sep 17 00:00:00 2001 From: listlessbird <124798751+listlessbird@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:59:10 +0530 Subject: [PATCH 1/3] feat(mobile): send AZW3 with its cover and TXT as shared Only an EPUB is converted. An AZW3's cover is read at add and uploaded first; one without a readable cover still goes. --- apps/mobile/src/features/sending/book.test.ts | 52 ++++++++------- apps/mobile/src/features/sending/book.ts | 36 +++++++--- .../src/features/sending/sender.test.ts | 65 ++++++++++++++++++- apps/mobile/src/features/sending/sender.ts | 42 ++++++++---- 4 files changed, 150 insertions(+), 45 deletions(-) diff --git a/apps/mobile/src/features/sending/book.test.ts b/apps/mobile/src/features/sending/book.test.ts index 99168fa..5c4259d 100644 --- a/apps/mobile/src/features/sending/book.test.ts +++ b/apps/mobile/src/features/sending/book.test.ts @@ -13,7 +13,7 @@ import { formatOf, isUnconfirmed, leftInMailbox, - pdfItemName, + itemNameOf, transition, type Converted, } from "./book"; @@ -203,15 +203,18 @@ describe("leftInMailbox", () => { describe("item names", () => { const decodeName = Schema.decodeUnknownOption(ItemName); - it("makes a PDF's name safe for the Kindle's listing and file system", () => { - for (const [shared, expected] of [ - ["Gardens of the Moon.pdf", "Gardens of the Moon.pdf"], - ["Tab\tand\nnewline.PDF", "Tab_and_newline.pdf"], - ["a/b\\c:d.pdf", "a_b_c_d.pdf"], - [" .pdf", "untitled.pdf"], - ["no extension", "no extension.pdf"], + it("makes a shared file's name safe for the Kindle's listing and file system", () => { + for (const [shared, format, expected] of [ + ["Gardens of the Moon.pdf", "pdf", "Gardens of the Moon.pdf"], + ["Tab\tand\nnewline.PDF", "pdf", "Tab_and_newline.pdf"], + ["a/b\\c:d.pdf", "pdf", "a_b_c_d.pdf"], + [" .pdf", "pdf", "untitled.pdf"], + ["no extension", "pdf", "no extension.pdf"], + ["Dune.AZW3", "azw3", "Dune.azw3"], + ["notes.txt", "txt", "notes.txt"], + ["shared as text", "txt", "shared as text.txt"], ] as const) { - const name = pdfItemName(shared); + const name = itemNameOf(shared, format); assert.strictEqual(name, expected); assert.isTrue(Option.isSome(decodeName(name))); @@ -219,19 +222,24 @@ describe("item names", () => { }); it("knows a book by its type, or by its name when the sender does not know the type", () => { - assert.deepStrictEqual( - formatOf("book", Option.some("application/epub+zip")), - Option.some("epub"), - ); - assert.deepStrictEqual( - formatOf("Dune.EPUB", Option.some("application/octet-stream")), - Option.some("epub"), - ); - assert.deepStrictEqual(formatOf("Dune.pdf", Option.none()), Option.some("pdf")); - assert.deepStrictEqual( - formatOf("Dune.mobi", Option.some("application/octet-stream")), - Option.none(), - ); + for (const [name, type, expected] of [ + ["book", "application/epub+zip", "epub"], + ["Dune.EPUB", "application/octet-stream", "epub"], + ["Dune.pdf", undefined, "pdf"], + ["book", "application/vnd.amazon.mobi8-ebook", "azw3"], + ["book", "application/x-mobi8-ebook", "azw3"], + ["Dune.azw3", "application/octet-stream", "azw3"], + ["notes", "text/plain", "txt"], + ["notes.TXT", undefined, "txt"], + ["Dune.mobi", "application/octet-stream", undefined], + ["Dune.azw", "application/octet-stream", undefined], + ] as const) { + assert.deepStrictEqual( + formatOf(name, Option.fromNullishOr(type)), + Option.fromNullishOr(expected), + name, + ); + } }); }); diff --git a/apps/mobile/src/features/sending/book.ts b/apps/mobile/src/features/sending/book.ts index ea5d6c3..cd1025d 100644 --- a/apps/mobile/src/features/sending/book.ts +++ b/apps/mobile/src/features/sending/book.ts @@ -17,10 +17,13 @@ export const BookId = Schema.String.pipe(Schema.brand("BookId")); export type BookId = typeof BookId.Type; -export const BookFormat = Schema.Literals(["epub", "pdf"]); +export const BookFormat = Schema.Literals(["epub", "azw3", "pdf", "txt"]); export type BookFormat = typeof BookFormat.Type; +/** Only an EPUB is converted. The Kindle reads the other formats as shared. */ +export const isConvertible = (format: BookFormat) => format === "epub"; + /** The file as the user shared it. */ export const Source = Schema.Struct({ name: Schema.String, @@ -30,7 +33,10 @@ export const Source = Schema.Struct({ export type Source = typeof Source.Type; -/** What the Kindle receives. A PDF is its own book, with no thumbnail and no conversion time. */ +/** + * What the Kindle receives. A book sent as shared has no conversion time, and only an AZW3 that + * carries a cover has a thumbnail. + */ export const Converted = Schema.Struct({ title: Schema.String, author: Schema.String, @@ -295,35 +301,45 @@ const decodeFormat = Schema.decodeUnknownOption(BookFormat); const FORMAT_OF_TYPE: ReadonlyMap = new Map([ ["application/epub+zip", "epub"], + ["application/vnd.amazon.mobi8-ebook", "azw3"], + ["application/x-mobi8-ebook", "azw3"], ["application/pdf", "pdf"], + ["text/plain", "txt"], ]); -/** EPUB or PDF by MIME type, or by extension for senders that only say `application/octet-stream`. */ +const EXTENSION = /\.(epub|azw3|pdf|txt)$/i; + +/** The format by MIME type, or by extension for senders that only say `application/octet-stream`. */ export const formatOf = (name: string, mimeType: Option.Option) => Option.orElse( Option.flatMapNullishOr(mimeType, (type) => FORMAT_OF_TYPE.get(type)), - () => decodeFormat(/\.(epub|pdf)$/i.exec(name)?.[1]?.toLowerCase()), + () => decodeFormat(EXTENSION.exec(name)?.[1]?.toLowerCase()), ); -const stem = (name: string) => name.replace(/\.(epub|pdf)$/i, ""); +/** The shared file's name without a format's extension, the title until conversion finds one. */ +export const stem = (name: string) => name.replace(EXTENSION, ""); // What ts.sh's tab-separated listing and the Kindle's file system cannot take. const UNSAFE_IN_NAME = /[\u0000-\u001f\u007f/\\:*?"<>|]/g; /** The shared file's name, made safe for the mailbox. EPUBs keep the converter's names instead. */ -export const pdfItemName = (sharedName: string) => { +export const itemNameOf = (sharedName: string, format: BookFormat) => { const safe = stem(sharedName).replace(UNSAFE_IN_NAME, "_").trim().slice(0, 120); - return ItemName.make(`${safe === "" ? "untitled" : safe}.pdf`); + return ItemName.make(`${safe === "" ? "untitled" : safe}.${format}`); }; -/** A PDF goes to the Kindle as shared. */ -export const pdfConverted = (source: Source, name: ItemName): Converted => ({ +/** A book the Kindle reads as shared, with the cover an AZW3 carries. */ +export const sentAsShared = ( + source: Source, + name: ItemName, + thumbnail: Option.Option, +): Converted => ({ title: stem(source.name), author: "", book: name, bookBytes: source.bytes, - thumbnail: Option.none(), + thumbnail, convertedIn: Option.none(), }); diff --git a/apps/mobile/src/features/sending/sender.test.ts b/apps/mobile/src/features/sending/sender.test.ts index 26e04a3..727faaa 100644 --- a/apps/mobile/src/features/sending/sender.test.ts +++ b/apps/mobile/src/features/sending/sender.test.ts @@ -55,8 +55,15 @@ const epub = { mimeType: Option.some("application/epub+zip"), }; +const azw3 = { + uri: "content://downloads/2", + name: "House of Chains - Steven Erikson.azw3", + mimeType: Option.some("application/octet-stream"), +}; + interface Overrides { readonly convertEpub?: KindleCore["Service"]["convertEpub"]; + readonly deviceThumbnail?: KindleCore["Service"]["deviceThumbnail"]; /** Whether the book files on this phone are gone, as after the user cleared app storage. */ readonly filesMissing?: boolean; /** Stored rows this version of the app cannot read. */ @@ -147,7 +154,9 @@ const world = ( title: "House of Chains", author: "Steven Erikson", })), - deviceThumbnail: () => Effect.die(new Error("The sender never reads AZW3 thumbnails.")), + deviceThumbnail: + overrides.deviceThumbnail ?? + ((_, outDir) => Effect.succeed({ name: THUMBNAIL, path: `${outDir}/${THUMBNAIL}` })), }), ); @@ -343,6 +352,58 @@ describe("Sender", () => { }), ); + it.effect("sends an AZW3 as it is, with the cover it carries first", () => + Effect.gen(function* () { + const phone = yield* world([], [], { + convertEpub: () => Effect.die(new Error("An AZW3 is never converted.")), + }); + + yield* Effect.gen(function* () { + const sender = yield* Sender; + const id = yield* sender.add(azw3, true); + + yield* until(id, "Waiting"); + + assert.deepStrictEqual(yield* Ref.get(phone.uploads), [THUMBNAIL, BOOK]); + }).pipe(Effect.provide(phone.layer)); + }), + ); + + it.effect("sends an AZW3 without a cover when it carries none", () => + Effect.gen(function* () { + const phone = yield* world([], [], { + deviceThumbnail: () => + Effect.fail(new ConvertError({ stage: "Thumbnail", message: "no ASIN" })), + }); + + yield* Effect.gen(function* () { + const sender = yield* Sender; + const id = yield* sender.add(azw3, true); + + yield* until(id, "Waiting"); + + assert.deepStrictEqual(yield* Ref.get(phone.uploads), [BOOK]); + }).pipe(Effect.provide(phone.layer)); + }), + ); + + it.effect("sends a text file as it is", () => + Effect.gen(function* () { + const phone = yield* world([], [], { + convertEpub: () => Effect.die(new Error("A text file is never converted.")), + }); + + yield* Effect.gen(function* () { + const txt = { uri: "content://x", name: "notes.txt", mimeType: Option.some("text/plain") }; + const id = yield* Sender.use((sender) => sender.add(txt, true)); + + yield* until(id, "Waiting"); + + assert.deepStrictEqual(yield* Ref.get(phone.uploads), ["notes.txt"]); + }).pipe(Effect.provide(phone.layer)); + }), + ); + it.effect("waits at Ready for Send, then uploads", () => Effect.gen(function* () { const phone = yield* world([]); @@ -408,7 +469,7 @@ describe("Sender", () => { const error = yield* Effect.flip( Sender.use((sender) => sender.add( - { uri: "content://x", name: "notes.txt", mimeType: Option.some("text/plain") }, + { uri: "content://x", name: "notes.docx", mimeType: Option.some("application/msword") }, true, ), ).pipe(Effect.provide(phone.layer)), diff --git a/apps/mobile/src/features/sending/sender.ts b/apps/mobile/src/features/sending/sender.ts index b96cd48..11e6b2f 100644 --- a/apps/mobile/src/features/sending/sender.ts +++ b/apps/mobile/src/features/sending/sender.ts @@ -28,12 +28,13 @@ import { claimedItems, convertedOf, formatOf, + isConvertible, isInFlight, isUnconfirmed, + itemNameOf, itemNames, leftInMailbox, - pdfConverted, - pdfItemName, + sentAsShared, transition, type Book, type Converted, @@ -216,6 +217,21 @@ export const make = Effect.gen(function* () { ); }); + // An AZW3 without a readable cover, such as a personal document with no ASIN, still goes. + const coverOf = Effect.fn("Sender.coverOf")(function* (id: BookId, book: ItemName) { + return yield* kindleCore.deviceThumbnail(files.path(id, book), files.path(id)).pipe( + Effect.flatMap(({ name }) => decodeItemName(name)), + Effect.asSome, + Effect.catchCause((cause) => + Cause.hasInterruptsOnly(cause) + ? Effect.interrupt + : Effect.logWarning("Could not read an AZW3's cover.", cause).pipe( + Effect.as(Option.none()), + ), + ), + ); + }); + /** Puts the book's next item in the mailbox, or adopts the copy an interrupted attempt left. */ const uploadNext = Effect.fn("Sender.uploadNext")(function* ( id: BookId, @@ -425,13 +441,18 @@ export const make = Effect.gen(function* () { ); const id = yield* newId; - const pdfName = pdfItemName(file.name); - const storedAs = format === "epub" ? SOURCE_EPUB : pdfName; + const name = itemNameOf(file.name, format); + const storedAs = isConvertible(format) ? SOURCE_EPUB : name; // Also on interruption, as when the user leaves a share sheet while a large file copies. - const bytes = yield* files - .copyIn(id, file.uri, storedAs) - .pipe(Effect.onError(() => files.removeAll(id))); + const { bytes, thumbnail } = yield* Effect.gen(function* () { + const bytes = yield* files.copyIn(id, file.uri, storedAs); + + return { + bytes, + thumbnail: format === "azw3" ? yield* coverOf(id, name) : Option.none(), + }; + }).pipe(Effect.onError(() => files.removeAll(id))); const source = { name: file.name, format, bytes }; @@ -439,10 +460,9 @@ export const make = Effect.gen(function* () { id, addedAt: yield* DateTime.now, source, - status: - format === "epub" - ? BookStatus.cases.Converting.make({ confirmed }) - : BookStatus.cases.Ready.make({ converted: pdfConverted(source, pdfName) }), + status: isConvertible(format) + ? BookStatus.cases.Converting.make({ confirmed }) + : BookStatus.cases.Ready.make({ converted: sentAsShared(source, name, thumbnail) }), }; yield* put(confirmed ? transition(added, BookEvent.Confirmed()) : added); From 0905dfccdee93ddfe7e81339734792a2146efc9f Mon Sep 17 00:00:00 2001 From: listlessbird <124798751+listlessbird@users.noreply.github.com> Date: Sat, 21 Nov 2026 14:59:13 +0530 Subject: [PATCH 2/3] feat(mobile): share sheet and Choose a file accept AZW3 and TXT --- .agents/skills/kindle-drop-ui/SKILL.md | 6 +++--- .agents/skills/kindle-drop-ui/mockups.html | 2 +- .../android/src/main/AndroidManifest.xml | 6 +++++- apps/mobile/src/features/library/BookCover.tsx | 4 ++-- apps/mobile/src/features/library/LibraryScreen.tsx | 2 +- apps/mobile/src/features/library/chooseFile.ts | 13 ++++++++++--- apps/mobile/src/features/library/libraryCopy.ts | 4 ++-- apps/mobile/src/features/library/timeline.ts | 9 ++++++--- apps/mobile/src/features/share/ShareSheet.tsx | 4 ++-- 9 files changed, 32 insertions(+), 18 deletions(-) diff --git a/.agents/skills/kindle-drop-ui/SKILL.md b/.agents/skills/kindle-drop-ui/SKILL.md index 5d3f49f..9973c07 100644 --- a/.agents/skills/kindle-drop-ui/SKILL.md +++ b/.agents/skills/kindle-drop-ui/SKILL.md @@ -49,7 +49,7 @@ The mockups are in [mockups.html](mockups.html). Open it in a browser. - A status chip at the top shows "Kindle checked in 2 h ago" with a green dot. When the Kindle hasn't checked in for more than a day, the dot turns amber and the chip stays neutral. - Under a "Sending" section, each row has a cover, title, author, and a status line. Converting shows an indeterminate bar. Uploading shows a determinate bar and "Uploading · 46%". Waiting shows "Waiting for Kindle". - Under a "Recent" section, rows show "On Kindle · Yesterday" or, for failures, "Couldn't convert · Tap for details" in the danger color. Recent keeps books for 30 days, the mailbox's lifetime. A long-press clears a row. -- The empty state has the title "No books yet", the line "Share an EPUB or PDF from any app. It arrives the next time your Kindle wakes", and a "Choose a file" button. +- The empty state has the title "No books yet", the line "Share an EPUB, AZW3, PDF, or TXT file from any app. It arrives the next time your Kindle wakes", and a "Choose a file" button. **Share sheet.** The app opens as a bottom sheet over the app you shared from. - It shows the cover, title, author, and "EPUB · 9.1 MB", with **Send to Kindle** as the primary action and **Cancel** as a text button. @@ -59,7 +59,7 @@ The mockups are in [mockups.html](mockups.html). Open it in a browser. **Book** (detail). - The cover, title, and author are centered at the top. -- A four-step timeline follows: Converted (size and time), Uploaded (time), Waiting for Kindle ("Arrives the next time your Kindle wakes"), and On Kindle. +- A four-step timeline follows: Converted (size and time), Uploaded (time), Waiting for Kindle ("Arrives the next time your Kindle wakes"), and On Kindle. Only an EPUB is converted, so an AZW3, PDF, or TXT file starts at Uploaded. - The one action is "Cancel sending" while the book is waiting. For a failure, show the error copy and "Try again". **First run** (Connect your Kindle). @@ -80,7 +80,7 @@ At the bottom is "Disconnect Kindle" in the danger color, behind a confirmation | Situation | Copy | |---|---| | Conversion failed | Couldn't convert this book. Try another copy of the file. | -| Unsupported file | Kindle Drop sends EPUB and PDF files. | +| Unsupported file | Kindle Drop sends EPUB, AZW3, PDF, and TXT files. | | Upload failed, offline | Couldn't upload. Kindle Drop retries when you're back online. | | Upload failed, server | Couldn't upload. Try again in a few minutes. | | Pairing code unreadable | This isn't a Kindle Drop pairing code. | diff --git a/.agents/skills/kindle-drop-ui/mockups.html b/.agents/skills/kindle-drop-ui/mockups.html index 5bab3dd..c99126d 100644 --- a/.agents/skills/kindle-drop-ui/mockups.html +++ b/.agents/skills/kindle-drop-ui/mockups.html @@ -90,7 +90,7 @@

No books yet

-

Share an EPUB or PDF from any app. It arrives the next time your Kindle wakes.

+

Share an EPUB, AZW3, PDF, or TXT file from any app. It arrives the next time your Kindle wakes.

Choose a file
Empty librarySays what goes here and how. One action
diff --git a/apps/mobile/modules/share-sheet/android/src/main/AndroidManifest.xml b/apps/mobile/modules/share-sheet/android/src/main/AndroidManifest.xml index 01ea982..35b3558 100644 --- a/apps/mobile/modules/share-sheet/android/src/main/AndroidManifest.xml +++ b/apps/mobile/modules/share-sheet/android/src/main/AndroidManifest.xml @@ -13,8 +13,12 @@ + + - + + + diff --git a/apps/mobile/src/features/library/BookCover.tsx b/apps/mobile/src/features/library/BookCover.tsx index 5206f2a..a6dbcba 100644 --- a/apps/mobile/src/features/library/BookCover.tsx +++ b/apps/mobile/src/features/library/BookCover.tsx @@ -14,8 +14,8 @@ const SIZE = { } as const; /** - * The Kindle thumbnail the converter made, or a plain book before conversion, for PDFs, and while a - * shared file is still being copied in and has no book yet. + * The book's Kindle thumbnail, or a plain book before conversion, for a book without a cover, and + * while a shared file is still being copied in and has no book yet. */ export function BookCover(props: { readonly book?: Book; readonly size: keyof typeof SIZE }) { const { className, icon } = SIZE[props.size]; diff --git a/apps/mobile/src/features/library/LibraryScreen.tsx b/apps/mobile/src/features/library/LibraryScreen.tsx index f86e2ab..e83aeb7 100644 --- a/apps/mobile/src/features/library/LibraryScreen.tsx +++ b/apps/mobile/src/features/library/LibraryScreen.tsx @@ -174,7 +174,7 @@ export function LibraryScreen() { variant="plain" icon="book" title="No books yet" - detail="Share an EPUB or PDF from any app. It arrives the next time your Kindle wakes." + detail="Share an EPUB, AZW3, PDF, or TXT file from any app. It arrives the next time your Kindle wakes." action={