# mattromano/epub-reader architecture

> Architecture diagram of the GitHub repository mattromano/epub-reader, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mattromano/epub-reader
- Repository: https://github.com/mattromano/epub-reader

## Overview

Margins is an EPUB reading and personal reading-library app. Users import EPUBs into a catalog, open them in a Readium-backed reader, capture highlights and track reading sessions, then browse quotes and reading statistics. Supporting capabilities include full-text word indexing, configurable goals, backups, widgets, and optional Readwise export. The graph combines sampled call sites with the source index’s references; unsampled implementations and the absent README limit some relationships.

Main entry points: [`Margins/App/AppEnvironment.swift`](https://github.com/mattromano/epub-reader/blob/main/Margins/App/AppEnvironment.swift) (App environment), [`Margins/Features/Reader/ReaderViewModel.swift`](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderViewModel.swift) (Reader model), [`Packages/BookKit/Sources/Persistence/AppDatabase.swift`](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Persistence/AppDatabase.swift) (App database), [`Margins/App/MarginsApp.swift`](https://github.com/mattromano/epub-reader/blob/main/Margins/App/MarginsApp.swift) (App entry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["App and screens"]
  node_app_entry["App entry<br/>[MarginsApp.swift]"]
  node_environment["App environment"]
  node_quotes_ui["Quotes screen<br/>[QuotesView.swift]"]
  node_stats_ui["Statistics screen<br/>[StatsView.swift]"]
  node_settings_ui["Settings screen<br/>[SettingsView.swift]"]
end

subgraph group_reader["Reading experience"]
  node_reader_ui["Reader screen<br/>[ReaderView.swift]"]
  node_reader_model["Reader model"]
  node_reader_host["Readium host"]
  node_search["Reader search"]
  node_tracker["Session tracker"]
  node_highlight_capture["Highlight capture"]
  node_publication["Publication opening"]
end

subgraph group_library["Library and import"]
  node_library_ui["Library screen<br/>[LibraryView.swift]"]
  node_import["Import coordination"]
  node_index_service["Indexing service"]
  node_book_paths["Book file paths<br/>[BookPaths.swift]"]
  node_word_index["Word indexing<br/>[WordIndexer.swift]"]
end

subgraph group_bookkit["BookKit data and metrics"]
  node_database[("App database<br/>[AppDatabase.swift]")]
  node_metrics["Metrics queries"]
  node_text_normalizer["Text normalization"]
  node_backup["Library backup"]
end

subgraph group_integrations["Integrations"]
  node_readwise_service["Readwise service"]
  node_readwise_pusher["Readwise pushing"]
  node_readwise_client["Readwise client"]
  node_widget_publisher["Widget publishing"]
  node_widget["Home-screen widget"]
end

node_person(("Reader"))

node_person -->|"opens app"| node_app_entry
node_app_entry -->|"sets up"| node_environment
node_app_entry -->|"shows"| node_library_ui
node_app_entry -->|"shows"| node_quotes_ui
node_app_entry -->|"shows"| node_stats_ui
node_app_entry -->|"shows"| node_settings_ui
node_environment -->|"creates"| node_database
node_environment -->|"creates"| node_index_service
node_environment -->|"creates"| node_widget_publisher
node_environment -->|"creates"| node_readwise_service
node_library_ui -.->|"coordinates import"| node_import
node_import -.->|"parses EPUB"| node_publication
node_index_service -->|"reads and saves index"| node_database
node_index_service -->|"resolves file"| node_book_paths
node_index_service -->|"opens for indexing"| node_publication
node_index_service -->|"builds index"| node_word_index
node_person -->|"reads book"| node_reader_ui
node_reader_ui -.->|"uses model"| node_reader_model
node_reader_model -->|"reads and writes"| node_database
node_reader_model -->|"opens publication"| node_publication
node_reader_model -->|"controls navigation"| node_reader_host
node_reader_model -->|"uses search"| node_search
node_reader_model -->|"tracks sessions"| node_tracker
node_reader_host -->|"captures selection"| node_highlight_capture
node_reader_host -->|"creates highlight"| node_reader_model
node_highlight_capture -->|"normalizes quote"| node_text_normalizer
node_quotes_ui -->|"queries quotes"| node_database
node_stats_ui -->|"queries metrics"| node_metrics
node_metrics -->|"reads aggregates"| node_database
node_settings_ui -->|"updates settings"| node_database
node_settings_ui -->|"configures Readwise"| node_readwise_service
node_readwise_service -->|"pushes highlights"| node_readwise_pusher
node_readwise_service -->|"validates and sends"| node_readwise_client
node_readwise_service -->|"reads sync status"| node_database
node_widget_publisher -->|"reads widget data"| node_database
node_widget -.->|"opens deep links"| node_environment
node_environment -->|"publishes snapshot"| node_widget_publisher
node_settings_ui -.->|"offers backup"| node_backup

click node_app_entry "https://github.com/mattromano/epub-reader/blob/main/Margins/App/MarginsApp.swift"
click node_environment "https://github.com/mattromano/epub-reader/blob/main/Margins/App/AppEnvironment.swift"
click node_library_ui "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/LibraryView.swift"
click node_import "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/ImportCoordinator.swift"
click node_index_service "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/IndexingService.swift"
click node_book_paths "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/BookPaths.swift"
click node_word_index "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/WordIndexer.swift"
click node_reader_ui "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderView.swift"
click node_reader_model "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderViewModel.swift"
click node_reader_host "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderHostViewController.swift"
click node_search "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/SearchViewModel.swift"
click node_tracker "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Tracking/SessionTracker.swift"
click node_highlight_capture "https://github.com/mattromano/epub-reader/blob/main/Margins/ReadiumBridge/HighlightCapture.swift"
click node_publication "https://github.com/mattromano/epub-reader/blob/main/Margins/ReadiumBridge/PublicationOpening.swift"
click node_quotes_ui "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Quotes/QuotesView.swift"
click node_stats_ui "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Stats/StatsView.swift"
click node_settings_ui "https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Settings/SettingsView.swift"
click node_database "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Persistence/AppDatabase.swift"
click node_metrics "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Metrics/MetricsQueries.swift"
click node_text_normalizer "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/TextNormalizer.swift"
click node_readwise_service "https://github.com/mattromano/epub-reader/blob/main/Margins/App/ReadwiseService.swift"
click node_readwise_pusher "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Readwise/ReadwisePusher.swift"
click node_readwise_client "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Readwise/ReadwiseClient.swift"
click node_widget_publisher "https://github.com/mattromano/epub-reader/blob/main/Margins/App/WidgetPublisher.swift"
click node_widget "https://github.com/mattromano/epub-reader/blob/main/MarginsWidget/MarginsWidget.swift"
click node_backup "https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Persistence/LibraryBackup.swift"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_entry,node_environment,node_quotes_ui,node_stats_ui,node_settings_ui toneBlue
class node_reader_ui,node_reader_model,node_reader_host,node_search,node_tracker,node_highlight_capture,node_publication toneAmber
class node_library_ui,node_import,node_index_service,node_book_paths,node_word_index toneMint
class node_database,node_metrics,node_text_normalizer,node_backup toneRose
class node_readwise_service,node_readwise_pusher,node_readwise_client,node_widget_publisher,node_widget,node_person toneIndigo
```

## Components

### App and screens

- [App entry](https://github.com/mattromano/epub-reader/blob/main/Margins/App/MarginsApp.swift): `Margins/App/MarginsApp.swift`
- [App environment](https://github.com/mattromano/epub-reader/blob/main/Margins/App/AppEnvironment.swift): `Margins/App/AppEnvironment.swift`
- [Quotes screen](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Quotes/QuotesView.swift): `Margins/Features/Quotes/QuotesView.swift`
- [Statistics screen](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Stats/StatsView.swift): `Margins/Features/Stats/StatsView.swift`
- [Settings screen](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Settings/SettingsView.swift): `Margins/Features/Settings/SettingsView.swift`

### Reading experience

- [Reader screen](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderView.swift): `Margins/Features/Reader/ReaderView.swift`
- [Reader model](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderViewModel.swift): `Margins/Features/Reader/ReaderViewModel.swift`
- [Readium host](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/ReaderHostViewController.swift): `Margins/Features/Reader/ReaderHostViewController.swift`
- [Reader search](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Reader/SearchViewModel.swift): `Margins/Features/Reader/SearchViewModel.swift`
- [Session tracker](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Tracking/SessionTracker.swift): `Packages/BookKit/Sources/Tracking/SessionTracker.swift`
- [Highlight capture](https://github.com/mattromano/epub-reader/blob/main/Margins/ReadiumBridge/HighlightCapture.swift): `Margins/ReadiumBridge/HighlightCapture.swift`
- [Publication opening](https://github.com/mattromano/epub-reader/blob/main/Margins/ReadiumBridge/PublicationOpening.swift): `Margins/ReadiumBridge/PublicationOpening.swift`

### Library and import

- [Library screen](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/LibraryView.swift): `Margins/Features/Library/LibraryView.swift`
- [Import coordination](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/ImportCoordinator.swift): `Margins/Features/Library/ImportCoordinator.swift`
- [Indexing service](https://github.com/mattromano/epub-reader/blob/main/Margins/Features/Library/IndexingService.swift): `Margins/Features/Library/IndexingService.swift`
- [Book file paths](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/BookPaths.swift): `Packages/BookKit/Sources/Importing/BookPaths.swift`
- [Word indexing](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/WordIndexer.swift): `Packages/BookKit/Sources/Importing/WordIndexer.swift`

### BookKit data and metrics

- [App database](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Persistence/AppDatabase.swift): `Packages/BookKit/Sources/Persistence/AppDatabase.swift`
- [Metrics queries](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Metrics/MetricsQueries.swift): `Packages/BookKit/Sources/Metrics/MetricsQueries.swift`
- [Text normalization](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Importing/TextNormalizer.swift): `Packages/BookKit/Sources/Importing/TextNormalizer.swift`
- [Library backup](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Persistence/LibraryBackup.swift): `Packages/BookKit/Sources/Persistence/LibraryBackup.swift`

### Integrations

- [Readwise service](https://github.com/mattromano/epub-reader/blob/main/Margins/App/ReadwiseService.swift): `Margins/App/ReadwiseService.swift`
- [Readwise pushing](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Readwise/ReadwisePusher.swift): `Packages/BookKit/Sources/Readwise/ReadwisePusher.swift`
- [Readwise client](https://github.com/mattromano/epub-reader/blob/main/Packages/BookKit/Sources/Readwise/ReadwiseClient.swift): `Packages/BookKit/Sources/Readwise/ReadwiseClient.swift`
- [Widget publishing](https://github.com/mattromano/epub-reader/blob/main/Margins/App/WidgetPublisher.swift): `Margins/App/WidgetPublisher.swift`
- [Home-screen widget](https://github.com/mattromano/epub-reader/blob/main/MarginsWidget/MarginsWidget.swift): `MarginsWidget/MarginsWidget.swift`

### Other components

- **Reader**

## Connections

- Reader → App entry: opens app (evidence: `Margins/App/MarginsApp.swift`)
- App entry → App environment: sets up (evidence: `Margins/App/MarginsApp.swift`)
- App entry → Library screen: shows (evidence: `Margins/App/MarginsApp.swift`)
- App entry → Quotes screen: shows (evidence: `Margins/App/MarginsApp.swift`)
- App entry → Statistics screen: shows (evidence: `Margins/App/MarginsApp.swift`)
- App entry → Settings screen: shows (evidence: `Margins/App/MarginsApp.swift`)
- App environment → App database: creates (evidence: `Margins/App/AppEnvironment.swift`)
- App environment → Indexing service: creates (evidence: `Margins/App/AppEnvironment.swift`)
- App environment → Widget publishing: creates (evidence: `Margins/App/AppEnvironment.swift`)
- App environment → Readwise service: creates (evidence: `Margins/App/AppEnvironment.swift`)
- Library screen → Import coordination: coordinates import (evidence: `Margins/Features/Library/LibraryView.swift`)
- Import coordination → Publication opening: parses EPUB
- Indexing service → App database: reads and saves index (evidence: `Margins/Features/Library/IndexingService.swift`)
- Indexing service → Book file paths: resolves file (evidence: `Margins/Features/Library/IndexingService.swift`)
- Indexing service → Publication opening: opens for indexing (evidence: `Margins/Features/Library/IndexingService.swift`)
- Indexing service → Word indexing: builds index (evidence: `Margins/Features/Library/IndexingService.swift`)
- Reader → Reader screen: reads book
- Reader screen → Reader model: uses model
- Reader model → App database: reads and writes (evidence: `Margins/Features/Reader/ReaderViewModel.swift`)
- Reader model → Publication opening: opens publication (evidence: `Margins/Features/Reader/ReaderViewModel.swift`)
- Reader model → Readium host: controls navigation (evidence: `Margins/Features/Reader/ReaderViewModel.swift`)
- Reader model → Reader search: uses search (evidence: `Margins/Features/Reader/ReaderViewModel.swift`)
- Reader model → Session tracker: tracks sessions (evidence: `Margins/Features/Reader/ReaderViewModel.swift`)
- Readium host → Highlight capture: captures selection (evidence: `Margins/Features/Reader/ReaderHostViewController.swift`)
- Readium host → Reader model: creates highlight (evidence: `Margins/Features/Reader/ReaderHostViewController.swift`)
- Highlight capture → Text normalization: normalizes quote (evidence: `Margins/ReadiumBridge/HighlightCapture.swift`)
- Quotes screen → App database: queries quotes (evidence: `Margins/Features/Quotes/QuotesView.swift`)
- Statistics screen → Metrics queries: queries metrics (evidence: `Margins/Features/Stats/StatsView.swift`)
- Metrics queries → App database: reads aggregates (evidence: `Packages/BookKit/Sources/Metrics/MetricsQueries.swift`)
- Settings screen → App database: updates settings (evidence: `Margins/Features/Settings/SettingsView.swift`)
- Settings screen → Readwise service: configures Readwise (evidence: `Margins/Features/Settings/SettingsView.swift`)
- Readwise service → Readwise pushing: pushes highlights (evidence: `Margins/App/ReadwiseService.swift`)
- Readwise service → Readwise client: validates and sends (evidence: `Margins/App/ReadwiseService.swift`)
- Readwise service → App database: reads sync status (evidence: `Margins/App/ReadwiseService.swift`)
- Widget publishing → App database: reads widget data (evidence: `Margins/App/WidgetPublisher.swift`)
- Home-screen widget → App environment: opens deep links
- App environment → Widget publishing: publishes snapshot (evidence: `Margins/App/AppEnvironment.swift`)
- Settings screen → Library backup: offers backup
