architecture.html
docs/architecture.html
1<!doctype html>2<html lang="en">3<head>4 <meta charset="utf-8">5 <meta name="viewport" content="width=device-width, initial-scale=1">6 <meta name="description" content="Trace Yokan from native platform entry points through shared Kotlin, Compose UI, services, and Room persistence.">7 <meta name="theme-color" content="#172033">8 <title>Architecture · Yokan KMP Field Guide</title>9 <link rel="icon" href="./assets/mark.svg" type="image/svg+xml">10 <link rel="stylesheet" href="./assets/styles.css">11 <script>try{const t=localStorage.getItem('yokan-docs-theme');document.documentElement.dataset.theme=t||(matchMedia('(prefers-color-scheme: dark)').matches?'dark':'light')}catch{}</script>12 <script defer src="./assets/app.js"></script>13</head>14<body data-page="architecture">15 <a class="skip-link" href="#main-content">Skip to content</a>16 <header data-site-header></header>1718 <div class="page-shell">19 <aside class="side-nav" aria-label="Guide chapters">20 <span class="nav-label">Field guide</span>21 <ul>22 <li><a href="./getting-started.html">01 · Get started</a></li>23 <li><a href="./architecture.html" aria-current="page">02 · Architecture</a></li>24 <li><a href="./patterns.html">03 · Patterns</a></li>25 </ul>26 </aside>2728 <main id="main-content" class="page-main">29 <header class="chapter-header">30 <nav class="breadcrumbs" aria-label="Breadcrumb"><a href="./index.html">Field guide</a><span>/</span><span>Architecture</span></nav>31 <h1>Trace the shared core.</h1>32 <p>Follow Yokan from each native entry point to one shared composition, then down through state, services, repositories, and Room.</p>33 <div class="chapter-meta"><span class="pill">25–35 minutes</span><span class="pill">Source-set boundaries</span><span class="pill">End-to-end trace</span></div>34 </header>3536 <section class="doc-section" aria-labelledby="architecture-thesis">37 <h2 id="architecture-thesis">The architecture in one sentence</h2>38 <p>Yokan shares product behavior and most UI in <code>commonMain</code>, while thin target hosts and small <code>actual</code> implementations provide capabilities that require operating-system APIs.</p>39 <div class="flow-diagram" aria-label="Yokan application layers">40 <div class="flow-step"><small>Platform host</small><strong>Launch + OS context</strong></div>41 <div class="flow-step"><small>Shared UI</small><strong>Routes + ViewModels</strong></div>42 <div class="flow-step"><small>Application</small><strong>Holders + services</strong></div>43 <div class="flow-step"><small>Data</small><strong>Repository + Room</strong></div>44 </div>45 <div class="callout kotlin">46 <strong>The boundary is a design decision.</strong>47 <p>A file belongs in <code>commonMain</code> when its behavior is meaningful on every target and its dependencies are portable. It belongs in a platform source set when it must speak to an OS framework or target-only library.</p>48 </div>49 </section>5051 <section class="doc-section" aria-labelledby="three-entry-points">52 <h2 id="three-entry-points">Three entry points, one composition</h2>53 <p>Each app starts in the idiom of its platform and renders the same <code>YokanApp()</code>, which bootstraps shared services off the main thread before showing the UI.</p>54 <table class="comparison">55 <thead><tr><th>Target</th><th>Native entry</th><th>Shared handoff</th></tr></thead>56 <tbody>57 <tr><td>Android</td><td><code>YokanApplication</code> + <code>MainActivity : ComponentActivity</code></td><td><code>setContent { YokanApp() }</code></td></tr>58 <tr><td>Desktop</td><td><code>fun main(args)</code> + Compose <code>Window</code></td><td><code>Window { YokanApp() }</code></td></tr>59 <tr><td>iOS</td><td>SwiftUI <code>@main</code> application</td><td><code>ComposeUIViewController { YokanApp() }</code></td></tr>60 </tbody>61 </table>6263 <h3 id="android-entry">Android host</h3>64 <div class="code-block">65 <div class="code-label"><span>androidMain/AndroidAppContext.kt + MainActivity.kt</span></div>66 <pre><code>class YokanApplication : Application(), BackgroundTaskHost {67 override fun onCreate() {68 super.onCreate()69 AndroidAppContext.appContext = applicationContext70 }71}7273class MainActivity : ComponentActivity() {74 override fun onCreate(savedInstanceState: Bundle?) {75 super.onCreate(savedInstanceState)76 setContent { YokanApp() }77 }78}</code></pre>79 </div>8081 <h3 id="desktop-entry">Desktop host</h3>82 <div class="code-block">83 <div class="code-label"><span>desktopMain/eu/friedcutlet/yokan/Main.kt</span></div>84 <pre><code>fun main(args: Array<String>) {85 DesktopBootstrapState.args = args86 YokanRuntime.bootstrap()87 application {88 Window(onCloseRequest = ::exitApplication, title = "Yokan") {89 YokanApp()90 }91 }92}</code></pre>93 </div>9495 <h3 id="ios-entry">iOS host</h3>96 <div class="code-block">97 <div class="code-label"><span>iosMain/MainViewController.kt + iosApp/YokanApp.swift</span></div>98 <pre><code>fun MainViewController() = ComposeUIViewController {99 YokanApp()100}101102struct ComposeRootView: UIViewControllerRepresentable {103 func makeUIViewController(context: Context) -> UIViewController {104 MainViewControllerKt.MainViewController()105 }106}</code></pre>107 </div>108 <p>The Swift app owns the iOS lifecycle; Kotlin supplies a regular <code>UIViewController</code>. This keeps native integration possible without duplicating the feature UI.</p>109 </section>110111 <section class="doc-section" aria-labelledby="bootstrap-runtime">112 <h2 id="bootstrap-runtime">Bootstrap and dependency ownership</h2>113 <p><a href="https://codeberg.org/FriedCutlet/yokan/src/branch/main/composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/app/YokanRuntime.kt"><code>YokanRuntime</code></a> (package <code>eu.friedcutlet.yokan.app</code>, next to <code>ServiceRegistry.kt</code> and <code>AppBootstrap.kt</code>) is the composition root. Its lazy properties create the database, repository, service registry, app-scoped state holders (<code>settings</code>, <code>libraryIndex</code>, <code>libraryUpdates</code>, <code>localLibrary</code>, <code>migration</code>, <code>chapterDownloads</code>, <code>sourceInstall</code>), platform controllers, and background work only when needed, exactly once per process.</p>114 <div class="code-block">115 <div class="code-label"><span>commonMain/app/YokanRuntime.kt</span></div>116 <pre><code>object YokanRuntime {117 val database: YokanDatabase by lazy { createDefaultDatabase() }118 val services: YokanServices by lazy {119 defaultServices(database, localFiles, localArchive, notifications)120 }121 val repository: YokanRepository by lazy {122 DefaultYokanRepository(database, services.downloader)123 }124125 // Idempotent and thread-safe; OS workers pass background = true.126 fun bootstrap(input: PlatformBootstrapInput = platformBootstrapInput(), background: Boolean = false) {127 // install crash handling, run startup checks, start foreground services…128 }129}</code></pre>130 </div>131 <p>Only the composition root reads <code>YokanRuntime</code>: <code>App.kt</code>, platform entry points, and the route adapters that build each screen's ViewModel. Everything below receives its dependencies through constructors, so tests pass fakes and in-memory databases without touching the runtime.</p>132 <div class="callout warning">133 <strong>Layers point down.</strong>134 <p><code>data</code>, <code>database</code>, <code>domain</code> and <code>services.<domain></code> never import <code>ui</code> or <code>app</code>. A worker or BGTask that needs the whole app gets it from its entry point (<code>BackgroundTaskHost</code>, <code>IosBackgroundTaskRegistration</code>), not by reaching up.</p>135 </div>136 </section>137138 <section class="doc-section" aria-labelledby="data-flow">139 <h2 id="data-flow">Data flow: UI to SQLite and back</h2>140 <p>The database boundary is a common Kotlin interface. Production uses Room KMP; tests can use the in-memory implementation without a device database.</p>141 <div class="flow-diagram" aria-label="Yokan data flow">142 <div class="flow-step"><small>Intent</small><strong>ReaderEvent / UI action</strong></div>143 <div class="flow-step"><small>State owner</small><strong>Route-scoped ViewModel</strong></div>144 <div class="flow-step"><small>Boundary</small><strong>YokanRepository</strong></div>145 <div class="flow-step"><small>Persistence</small><strong>YokanDatabase / Room</strong></div>146 </div>147 <h3 id="database-contract">Common contract</h3>148 <div class="code-block">149 <div class="code-label"><span>commonMain/database/YokanDatabase.kt</span></div>150 <pre><code>interface YokanDatabase {151 fun snapshots(): StateFlow<YokanDatabaseSnapshot>152 fun mangas(itemType: DbItemType? = null): List<DbManga>153 fun upsertManga(manga: DbManga): DbManga154 fun chapters(mangaId: Long? = null): List<DbChapter>155 fun settings(): DbSettings156}</code></pre>157 </div>158 <p><code>InMemoryYokanDatabase</code> implements the same interface with collections and a <code>MutableStateFlow</code>. <code>RoomYokanDatabase</code> adapts generated DAOs for persistent builds.</p>159160 <h3 id="room-declaration">Shared Room declaration</h3>161 <p>Entities, DAOs, and the abstract database live in <code>nonWebMain</code> (package <code>database.room</code>); the web target persists a snapshot in <code>localStorage</code> instead. Only the builder needs platform-specific paths and context.</p>162 <div class="code-block">163 <div class="code-label"><span>nonWebMain/database/room/YokanRoomDatabase.kt</span></div>164 <pre><code>@Database(165 entities = [MangaRow::class, ChapterRow::class, SettingsRow::class],166 version = 1,167 exportSchema = true,168)169@ConstructedBy(YokanRoomDatabaseConstructor::class)170abstract class YokanRoomDatabase : RoomDatabase()171172expect fun createYokanRoomDatabase(): YokanRoomDatabase</code></pre>173 </div>174 <table class="comparison">175 <thead><tr><th>Actual</th><th>What changes</th><th>What stays shared</th></tr></thead>176 <tbody>177 <tr><td>Android</td><td><code>Context.getDatabasePath()</code> and Android Room builder overload</td><td>Schema, DAOs, bundled SQLite driver</td></tr>178 <tr><td>Desktop</td><td>OS data directory and file path</td><td>Schema, DAOs, bundled SQLite driver</td></tr>179 <tr><td>iOS</td><td><code>NSDocumentDirectory</code> and Kotlin/Native path</td><td>Schema, DAOs, bundled SQLite driver</td></tr>180 </tbody>181 </table>182 </section>183184 <section class="doc-section" aria-labelledby="presentation-flow">185 <h2 id="presentation-flow">Presentation: routes, state, events, effects</h2>186 <p>Navigation is typed: a <code>Route</code> sealed interface names every destination and its arguments, and one <code>Navigator</code> owns the back stack as a <code>StateFlow</code>. Each stack entry has its own <code>ViewModelStore</code>, so a route's ViewModel lives exactly as long as the entry. A route adapter such as <code>ReaderEntryRoute(route, nav)</code> builds the ViewModel from runtime holders, collects its state, and hands values and lambdas to a stateless screen.</p>187 <div class="code-block">188 <div class="code-label"><span>commonMain/ui/screens/reader/ReaderContract.kt</span></div>189 <pre><code>data class ReaderState(190 val manga: Manga,191 val chapter: Chapter,192 val load: ReaderLoadState = ReaderLoadState.Idle,193 val currentPageIndex: Int = 0,194 // …195)196197sealed interface ReaderEvent {198 data object NextPage : ReaderEvent199 data object ToggleChrome : ReaderEvent200 data class JumpToPage(val index: Int) : ReaderEvent201}202203sealed interface ReaderEffect {204 data object Exit : ReaderEffect205 data class ShowMessage(val text: String) : ReaderEffect206 data class OpenChapter(val chapter: Chapter) : ReaderEffect207}</code></pre>208 </div>209 <p><code>ReaderViewModel</code> exposes immutable <code>StateFlow</code>, receives events through one exhaustive <code>when</code>, and emits one-shot effects through a channel; the route turns <code>OpenChapter</code> into <code>nav.navigate(Route.Reader(…))</code>. The same state owner runs on every target.</p>210 <div class="callout">211 <strong>Platform context remains at the edge.</strong>212 <p>The reader ViewModel owns page loading and progress. Per-image Coil prefetch stays in the composable because it needs a platform <code>ImageLoader</code>. Sharing everything is not the goal; a clear owner is.</p>213 </div>214 </section>215216 <section class="doc-section" aria-labelledby="place-new-code">217 <h2 id="place-new-code">Where should new code go?</h2>218 <table class="comparison">219 <thead><tr><th>Question</th><th>Place it here</th><th>Yokan example</th></tr></thead>220 <tbody>221 <tr><td>Same rule on every target?</td><td><code>commonMain/domain</code> or shared service</td><td>Chapter recognition, domain models</td></tr>222 <tr><td>Same UI on every target?</td><td><code>commonMain/ui</code></td><td>Library and reader screens</td></tr>223 <tr><td>Needs an OS capability?</td><td>Common contract + platform implementation</td><td>File picker, share sheet, notifications</td></tr>224 <tr><td>Shared by JVM only?</td><td>Intermediate <code>jvmCommonMain</code></td><td>Rhino JavaScript and LuaJ</td></tr>225 <tr><td>Builds the native host?</td><td>Target source set or <code>iosApp</code></td><td>MainActivity, desktop main, SwiftUI app</td></tr>226 </tbody>227 </table>228 <ul class="checklist">229 <li>Start from behavior, not from the API you already know.</li>230 <li>Keep common contracts small enough that every target can implement them honestly.</li>231 <li>Prefer a portable library before creating an expect/actual seam.</li>232 <li>Test shared rules through common interfaces whenever possible.</li>233 </ul>234 </section>235236 <nav class="next-page" aria-label="Chapter navigation">237 <a href="./getting-started.html"><small>Previous</small><strong>Build and map the project</strong></a>238 <a href="./patterns.html"><small>Next</small><strong>Reuse the KMP patterns</strong></a>239 </nav>240 </main>241242 <aside class="on-this-page" data-toc aria-label="On this page"></aside>243 </div>244245 <footer data-site-footer></footer>246</body>247</html>248