Trace the shared core.
Follow Yokan from each native entry point to one shared composition, then down through state, services, repositories, and Room.
The architecture in one sentence
Yokan shares product behavior and most UI in commonMain, while thin target hosts and small actual implementations provide capabilities that require operating-system APIs.
A file belongs in commonMain 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.
Three entry points, one composition
Each app starts in the idiom of its platform and renders the same YokanApp(), which bootstraps shared services off the main thread before showing the UI.
| Target | Native entry | Shared handoff |
|---|---|---|
| Android | YokanApplication + MainActivity : ComponentActivity | setContent { YokanApp() } |
| Desktop | fun main(args) + Compose Window | Window { YokanApp() } |
| iOS | SwiftUI @main application | ComposeUIViewController { YokanApp() } |
Android host
class YokanApplication : Application(), BackgroundTaskHost {
override fun onCreate() {
super.onCreate()
AndroidAppContext.appContext = applicationContext
}
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent { YokanApp() }
}
}
Desktop host
fun main(args: Array<String>) {
DesktopBootstrapState.args = args
YokanRuntime.bootstrap()
application {
Window(onCloseRequest = ::exitApplication, title = "Yokan") {
YokanApp()
}
}
}
iOS host
fun MainViewController() = ComposeUIViewController {
YokanApp()
}
struct ComposeRootView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController()
}
}
The Swift app owns the iOS lifecycle; Kotlin supplies a regular UIViewController. This keeps native integration possible without duplicating the feature UI.
Bootstrap and dependency ownership
YokanRuntime (package eu.friedcutlet.yokan.app, next to ServiceRegistry.kt and AppBootstrap.kt) is the composition root. Its lazy properties create the database, repository, service registry, app-scoped state holders (settings, libraryIndex, libraryUpdates, localLibrary, migration, chapterDownloads, sourceInstall), platform controllers, and background work only when needed, exactly once per process.
object YokanRuntime {
val database: YokanDatabase by lazy { createDefaultDatabase() }
val services: YokanServices by lazy {
defaultServices(database, localFiles, localArchive, notifications)
}
val repository: YokanRepository by lazy {
DefaultYokanRepository(database, services.downloader)
}
// Idempotent and thread-safe; OS workers pass background = true.
fun bootstrap(input: PlatformBootstrapInput = platformBootstrapInput(), background: Boolean = false) {
// install crash handling, run startup checks, start foreground services…
}
}
Only the composition root reads YokanRuntime: App.kt, 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.
data, database, domain and services.<domain> never import ui or app. A worker or BGTask that needs the whole app gets it from its entry point (BackgroundTaskHost, IosBackgroundTaskRegistration), not by reaching up.
Data flow: UI to SQLite and back
The database boundary is a common Kotlin interface. Production uses Room KMP; tests can use the in-memory implementation without a device database.
Common contract
interface YokanDatabase {
fun snapshots(): StateFlow<YokanDatabaseSnapshot>
fun mangas(itemType: DbItemType? = null): List<DbManga>
fun upsertManga(manga: DbManga): DbManga
fun chapters(mangaId: Long? = null): List<DbChapter>
fun settings(): DbSettings
}
InMemoryYokanDatabase implements the same interface with collections and a MutableStateFlow. RoomYokanDatabase adapts generated DAOs for persistent builds.
Shared Room declaration
Entities, DAOs, and the abstract database live in nonWebMain (package database.room); the web target persists a snapshot in localStorage instead. Only the builder needs platform-specific paths and context.
@Database(
entities = [MangaRow::class, ChapterRow::class, SettingsRow::class],
version = 1,
exportSchema = true,
)
@ConstructedBy(YokanRoomDatabaseConstructor::class)
abstract class YokanRoomDatabase : RoomDatabase()
expect fun createYokanRoomDatabase(): YokanRoomDatabase
| Actual | What changes | What stays shared |
|---|---|---|
| Android | Context.getDatabasePath() and Android Room builder overload | Schema, DAOs, bundled SQLite driver |
| Desktop | OS data directory and file path | Schema, DAOs, bundled SQLite driver |
| iOS | NSDocumentDirectory and Kotlin/Native path | Schema, DAOs, bundled SQLite driver |
Presentation: routes, state, events, effects
Navigation is typed: a Route sealed interface names every destination and its arguments, and one Navigator owns the back stack as a StateFlow. Each stack entry has its own ViewModelStore, so a route's ViewModel lives exactly as long as the entry. A route adapter such as ReaderEntryRoute(route, nav) builds the ViewModel from runtime holders, collects its state, and hands values and lambdas to a stateless screen.
data class ReaderState(
val manga: Manga,
val chapter: Chapter,
val load: ReaderLoadState = ReaderLoadState.Idle,
val currentPageIndex: Int = 0,
// …
)
sealed interface ReaderEvent {
data object NextPage : ReaderEvent
data object ToggleChrome : ReaderEvent
data class JumpToPage(val index: Int) : ReaderEvent
}
sealed interface ReaderEffect {
data object Exit : ReaderEffect
data class ShowMessage(val text: String) : ReaderEffect
data class OpenChapter(val chapter: Chapter) : ReaderEffect
}
ReaderViewModel exposes immutable StateFlow, receives events through one exhaustive when, and emits one-shot effects through a channel; the route turns OpenChapter into nav.navigate(Route.Reader(…)). The same state owner runs on every target.
The reader ViewModel owns page loading and progress. Per-image Coil prefetch stays in the composable because it needs a platform ImageLoader. Sharing everything is not the goal; a clear owner is.
Where should new code go?
| Question | Place it here | Yokan example |
|---|---|---|
| Same rule on every target? | commonMain/domain or shared service | Chapter recognition, domain models |
| Same UI on every target? | commonMain/ui | Library and reader screens |
| Needs an OS capability? | Common contract + platform implementation | File picker, share sheet, notifications |
| Shared by JVM only? | Intermediate jvmCommonMain | Rhino JavaScript and LuaJ |
| Builds the native host? | Target source set or iosApp | MainActivity, desktop main, SwiftUI app |
- Start from behavior, not from the API you already know.
- Keep common contracts small enough that every target can implement them honestly.
- Prefer a portable library before creating an expect/actual seam.
- Test shared rules through common interfaces whenever possible.