Trace the shared core.

Follow Yokan from each native entry point to one shared composition, then down through state, services, repositories, and Room.

25–35 minutesSource-set boundariesEnd-to-end trace

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.

Platform hostLaunch + OS context
Shared UIRoutes + ViewModels
ApplicationHolders + services
DataRepository + Room
The boundary is a design decision.

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.

TargetNative entryShared handoff
AndroidYokanApplication + MainActivity : ComponentActivitysetContent { YokanApp() }
Desktopfun main(args) + Compose WindowWindow { YokanApp() }
iOSSwiftUI @main applicationComposeUIViewController { YokanApp() }

Android host

androidMain/AndroidAppContext.kt + MainActivity.kt
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

desktopMain/eu/friedcutlet/yokan/Main.kt
fun main(args: Array<String>) {
    DesktopBootstrapState.args = args
    YokanRuntime.bootstrap()
    application {
        Window(onCloseRequest = ::exitApplication, title = "Yokan") {
            YokanApp()
        }
    }
}

iOS host

iosMain/MainViewController.kt + iosApp/YokanApp.swift
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.

commonMain/app/YokanRuntime.kt
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.

Layers point down.

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.

IntentReaderEvent / UI action
State ownerRoute-scoped ViewModel
BoundaryYokanRepository
PersistenceYokanDatabase / Room

Common contract

commonMain/database/YokanDatabase.kt
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.

nonWebMain/database/room/YokanRoomDatabase.kt
@Database(
    entities = [MangaRow::class, ChapterRow::class, SettingsRow::class],
    version = 1,
    exportSchema = true,
)
@ConstructedBy(YokanRoomDatabaseConstructor::class)
abstract class YokanRoomDatabase : RoomDatabase()

expect fun createYokanRoomDatabase(): YokanRoomDatabase
ActualWhat changesWhat stays shared
AndroidContext.getDatabasePath() and Android Room builder overloadSchema, DAOs, bundled SQLite driver
DesktopOS data directory and file pathSchema, DAOs, bundled SQLite driver
iOSNSDocumentDirectory and Kotlin/Native pathSchema, 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.

commonMain/ui/screens/reader/ReaderContract.kt
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.

Platform context remains at the edge.

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?

QuestionPlace it hereYokan example
Same rule on every target?commonMain/domain or shared serviceChapter recognition, domain models
Same UI on every target?commonMain/uiLibrary and reader screens
Needs an OS capability?Common contract + platform implementationFile picker, share sheet, notifications
Shared by JVM only?Intermediate jvmCommonMainRhino JavaScript and LuaJ
Builds the native host?Target source set or iosAppMainActivity, 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.