diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 00000000..585bfdfa --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,126 @@ +# flutter_readium + +A Flutter plugin wrapping the [Readium](https://readium.org) toolkits for EPUB / audiobook / WebPub reading. The Dart API is shared across iOS, Android, and Web; each platform delegates to the matching native Readium toolkit. macOS registers a no-op stub only — native macOS is unsupported by upstream swift-toolkit. + +## Architecture + +Federated Flutter plugin with two pub packages: + +- `flutter_readium_platform_interface/` — shared Dart models, method-channel contract, abstract platform interface. +- `flutter_readium/` — app-facing package with native wrappers (iOS/Swift, Android/Kotlin, macOS stub) and a TypeScript→JS web implementation. + - `flutter_readium/example/` — canonical smoke-test app. Verify all UI/behavior changes here before declaring a task done. + +Data flow: +``` +FlutterReadium (singleton) + → FlutterReadiumPlatform (abstract) + → MethodChannelFlutterReadium + ├── iOS: Swift / swift-toolkit + ├── Android: Kotlin / kotlin-toolkit + └── Web: TypeScript / @readium/navigator (in webview via postMessage) +``` + +Channels defined in `MethodChannelFlutterReadium`: +- **Method channel** `flutter_readium` — request/response (open, navigate, preferences, …) +- **Event channels** (native→Dart): `flutter_readium/reader_status`, `flutter_readium/text_locator`, `flutter_readium/timebased_state`, `flutter_readium/error_event` + +Upstream toolkits (source of truth for native behavior — read on GitHub, don't decompile artifacts): +- iOS: https://github.com/readium/swift-toolkit/ (pinned in `flutter_readium/ios/flutter_readium.podspec`) +- Android: https://github.com/readium/kotlin-toolkit/ (pinned via `ext.readium_version` in `flutter_readium/android/build.gradle`) +- Web: `@readium/*` npm packages (see `flutter_readium/package.json`) + +## Commands + +Run all scripts from the repo root unless noted. + +| Command | Purpose | +|---------|---------| +| `bin/install` | Bootstrap: `pub get`, CocoaPods install, build web JS. Run after clone or dependency changes. | +| `bin/format` | Check Dart formatting across all three packages. Fails on any reformatting needed. | +| `bin/analyze` | `dart analyze --fatal-infos --fatal-warnings` across all packages. | +| `bin/update_web_example` | Build TS → JS and copy into `example/web/`. Run after editing TypeScript. | +| `bin/forAll ` | Run a command in both pub packages. | +| `bin/prepare-release ` | Bump versions, move Unreleased changelog entries, leave fresh Unreleased header. | + +**Run before any PR:** `bin/format && bin/analyze` + +### Tests + +```bash +# Unit tests (platform interface) +cd flutter_readium_platform_interface && flutter test + +# Unit tests (plugin) +cd flutter_readium && flutter test + +# Single test file +cd flutter_readium && flutter test test/some_test.dart + +# Integration tests (requires booted simulator/emulator) +cd flutter_readium/example +flutter test integration_test --device-id= # iOS +flutter test integration_test # Android +``` + +## Key Conventions + +### Models & serialization + +- Models use hand-written `toJson` / `fromJson` (via `JSONable` mixin). **Do not reintroduce `json_serializable` or `freezed` / build_runner codegen.** +- **Method channel serialization**: Readium-owned objects (`Locator`, `Decoration`, etc.) cross the bridge as **JSON strings** via `json.encode`. Plugin-owned flat structures use Maps/Dictionaries. + +### Method channel contract + +When adding a method-channel call, all three native sides (Swift, Kotlin, web) need a matching handler — or an explicit `UnimplementedError`. Undocumented silence looks like a bug. + +### Singleton pattern + +`FlutterReadium` is a singleton with one reader active at a time. Don't introduce per-instance state without reviewing the existing global publication lifecycle. + +### PDF locators + +PDF position lives in `Locator.locations.position` as a **1-based page number**. Don't invent plugin-side parallel models — use the upstream representation and round-trip via `goToLocator`. + +### Changelog & commits + +- Update `CHANGELOG.md` for every feature or bugfix. New entries go under `## Unreleased`. +- [Conventional Commits](https://www.conventionalcommits.org/) with scopes: `feat(android):`, `fix(ios):`, `chore(example):`, etc. Branch off `main`. + +> Platform-specific conventions (Android log format, navigator null guard, TypeScript locator serialization) are in the scoped instruction files under `.github/instructions/`. + +## MCP Servers + +Two MCP servers are configured in `.mcp.json`: + +**`dart`** — Dart tooling daemon. Prefer its tools over raw bash for Dart/Flutter work: +- `dart-run_tests` instead of `flutter test` in bash — structured pass/fail output, supports filtering by name +- `dart-analyze_files` instead of `dart analyze` — targeted per-file analysis +- `dart-dart_format` / `dart-dart_fix` — format or auto-fix specific files +- `dart-resolve_workspace_symbol` — find symbols by name (fuzzy) across all packages +- `dart-hover` — type info and docs at a cursor position +- `dart-pub` — `add`, `remove`, `get`, `upgrade` without leaving the tool interface + +**`mnemosyne`** *(optional, user-wide)* — persistent memory layer backed by a local SQLite file. Both Copilot CLI (`store_memory`) and Claude Code (auto memory) have native memory, so mnemosyne's main value here is as a **shared cross-agent store** — facts either agent writes are visible to the other. Available user-wide in Claude Code via `claude mcp add --scope user`. + +**`marionette`** — Flutter app remote control (requires a running example app). Use for all smoke testing: +- `marionette-connect` first (with the VM service URI from `flutter run` output, suffixed with `/ws`) +- `marionette-take_screenshots` — capture current visual state +- `marionette-get_interactive_elements` — discover tappable widgets and their keys +- `marionette-tap` / `marionette-enter_text` — interact with UI elements +- `marionette-get_logs` — read Flutter logs from the running app +- `marionette-hot_reload` / `marionette-hot_restart` — apply code changes without restarting + +## Smoke Testing (marionette) + +Run the example app in the background — `flutter run` never terminates: + +```bash +# Start app (async, background) +flutter run # poll output for "A Dart VM Service ... is available at: " +# Connect marionette +marionette register /ws +``` + +- Prefer `tap --key` / `tap --text` over coordinate taps (fragile). +- Add `ValueKey` to interactive widgets in the example app for reliable marionette targeting. +- PDF content is invisible to marionette screenshots (native platform view). Use `xcrun simctl io booted screenshot /tmp/screen.png` for visual verification; check `marionette get-logs` for `onPageChanged` locator position. diff --git a/.github/instructions/context7.instructions.md b/.github/instructions/context7.instructions.md new file mode 100644 index 00000000..4ab3e6f3 --- /dev/null +++ b/.github/instructions/context7.instructions.md @@ -0,0 +1,28 @@ +--- +applyTo: '**' +--- + +# Context7 MCP — Readium & Flutter Documentation + +Use the `context7` MCP server proactively when working with Readium toolkit APIs or Flutter/Dart framework APIs — **without waiting for the user to ask**. Your training data may be stale or miss recent API changes. + +## Two-step workflow + +1. `resolve-library-id` — map a library name to its Context7 ID. +2. `get-library-docs` — fetch relevant docs for that ID, scoped by a topic query. + +## Library IDs (pre-resolved) + +| Library | Context7 ID | +|---|---| +| Readium Swift Toolkit | `/readium/swift-toolkit` | +| Readium Kotlin Toolkit | `/readium/kotlin-toolkit` | +| Readium Web (TS) | `/readium/ts-toolkit` | +| Flutter | `/websites/flutter_dev` | +| Flutter API reference | `/websites/api_flutter_dev` | + +Use `get-library-docs` with a focused `topic` query, e.g.: +- `"Locator navigation"` for locator / navigator APIs +- `"EPUB decorations"` for highlight/decoration APIs +- `"TTS ReadingProgression"` for TTS APIs +- `"Publication resource loading"` for resource/fetcher APIs diff --git a/.github/instructions/dart.instructions.md b/.github/instructions/dart.instructions.md new file mode 100644 index 00000000..e22f18b9 --- /dev/null +++ b/.github/instructions/dart.instructions.md @@ -0,0 +1,447 @@ +--- +description: 'Instructions for writing Dart and Flutter code following the official recommendations.' +applyTo: '**/*.dart' +--- + +# Dart and Flutter + +Best practices recommended by the Dart and Flutter teams. These instructions were taken from [Effective Dart](https://dart.dev/effective-dart) and [Architecture Recommendations](https://docs.flutter.dev/app-architecture/recommendations). + +## Effective Dart + +Over the past several years, we've written a ton of Dart code and learned a lot about what works well and what doesn't. We're sharing this with you so you can write consistent, robust, fast code too. There are two overarching themes: + +1. **Be consistent.** When it comes to things like formatting, and casing, arguments about which is better are subjective and impossible to resolve. What we do know is that being *consistent* is objectively helpful. + + If two pieces of code look different it should be because they *are* different in some meaningful way. When a bit of code stands out and catches your eye, it should do so for a useful reason. + +2. **Be brief.** Dart was designed to be familiar, so it inherits many of the same statements and expressions as C, Java, JavaScript and other languages. But we created Dart because there is a lot of room to improve on what those languages offer. We added a bunch of features, from string interpolation to initializing formals, to help you express your intent more simply and easily. + + If there are multiple ways to say something, you should generally pick the most concise one. This is not to say you should `code golf` yourself into cramming a whole program into a single line. The goal is code that is *economical*, not *dense*. + +### The topics + +We split the guidelines into a few separate topics for easy digestion: + +* **Style** – This defines the rules for laying out and organizing code, or at least the parts that `dart format` doesn't handle for you. The style topic also specifies how identifiers are formatted: `camelCase`, `using_underscores`, etc. + +* **Documentation** – This tells you everything you need to know about what goes inside comments. Both doc comments and regular, run-of-the-mill code comments. + +* **Usage** – This teaches you how to make the best use of language features to implement behavior. If it's in a statement or expression, it's covered here. + +* **Design** – This is the softest topic, but the one with the widest scope. It covers what we've learned about designing consistent, usable APIs for libraries. If it's in a type signature or declaration, this goes over it. + +### How to read the topics + +Each topic is broken into a few sections. Sections contain a list of guidelines. Each guideline starts with one of these words: + +* **DO** guidelines describe practices that should always be followed. There will almost never be a valid reason to stray from them. + +* **DON'T** guidelines are the converse: things that are almost never a good idea. Hopefully, we don't have as many of these as other languages do because we have less historical baggage. + +* **PREFER** guidelines are practices that you *should* follow. However, there may be circumstances where it makes sense to do otherwise. Just make sure you understand the full implications of ignoring the guideline when you do. + +* **AVOID** guidelines are the dual to "prefer": stuff you shouldn't do but where there may be good reasons to on rare occasions. + +* **CONSIDER** guidelines are practices that you might or might not want to follow, depending on circumstances, precedents, and your own preference. + +Some guidelines describe an **exception** where the rule does *not* apply. When listed, the exceptions may not be exhaustive—you might still need to use your judgement on other cases. + +This sounds like the police are going to beat down your door if you don't have your laces tied correctly. Things aren't that bad. Most of the guidelines here are common sense and we're all reasonable people. The goal, as always, is nice, readable and maintainable code. + +### Rules + +#### Style + +##### Identifiers + +* DO name types using `UpperCamelCase`. +* DO name extensions using `UpperCamelCase`. +* DO name packages, directories, and source files using `lowercase_with_underscores`. +* DO name import prefixes using `lowercase_with_underscores`. +* DO name other identifiers using `lowerCamelCase`. +* PREFER using `lowerCamelCase` for constant names. +* DO capitalize acronyms and abbreviations longer than two letters like words. +* PREFER using wildcards for unused callback parameters. +* DON'T use a leading underscore for identifiers that aren't private. +* DON'T use prefix letters. +* DON'T explicitly name libraries. + +##### Ordering + +* DO place `dart:` imports before other imports. +* DO place `package:` imports before relative imports. +* DO specify exports in a separate section after all imports. +* DO sort sections alphabetically. + +##### Formatting + +* DO format your code using `dart format`. +* CONSIDER changing your code to make it more formatter-friendly. +* PREFER lines 80 characters or fewer. +* DO use curly braces for all flow control statements. + +#### Documentation + +##### Comments + +* DO format comments like sentences. +* DON'T use block comments for documentation. + +##### Doc comments + +* DO use `///` doc comments to document members and types. +* PREFER writing doc comments for public APIs. +* CONSIDER writing a library-level doc comment. +* CONSIDER writing doc comments for private APIs. +* DO start doc comments with a single-sentence summary. +* DO separate the first sentence of a doc comment into its own paragraph. +* AVOID redundancy with the surrounding context. +* PREFER starting comments of a function or method with third-person verbs if its main purpose is a side effect. +* PREFER starting a non-boolean variable or property comment with a noun phrase. +* PREFER starting a boolean variable or property comment with "Whether" followed by a noun or gerund phrase. +* PREFER a noun phrase or non-imperative verb phrase for a function or method if returning a value is its primary purpose. +* DON'T write documentation for both the getter and setter of a property. +* PREFER starting library or type comments with noun phrases. +* CONSIDER including code samples in doc comments. +* DO use square brackets in doc comments to refer to in-scope identifiers. +* DO use prose to explain parameters, return values, and exceptions. +* DO put doc comments before metadata annotations. + +##### Markdown + +* AVOID using markdown excessively. +* AVOID using HTML for formatting. +* PREFER backtick fences for code blocks. + +##### Writing + +* PREFER brevity. +* AVOID abbreviations and acronyms unless they are obvious. +* PREFER using "this" instead of "the" to refer to a member's instance. + +#### Usage + +##### Libraries + +* DO use strings in `part of` directives. +* DON'T import libraries that are inside the `src` directory of another package. +* DON'T allow an import path to reach into or out of `lib`. +* PREFER relative import paths. + +##### Null + +* DON'T explicitly initialize variables to `null`. +* DON'T use an explicit default value of `null`. +* DON'T use `true` or `false` in equality operations. +* AVOID `late` variables if you need to check whether they are initialized. +* CONSIDER type promotion or null-check patterns for using nullable types. + +##### Strings + +* DO use adjacent strings to concatenate string literals. +* PREFER using interpolation to compose strings and values. +* AVOID using curly braces in interpolation when not needed. + +##### Collections + +* DO use collection literals when possible. +* DON'T use `.length` to see if a collection is empty. +* AVOID using `Iterable.forEach()` with a function literal. +* DON'T use `List.from()` unless you intend to change the type of the result. +* DO use `whereType()` to filter a collection by type. +* DON'T use `cast()` when a nearby operation will do. +* AVOID using `cast()`. + +##### Functions + +* DO use a function declaration to bind a function to a name. +* DON'T create a lambda when a tear-off will do. + +##### Variables + +* DO follow a consistent rule for `var` and `final` on local variables. +* AVOID storing what you can calculate. + +##### Members + +* DON'T wrap a field in a getter and setter unnecessarily. +* PREFER using a `final` field to make a read-only property. +* CONSIDER using `=>` for simple members. +* DON'T use `this.` except to redirect to a named constructor or to avoid shadowing. +* DO initialize fields at their declaration when possible. + +##### Constructors + +* DO use initializing formals when possible. +* DON'T use `late` when a constructor initializer list will do. +* DO use `;` instead of `{}` for empty constructor bodies. +* DON'T use `new`. +* DON'T use `const` redundantly. + +##### Error handling + +* AVOID catches without `on` clauses. +* DON'T discard errors from catches without `on` clauses. +* DO throw objects that implement `Error` only for programmatic errors. +* DON'T explicitly catch `Error` or types that implement it. +* DO use `rethrow` to rethrow a caught exception. + +##### Asynchrony + +* PREFER async/await over using raw futures. +* DON'T use `async` when it has no useful effect. +* CONSIDER using higher-order methods to transform a stream. +* AVOID using Completer directly. +* DO test for `Future` when disambiguating a `FutureOr` whose type argument could be `Object`. + +#### Design + +##### Names + +* DO use terms consistently. +* AVOID abbreviations. +* PREFER putting the most descriptive noun last. +* CONSIDER making the code read like a sentence. +* PREFER a noun phrase for a non-boolean property or variable. +* PREFER a non-imperative verb phrase for a boolean property or variable. +* CONSIDER omitting the verb for a named boolean parameter. +* PREFER the "positive" name for a boolean property or variable. +* PREFER an imperative verb phrase for a function or method whose main purpose is a side effect. +* PREFER a noun phrase or non-imperative verb phrase for a function or method if returning a value is its primary purpose. +* CONSIDER an imperative verb phrase for a function or method if you want to draw attention to the work it performs. +* AVOID starting a method name with `get`. +* PREFER naming a method `to...()` if it copies the object's state to a new object. +* PREFER naming a method `as...()` if it returns a different representation backed by the original object. +* AVOID describing the parameters in the function's or method's name. +* DO follow existing mnemonic conventions when naming type parameters. + +##### Libraries + +* PREFER making declarations private. +* CONSIDER declaring multiple classes in the same library. + +##### Classes and mixins + +* AVOID defining a one-member abstract class when a simple function will do. +* AVOID defining a class that contains only static members. +* AVOID extending a class that isn't intended to be subclassed. +* DO use class modifiers to control if your class can be extended. +* AVOID implementing a class that isn't intended to be an interface. +* DO use class modifiers to control if your class can be an interface. +* PREFER defining a pure `mixin` or pure `class` to a `mixin class`. + +##### Constructors + +* CONSIDER making your constructor `const` if the class supports it. + +##### Members + +* PREFER making fields and top-level variables `final`. +* DO use getters for operations that conceptually access properties. +* DO use setters for operations that conceptually change properties. +* DON'T define a setter without a corresponding getter. +* AVOID using runtime type tests to fake overloading. +* AVOID public `late final` fields without initializers. +* AVOID returning nullable `Future`, `Stream`, and collection types. +* AVOID returning `this` from methods just to enable a fluent interface. + +##### Types + +* DO type annotate variables without initializers. +* DO type annotate fields and top-level variables if the type isn't obvious. +* DON'T redundantly type annotate initialized local variables. +* DO annotate return types on function declarations. +* DO annotate parameter types on function declarations. +* DON'T annotate inferred parameter types on function expressions. +* DON'T type annotate initializing formals. +* DO write type arguments on generic invocations that aren't inferred. +* DON'T write type arguments on generic invocations that are inferred. +* AVOID writing incomplete generic types. +* DO annotate with `dynamic` instead of letting inference fail. +* PREFER signatures in function type annotations. +* DON'T specify a return type for a setter. +* DON'T use the legacy typedef syntax. +* PREFER inline function types over typedefs. +* PREFER using function type syntax for parameters. +* AVOID using `dynamic` unless you want to disable static checking. +* DO use `Future` as the return type of asynchronous members that do not produce values. +* AVOID using `FutureOr` as a return type. + +##### Parameters + +* AVOID positional boolean parameters. +* AVOID optional positional parameters if the user may want to omit earlier parameters. +* AVOID mandatory parameters that accept a special "no argument" value. +* DO use inclusive start and exclusive end parameters to accept a range. + +##### Equality + +* DO override `hashCode` if you override `==`. +* DO make your `==` operator obey the mathematical rules of equality. +* AVOID defining custom equality for mutable classes. +* DON'T make the parameter to `==` nullable. + +--- + +## Flutter Architecture Recommendations + +This page presents architecture best practices, why they matter, and +whether we recommend them for your Flutter application. +You should treat these recommendations as recommendations, +and not steadfast rules, and you should +adapt them to your app's unique requirements. + +The best practices on this page have a priority, +which reflects how strongly the Flutter team recommends it. + +* **Strongly recommend:** You should always implement this recommendation if + you're starting to build a new application. You should strongly consider + refactoring an existing app to implement this practice unless doing so would + fundamentally clash with your current approach. +* **Recommend**: This practice will likely improve your app. +* **Conditional**: This practice can improve your app in certain circumstances. + +### Separation of concerns + +You should separate your app into a UI layer and a data layer. Within those layers, you should further separate logic into classes by responsibility. + +#### Use clearly defined data and UI layers. +**Strongly recommend** + +Separation of concerns is the most important architectural principle. +The data layer exposes application data to the rest of the app, and contains most of the business logic in your application. +The UI layer displays application data and listens for user events from users. The UI layer contains separate classes for UI logic and widgets. + +#### Use the repository pattern in the data layer. +**Strongly recommend** + +The repository pattern is a software design pattern that isolates the data access logic from the rest of the application. +It creates an abstraction layer between the application's business logic and the underlying data storage mechanisms (databases, APIs, file systems, etc.). +In practice, this means creating Repository classes and Service classes. + +#### Use ViewModels and Views in the UI layer. (MVVM) +**Strongly recommend** + +Separation of concerns is the most important architectural principle. +This particular separation makes your code much less error prone because your widgets remain "dumb". + +#### Use `ChangeNotifiers` and `Listenables` to handle widget updates. +**Conditional** + +> There are many options to handle state-management, and ultimately the decision comes down to personal preference. + +The `ChangeNotifier` API is part of the Flutter SDK, and is a convenient way to have your widgets observe changes in your ViewModels. + +#### Do not put logic in widgets. +**Strongly recommend** + +Logic should be encapsulated in methods on the ViewModel. The only logic a view should contain is: +* Simple if-statements to show and hide widgets based on a flag or nullable field in the ViewModel +* Animation logic that relies on the widget to calculate +* Layout logic based on device information, like screen size or orientation. +* Simple routing logic + +#### Use a domain layer. +**Conditional** + +> Use in apps with complex logic requirements. + +A domain layer is only needed if your application has exceeding complex logic that crowds your ViewModels, +or if you find yourself repeating logic in ViewModels. +In very large apps, use-cases are useful, but in most apps they add unnecessary overhead. + +### Handling data + +Handling data with care makes your code easier to understand, less error prone, and +prevents malformed or unexpected data from being created. + +#### Use unidirectional data flow. +**Strongly recommend** + +Data updates should only flow from the data layer to the UI layer. +Interactions in the UI layer are sent to the data layer where they're processed. + +#### Use `Commands` to handle events from user interaction. +**Recommend** + +Commands prevent rendering errors in your app, and standardize how the UI layer sends events to the data layer. + +#### Use immutable data models. +**Strongly recommend** + +Immutable data is crucial in ensuring that any necessary changes occur only in the proper place, usually the data or domain layer. +Because immutable objects can't be modified after creation, you must create a new instance to reflect changes. +This process prevents accidental updates in the UI layer and supports a clear, unidirectional data flow. + +#### Use freezed or built_value to generate immutable data models. +**Recommend** + +You can use packages to help generate useful functionality in your data models, `freezed` or `built_value`. +These can generate common model methods like JSON ser/des, deep equality checking and copy methods. +These code generation packages can add significant build time to your applications if you have a lot of models. + +#### Create separate API models and domain models. +**Conditional** + +> Use in large apps. + +Using separate models adds verbosity, but prevents complexity in ViewModels and use-cases. + +### App structure + +Well organized code benefits both the health of the app itself, and the team working on the code. + +#### Use dependency injection. +**Strongly recommend** + +Dependency injection prevents your app from having globally accessible objects, which makes your code less error prone. +We recommend you use the `provider` package to handle dependency injection. + +#### Use `go_router` for navigation. +**Recommend** + +Go_router is the preferred way to write 90% of Flutter applications. +There are some specific use-cases that go_router doesn't solve, +in which case you can use the `Flutter Navigator API` directly or try other packages found on `pub.dev`. + +#### Use standardized naming conventions for classes, files and directories. +**Recommend** + +We recommend naming classes for the architectural component they represent. +For example, you may have the following classes: + +* HomeViewModel +* HomeScreen +* UserRepository +* ClientApiService + +For clarity, we do not recommend using names that can be confused with objects from the Flutter SDK. +For example, you should put your shared widgets in a directory called `ui/core/`, +rather than a directory called `/widgets`. + +#### Use abstract repository classes +**Strongly recommend** + +Repository classes are the sources of truth for all data in your app, +and facilitate communication with external APIs. +Creating abstract repository classes allows you to create different implementations, +which can be used for different app environments, such as "development" and "staging". + +### Testing + +Good testing practices makes your app flexible. +It also makes it straightforward and low risk to add new logic and new UI. + +#### Test architectural components separately, and together. +**Strongly recommend** + +* Write unit tests for every service, repository and ViewModel class. These tests should test the logic of every method individually. +* Write widget tests for views. Testing routing and dependency injection are particularly important. + +#### Make fakes for testing (and write code that takes advantage of fakes.) +**Strongly recommend** + +Fakes aren't concerned with the inner workings of any given method as much +as they're concerned with inputs and outputs. If you have this in mind while writing application code, +you're forced to write modular, lightweight functions and classes with well defined inputs and outputs. diff --git a/.github/instructions/kotlin.instructions.md b/.github/instructions/kotlin.instructions.md new file mode 100644 index 00000000..58dd3a0e --- /dev/null +++ b/.github/instructions/kotlin.instructions.md @@ -0,0 +1,41 @@ +--- +description: Android/Kotlin conventions for the flutter_readium plugin. +applyTo: 'flutter_readium/android/**/*.kt' +--- + +# Kotlin Conventions + +## Formatting + +After writing or editing any Kotlin file, run `ktlint --format` on it (or `./gradlew ktlintFormat` from `flutter_readium/android/`). The `standard:package-name` violation for `dk.nota.flutter_readium` is pre-existing and cannot be auto-corrected — ignore it. Fix all other violations. + +## Log messages + +Every `PluginLog.*` call must start with `::functionName` — double colon followed by the exact name of the enclosing function. For lambdas, use the name of the enclosing named function. + +```kotlin +PluginLog.d(TAG, "::goBackward. Some detail.") +PluginLog.w(TAG, "::onLocatorChanged. Navigator not ready.") +``` + +Single-colon prefixes, missing prefixes, and copy-pasted wrong function names are all bugs. + +## Navigator null guard + +Every `suspend` function that needs the navigator must: +1. Capture it as a local variable with a `?: run { }` early-return guard. +2. Wrap direct navigator calls in `return withContext(coroutineContext) { }`. + +```kotlin +val navigator = epubNavigator ?: run { + PluginLog.w(TAG, "::myFunction. Navigator not ready.") + return +} +return withContext(coroutineContext) { navigator.someCall() } +``` + +Functions that only delegate to other wrapper functions (e.g. `evaluateJavascript`) do **not** need their own guard or `withContext`. + +## Method channel serialization + +Pass Readium-owned objects (`Locator`, `Decoration`, etc.) across the method channel as **JSON strings**. Use plain Maps only for flat plugin-owned structures with a shape fully controlled by this plugin. diff --git a/.github/instructions/typescript.instructions.md b/.github/instructions/typescript.instructions.md new file mode 100644 index 00000000..9e63bd11 --- /dev/null +++ b/.github/instructions/typescript.instructions.md @@ -0,0 +1,25 @@ +--- +description: Web TypeScript conventions for the flutter_readium plugin. +applyTo: 'flutter_readium/web/**/*.ts' +--- + +# TypeScript Conventions + +## Locator serialization + +`@readium/shared` Locators store extra location fields (e.g. `cssSelector`, `tocHref`) in `locations.otherLocations` as a `Map`. `JSON.stringify(locator)` **silently drops all Map entries**. + +- Always use `JSON.stringify(locator.serialize())` when emitting a Locator to Dart. +- Use `locator.serialize()` when embedding a locator inside a larger object before stringifying. +- For deep-clones: `JSON.parse(JSON.stringify(locator.serialize()))` — not `JSON.parse(JSON.stringify(locator))`. +- Read `otherLocations` via the Map API: `locator.locations?.otherLocations?.get('cssSelector')` — not `(locator.locations as any)?.cssSelector`. + +## Built JS + +Do **not** hand-edit the compiled JS in `example/web/`. Edit TS sources in `flutter_readium/web/_scripts/`, then run `bin/update_web_example` from the repo root to rebuild and copy the bundle. + +## Linting + +```bash +cd flutter_readium && npm run lint +``` diff --git a/.github/lsp.json b/.github/lsp.json new file mode 100644 index 00000000..59ca7237 --- /dev/null +++ b/.github/lsp.json @@ -0,0 +1,11 @@ +{ + "lspServers": { + "typescript": { + "command": "typescript-language-server", + "args": ["--stdio"], + "fileExtensions": { + ".ts": "typescript" + } + } + } +} diff --git a/.github/workflows/build-android.yml b/.github/workflows/build-android.yml index d9d4f68e..b87f08a0 100644 --- a/.github/workflows/build-android.yml +++ b/.github/workflows/build-android.yml @@ -32,6 +32,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Cache Gradle dependencies uses: actions/cache@v5 diff --git a/.github/workflows/build-ios.yml b/.github/workflows/build-ios.yml index 12b59bde..1833f4e4 100644 --- a/.github/workflows/build-ios.yml +++ b/.github/workflows/build-ios.yml @@ -16,6 +16,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Cache CocoaPods uses: actions/cache@v5 diff --git a/.github/workflows/build-web.yml b/.github/workflows/build-web.yml index 780273ce..2070b705 100644 --- a/.github/workflows/build-web.yml +++ b/.github/workflows/build-web.yml @@ -16,6 +16,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Install dependencies run: flutter pub get diff --git a/.github/workflows/integration-test-android.yml b/.github/workflows/integration-test-android.yml index baddf78d..48e40ace 100644 --- a/.github/workflows/integration-test-android.yml +++ b/.github/workflows/integration-test-android.yml @@ -45,6 +45,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Cache Gradle dependencies uses: actions/cache@v5 diff --git a/.github/workflows/integration-test-web.yml b/.github/workflows/integration-test-web.yml new file mode 100644 index 00000000..325b4f7f --- /dev/null +++ b/.github/workflows/integration-test-web.yml @@ -0,0 +1,42 @@ +name: Integration Tests (Android) + +on: + workflow_call: + +jobs: + + web: + name: Web (ChromeDriver) + runs-on: ubuntu-latest + timeout-minutes: 20 + continue-on-error: true # allowed to fail for now + + steps: + - uses: actions/checkout@v6 + + - uses: subosito/flutter-action@1a449444c387b1966244ae4d4f8c696479add0b2 # v2.23.0 + with: + channel: stable + flutter-version-file: .flutter-version + cache: true + pub-cache: true + + - name: Install Dart dependencies + run: flutter pub get + working-directory: flutter_readium/example + + - name: Install ChromeDriver + uses: nanasess/setup-chromedriver@c75c3d53d445b96d41dbf2355797b470953c6c30 # v2.4.0 + + - name: Start ChromeDriver + run: | + chromedriver --port=4444 & + sleep 2 + + - name: Run web integration tests + working-directory: flutter_readium/example + run: | + flutter drive \ + --driver=test_driver/integration_test.dart \ + --target=integration_test/plugin_integration_test.dart \ + -d chrome diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index f6ca48b6..b19ac582 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -34,6 +34,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Install dependencies (${{ matrix.package }}) run: flutter pub get @@ -64,6 +65,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Install dependencies (${{ matrix.package }}) run: flutter pub get diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index aabd05a2..c79aad96 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,6 +23,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Determine release version id: tag diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b1fcb6a6..c67c953c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -28,6 +28,7 @@ jobs: channel: stable flutter-version-file: .flutter-version cache: true + pub-cache: true - name: Install dependencies (platform interface) run: flutter pub get @@ -52,3 +53,24 @@ jobs: flutter_readium_platform_interface/coverage/lcov.info flutter_readium/coverage/lcov.info fail_ci_if_error: false + + web-test: + runs-on: ubuntu-latest + timeout-minutes: 15 + + steps: + - uses: actions/checkout@v6 + + - uses: actions/setup-node@v6 + with: + node-version: lts/* + cache: npm + cache-dependency-path: flutter_readium/package-lock.json + + - name: Install dependencies + run: npm ci + working-directory: flutter_readium + + - name: Run web unit tests + run: npm test + working-directory: flutter_readium diff --git a/.gitignore b/.gitignore index 2749419a..1e9fbce0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Claude Code (personal/local overrides only — .claude/settings.json IS committed) .claude/settings.local.json +.claire # Miscellaneous *.class @@ -89,3 +90,4 @@ assets/_scripts/dist **/.build/ flutter_readium/example/devtools_options.yaml tmp +.chromedriver/ diff --git a/.mcp.json b/.mcp.json index f1029fd0..77e51b99 100644 --- a/.mcp.json +++ b/.mcp.json @@ -8,6 +8,10 @@ "command": "dart", "args": ["run", "marionette_mcp"], "cwd": "flutter_readium/example" + }, + "context7": { + "command": "npx", + "args": ["-y", "@upstash/context7-mcp@3.0.0"] } } } diff --git a/.vscode/launch.json b/.vscode/launch.json index 0c55524c..eaecb881 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,6 +13,16 @@ "flutterMode": "debug", "preLaunchTask": "build_helper_scripts" }, + { + "name": "example (web)", + "cwd": "flutter_readium/example", + "program": "lib/main.dart", + "request": "launch", + "type": "dart", + "deviceId": "chrome", + "flutterMode": "debug", + "preLaunchTask": "build_web" + }, { "name": "example (profile mode)", "cwd": "flutter_readium/example", diff --git a/.vscode/mcp.json b/.vscode/mcp.json index a67d6b9e..8f41c148 100644 --- a/.vscode/mcp.json +++ b/.vscode/mcp.json @@ -1,5 +1,9 @@ { "servers": { + "dart": { + "command": "dart", + "args": ["mcp-server"] + }, "marionette": { "type": "stdio", "command": "dart", diff --git a/.vscode/tasks.json b/.vscode/tasks.json index ea4e0688..9c384a6a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -9,6 +9,14 @@ "problemMatcher": [], "label": "build_helper_scripts", "detail": "cross-env NODE_ENV=production IS_FLUTTER=1 webpack" + }, + { + "label": "build_web", + "type": "shell", + "command": "${workspaceFolder}/bin/update_web_example", + "group": "build", + "problemMatcher": [], + "detail": "Build TypeScript bundle and copy readiumReader.js to example/web/" } ] } diff --git a/CLAUDE.md b/CLAUDE.md index 41d187f2..68e51746 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -21,22 +21,27 @@ The native sides are thin wrappers around upstream Readium code — when debuggi When you need to inspect upstream implementation details (e.g. how a navigator handles a locator, what fields a model uses), read the source on GitHub — do NOT decompile local JARs, .framework bundles, or other build artifacts. Use `gh api` or `WebFetch` against the repos above. +If unsure about plugin architecture, be sure to read the README.md files, /docs/architecture.md and /docs/api-reference files. + Voice data for TTS comes from https://github.com/readium/speech (refreshed by `bin/update_readium_voice_data`). When upgrading any toolkit version, check that all three platforms move together where API surface overlaps — divergence between platforms is a recurring source of bugs. Keep the build/package files above as the source-of-truth, and avoid duplicating exact version numbers broadly in docs. ## Developer workflow -Key scripts (run from repo root): +Scripts in `bin/` source `bin/_common.sh`, so they're location-independent (run from any directory) and self-bootstrap the toolchain on PATH for non-interactive shells (CI / AI agents). Run them directly (`bash bin/