# Reimplement Yokan by hand

**Courses:** This is Course 01, Yokan. [Course 02: Paperless backend](courses/paperless/README.md) covers backend architecture, DDD, the Anti-Corruption Layer, MapStruct, Kotlin versus Java, transactions, authorization, and testing. Both are available from the website’s course selector.

This is a guided reconstruction of the checkout at `/Users/duongph/Projects/yokan`, inspected on **2026-10-23**, including its three Git submodules and the directly vendored media compatibility module. It is intended for you to write the implementation yourself, in a new project, while keeping the original checkout available as a reference.

The baseline is main commit `8624561e94f713dda3f5834eb405d763038a8190`. The exact submodule commits, every tracked path, file hashes, declarations, schema changes and test names are recorded in [reference](reference/README.md). The [KMP architecture review](reference/architecture-review.md) adds identified improvements to the chapter implementations while preserving the original snapshot for comparison. This is not a recommendation to upgrade to today's newest libraries.

**Use the reviewed chapter listings and [reviewed source archive](reference/reviewed-source.zip) for your implementation.** Workbooks and browser source pages show the original pinned files. Changed chapter listings are explicitly marked and provide both versions; [the implementation manifest](reference/implementation-manifest.json) records their hashes and reasons.

## How to work through it

1. Read [the build and architecture chapter](chapters/01-build-and-architecture.md), then create an empty project alongside this guide. Do not overwrite the reference checkout.
2. Follow the numbered chapters. Chapters 03–26 now include their complete assigned source files directly under the relevant implementation steps, alongside code-flow explanations, cross-chapter dependencies, checks for understanding and complete test listings. Chapters 01–02 supply the build/domain walkthroughs. Some steps provide a temporary seam or fake; that is a milestone, not a finished feature.
3. Write each displayed file at its path in your new project. The chapter listings include imports, helpers, platform actuals, tests and configuration. Use the corresponding file workbook in [the coverage index](reference/README.md) as an indexed companion and completion checklist; it also provides exact-byte downloads. A behavior may need several files; the primary chapter assignment is organizational, not a dependency graph.
4. Use [model fields](reference/model-fields.md), [schema evolution](reference/schema-evolution.md), and [platform boundaries](reference/platform-boundaries.md) when defining storage and platform contracts. The declaration extracts are reading aids, not compilable implementations or substitutes for multiline source definitions.
5. Recreate the relevant checks in [the test behavior catalogue](reference/test-behaviors.md). Read the original assertions and fixtures, implement the behavior, and run your tests. Names alone are not specifications.
6. Complete the end-to-end acceptance matrix in chapter 25 and the coverage ledger before calling the reconstruction complete.

Use [IMPLEMENTATION-CHECKLIST.tsv](IMPLEMENTATION-CHECKLIST.tsv) as your editable progress ledger: it has one row per tracked path, its chapter, a place for your new implementation path/disposition and verification notes. It opens in a spreadsheet or text editor. The 26 workbooks contain **1,795 complete text listings and 44 binary downloads**, covering all 1,839 regular tracked files plus the three submodule boundaries.

The [pinned source archive](reference/pinned-source.zip) retains exact bytes and executable permissions, including required image/font/tooling assets. Keep it as a reference while you type the implementation; binary assets can be retained with their licenses. The archive includes submodule contents at the recorded revisions. SDKs, downloaded dependencies and external native runtimes still need the setup described in the build/platform chapters.

The [dependency checklist](reference/dependencies.md) records exact version pins and build read-points; [resource-keys.tsv](reference/resource-keys.tsv) inventories 13,046 resource entries and their format placeholders across the localized Compose XML files.

You can preserve the original packages and filenames for easy comparison, or choose new names and record a mapping. Preserve serialized names, public extension ABI names, protocol fields, database identities, resource identifiers and platform integration contracts where compatibility depends on them. You do not have to reproduce internal coding mistakes: follow the repository rule of immutable ViewModel state, `MutableStateFlow.update`, lifecycle-aware Android collection, stable lazy-list keys, and stateless content composables receiving callbacks.

## Reading order

| Step | Implement |
|---|---|
| 01 | [Build graph, pinned dependencies, architecture](chapters/01-build-and-architecture.md) |
| 02 | [Domain and settings models](chapters/02-domain-and-settings-models.md) |
| 03 | [Database backends and migrations](chapters/03-persistence-and-migrations.md) |
| 04 | [Profile ownership, activation, resource lifetimes](chapters/04-profiles-and-lifetimes.md) |
| 05 | [HTTP, cookies, WebViews and challenge handling](chapters/05-http-cookies-and-webviews.md) |
| 06 | [Source catalogs and extension runtimes](chapters/06-extension-runtimes.md) |
| 07 | [Signed declarative recipe catalogs](chapters/07-signed-recipe-catalogs.md) |
| 08 | [The yokan-ext submodule](chapters/08-yokan-ext-submodule.md) |
| 09 | [Repository, library and updates](chapters/09-repository-and-library.md) |
| 10 | [Navigation, UI shell and design system](chapters/10-navigation-and-design-system.md) |
| 11 | [Browsing, detail, source editing and migration](chapters/11-browse-detail-and-migration.md) |
| 12 | [Local files, archives and media library](chapters/12-local-library.md) |
| 13 | [Downloads, automation and processing](chapters/13-downloads-and-processing.md) |
| 14 | [Manga reader and colorization](chapters/14-manga-reader.md) |
| 15 | [Novel reader and speech](chapters/15-novel-reader.md) |
| 16 | [Anime player, streams, casting and torrents](chapters/16-anime-player.md) |
| 17 | [The kmp-mpv submodule](chapters/17-kmp-mpv-submodule.md) |
| 18 | [The vendored media compatibility module](chapters/18-media-compat-vendor.md) |
| 19 | [Metadata, discovery, home and trackers](chapters/19-metadata-discovery-and-trackers.md) |
| 20 | [Backup, restore and safety snapshots](chapters/20-backup-and-restore.md) |
| 21 | [Synchronization providers and conflict handling](chapters/21-synchronization.md) |
| 22 | [Together sessions](chapters/22-together.md) |
| 23 | [Android, desktop, iOS and browser hosts](chapters/23-platform-hosts-and-adapters.md) |
| 24 | [Settings, activity, background work and integrations](chapters/24-settings-and-integrations.md) |
| 25 | [Verification, packaging and releases](chapters/25-verification-and-release.md) |
| 26 | [Resources, documentation and development-skills submodule](chapters/26-resources-docs-and-development-skill.md) |

