Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .agents/skills/kindle-drop-ui/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand All @@ -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. |
Expand Down
2 changes: 1 addition & 1 deletion .agents/skills/kindle-drop-ui/mockups.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<div class="empty">
<svg width="44" height="44" viewBox="0 0 24 24" fill="none" stroke="#a1a1aa" stroke-width="1.5"><path d="M3 19a9 9 0 0 1 9 0a9 9 0 0 1 9 0"/><path d="M3 6a9 9 0 0 1 9 0a9 9 0 0 1 9 0"/><path d="M3 6v13M12 6v13M21 6v13"/></svg>
<h2 style="margin-top:6px">No books yet</h2>
<p>Share an EPUB or PDF from any app. It arrives the next time your Kindle wakes.</p>
<p>Share an EPUB, AZW3, PDF, or TXT file from any app. It arrives the next time your Kindle wakes.</p>
<div class="btn s" style="margin-top:14px;padding:0 22px">Choose a file</div>
</div></div>
<div class="cap">Empty library<span>Says what goes here and how. One action</span></div></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/epub+zip" />
<data android:mimeType="application/vnd.amazon.mobi8-ebook" />
<data android:mimeType="application/x-mobi8-ebook" />
<data android:mimeType="application/pdf" />
<!-- Many apps share an EPUB without knowing its type. -->
<!-- Also matches shared text with no file, which the sheet turns away as unsupported. -->
<data android:mimeType="text/plain" />
<!-- Many apps share an EPUB or AZW3 without knowing its type. -->
<data android:mimeType="application/octet-stream" />
</intent-filter>
</activity>
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/src/features/library/BookCover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/library/LibraryScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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={
<Button
tone="secondary"
Expand Down
13 changes: 10 additions & 3 deletions apps/mobile/src/features/library/chooseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ import { Atom } from "effect/reactivity";
import * as Runtime from "../../lib/runtime";
import { Sender } from "../sending/sender";

// Android's picker greys out files whose provider reports another type, and many report EPUBs as
// application/octet-stream.
const PICKABLE = ["application/epub+zip", "application/pdf", "application/octet-stream"];
// Android's picker greys out files whose provider reports another type, and many report EPUBs and
// AZW3s as application/octet-stream.
const PICKABLE = [
"application/epub+zip",
"application/vnd.amazon.mobi8-ebook",
"application/x-mobi8-ebook",
"application/pdf",
"text/plain",
"application/octet-stream",
];

/** Opens the system file picker and sends what the user picks. None when they back out. */
export const chooseFileAtom = Atom.runtime(Runtime.layer)
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/src/features/library/libraryCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { MailboxErrorReason } from "../../lib/mailboxClient";
import { relativeTime } from "../../lib/relativeTime";
import { BookStatus, type Book, type UploadFailure } from "../sending/book";

export const UNSUPPORTED_FILE = "Kindle Drop sends EPUB and PDF files.";
export const UNSUPPORTED_FILE = "Kindle Drop sends EPUB, AZW3, PDF, and TXT files.";

export const FILE_UNREADABLE = "Couldn't open this file. Try sharing it again.";

Expand All @@ -25,7 +25,7 @@ export const CANCEL_FAILED: Readonly<Record<MailboxErrorReason, string>> = {
unauthorized: "This pairing code no longer works. Create a new one on your computer.",
};

export const FORMAT_LABEL = { epub: "EPUB", pdf: "PDF" } as const;
export const FORMAT_LABEL = { epub: "EPUB", azw3: "AZW3", pdf: "PDF", txt: "TXT" } as const;

/** What went wrong, in the catalog's words, for a failed book. */
export const failureOf = (status: BookStatus) =>
Expand Down
13 changes: 8 additions & 5 deletions apps/mobile/src/features/library/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type * as DateTime from "effect/DateTime";
import * as Option from "effect/Option";

import { dayAndTime } from "../../lib/relativeTime";
import { megabytes, seconds } from "../../lib/units";
import { BookStatus, convertedOf, uploadedAt, type Book } from "../sending/book";
import { fileSize, seconds } from "../../lib/units";
import { BookStatus, convertedOf, isConvertible, uploadedAt, type Book } from "../sending/book";

export type StepState = "done" | "now" | "next" | "failed";

Expand All @@ -15,14 +15,17 @@ export interface Step {

const ARRIVES = "Arrives the next time your Kindle wakes";

/** Converted, Uploaded, Waiting for Kindle, On Kindle. A PDF has nothing to convert, so it starts at Uploaded. */
/**
* Converted, Uploaded, Waiting for Kindle, On Kindle. A book sent as shared has nothing to convert,
* so it starts at Uploaded.
*/
export const timelineOf = (
book: Book,
progress: Option.Option<number>,
now: DateTime.Utc,
): ReadonlyArray<Step> => {
const converted = Option.map(convertedOf(book.status), (c) =>
[megabytes(c.bookBytes), ...Option.toArray(Option.map(c.convertedIn, seconds))].join(" · "),
[fileSize(c.bookBytes), ...Option.toArray(Option.map(c.convertedIn, seconds))].join(" · "),
);

const step = (label: string, state: StepState, detail?: string): Step => ({
Expand Down Expand Up @@ -87,5 +90,5 @@ export const timelineOf = (
Clearing: () => [],
});

return book.source.format === "pdf" ? steps.slice(1) : steps;
return isConvertible(book.source.format) ? steps : steps.slice(1);
};
52 changes: 30 additions & 22 deletions apps/mobile/src/features/sending/book.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
formatOf,
isUnconfirmed,
leftInMailbox,
pdfItemName,
itemNameOf,
transition,
type Converted,
} from "./book";
Expand Down Expand Up @@ -203,35 +203,43 @@ 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<string>(name, expected);
assert.isTrue(Option.isSome(decodeName(name)));
}
});

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,
);
}
});
});

Expand Down
36 changes: 26 additions & 10 deletions apps/mobile/src/features/sending/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -295,35 +301,45 @@ const decodeFormat = Schema.decodeUnknownOption(BookFormat);

const FORMAT_OF_TYPE: ReadonlyMap<string, BookFormat> = 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<string>) =>
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<ItemName>,
): Converted => ({
title: stem(source.name),
author: "",
book: name,
bookBytes: source.bytes,
thumbnail: Option.none(),
thumbnail,
convertedIn: Option.none(),
});

Expand Down
65 changes: 63 additions & 2 deletions apps/mobile/src/features/sending/sender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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}` })),
}),
);

Expand Down Expand Up @@ -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([]);
Expand Down Expand Up @@ -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)),
Expand Down
Loading
Loading