## Milestones that keep the work usable

- **M1, chapters 01–04:** a desktop window with an empty, persistent Default profile; round-trip models and safe shutdown.
- **M2, chapters 05–11:** enroll a source, browse, inspect a title, add it to the library, restart and recover it.
- **M3, chapters 12–18:** import local content, read manga/novels, play video, download and resume offline content. Build the player libraries independently before connecting the real native backend.
- **M4, chapters 19–22:** tracking, discovery, backup/restore, synchronization and shared sessions.
- **M5, chapters 23–26:** finish every platform adapter, settings page, resource, native package and release check. Run platform compilation throughout, not only at M5.

There are dependency cycles in the final graph, but not in the implementation process. Define contracts first, exercise them with in-memory/fake implementations, then supply actual platform services. `ServiceRegistry` explicitly closes the HTTP/WebView challenge-solving cycle and downloader/automation cycle in the composition root.

## Scope and evidence

The guide covers all tracked files, including tests, fixtures, resources, schemas, scripts, CI, platform hosts, vendor libraries and the skill submodule. Generated build outputs, downloaded native libraries, local SDK paths, caches, credentials and untracked IDE data are not source to rewrite. Committed IDE metadata is inventoried and can be regenerated where appropriate. External server implementations and third-party native runtimes are dependencies, not code contained in this checkout.

The chapter walkthroughs explain execution flow, integration contracts and failure paths, with complete code at the teaching steps. The workbooks provide a second indexed view of the tracked baseline. Declaration indexes are lexical navigation aids, not a claim that every function was individually audited or explained line by line. Exact UI dimensions, every DTO field, parser edge case and platform implementation are present in the full listings and tests. The original checkout is useful for comparison, but the generated guide and its raw source downloads retain the full snapshot independently.

Important current limitations: Aidoku's dispatcher is simulated; browser QuickJS is unsupported; browser/native storage capabilities differ; iOS simulator playback can require the compatibility backend. Older documents sometimes state that extra profiles are gated, whereas current activation code includes creation/switching and leases. Reconstruct the current platform capability checks rather than copying historical status claims.

The original inventory was validated for coverage, hashes and local links. The reviewed implementation has a separate [verification record](reference/architecture-verification.json) for executed Kotlin tests and platform compilation; this is not a claim that every native/device/live-service acceptance scenario passed. Follow chapter 25 to validate your complete reconstruction.

To regenerate the reference inventory after intentionally changing your reference baseline:

```sh
python3 tools/build_reference.py --source /Users/duongph/Projects/yokan --output .
python3 tools/validate_guide.py --source /Users/duongph/Projects/yokan --guide .
```

Regeneration updates the reference material, complete listings, raw files, source archive and inline chapter content. Review and update narrative claims separately; they are written for the pinned snapshot. To refresh only the workbooks/raw files from the existing pinned manifest, run `python3 tools/build_code_workbooks.py --source /Users/duongph/Projects/yokan`. Then run `npm run guide:chapters` to refresh inline Chapters 03–26 from the bundled snapshot.

Chapter step explanations, file placement and reading order are maintained in `tools/chapter-plans/*.json`. The chapter generator replaces only its marked content blocks and preserves the surrounding editorial prose. `npm run guide:validate` checks each inline file against the pinned bytes, in addition to the workbook and archive checks; a truncated listing fails validation.

Reviewed replacements and added tests live under `implementation/`, with the original project-relative paths. After intentionally editing them, run `python3 tools/reviewed_implementation.py --refresh` to update their hashes and export the reviewed archive, then regenerate chapters. The validator compares unchanged listings with pinned bytes and reviewed listings with their separately recorded reviewed hashes.

## Web interface

The same Markdown is also published as a local website with the learning path, saved chapter progress, search, a filterable file inventory and pinned source pages:

```sh
npm install
npm run dev        # builds dist/ and serves http://127.0.0.1:4322
npm run check      # verifies pages, links, anchors and source hashes
npm run test:browser  # headless Chrome checks; needs the preview server running
```

The site reads the bundled snapshot in `reference/source` and checks it against `reference/file-manifest.json`. Set `YOKAN_SOURCE` to explicitly read a different checkout with the same pinned bytes. The web build does not need the original application checkout when the bundled snapshot is present.
