From 5bce1934c14c3392977154cbd30a8c89096ffebd Mon Sep 17 00:00:00 2001 From: Incido Developer Date: Tue, 6 Oct 2026 16:27:10 +1300 Subject: [PATCH] =?UTF-8?q?feat(phase-1/m4.3a):=20:core:settings=20?= =?UTF-8?q?=E2=86=92=20multiplatform=20(androidTarget=20+=20jvm)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verbatim M4.2 (:core:domain) playbook — both source files moved UNMODIFIED into commonMain (git records the rename; UserSettings.kt is a pure rename). This is the visibility gate for M4.3's AppGraph: a KMP commonMain cannot see plain kotlin("jvm") modules, and M4.3's container seam needs SettingsRepository/UserSettings from composeApp commonMain. One new seam, justified per §9 (never widen silently): kotlinx.coroutines `runBlocking` has no commonMain declaration, so SettingsRepository.snapshot() now goes through an internal expect/actual `blockingFirst(Flow): T`. Both actuals are the identical two-liner because both targets are JVM. Same startup-safety contract (block off the UI thread at startup only). Consumers unchanged by design: :desktopApp (plain jvm) and :composeApp's androidMain resolve through variant metadata — zero import changes. Verification (both green): ./gradlew assembleDebug :composeApp:compileKotlinJvm :core:data-jvm:smoke → BUILD SUCCESSFUL — RESULT: M1a PASS (one run, all consumers) ./gradlew :desktopApp:run --args="--data-dir /tmp/incido-desktop-smoke --self-test --exit-after 6" → SELFTEST wrote note id=1 → RENDER notes=1 selected=1 git diff core/data/objectbox-models → empty (ID model file unchanged) --- core/settings/build.gradle.kts | 57 +++++++++++++------ .../data/settings/BlockingFirst.android.kt | 7 +++ .../compose/data/settings/BlockingFirst.kt | 14 +++++ .../data/settings/SettingsRepository.kt | 12 ++-- .../compose/data/settings/UserSettings.kt | 0 .../data/settings/BlockingFirst.jvm.kt | 7 +++ 6 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 core/settings/src/androidMain/kotlin/org/incido/compose/data/settings/BlockingFirst.android.kt create mode 100644 core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/BlockingFirst.kt rename core/settings/src/{main => commonMain}/kotlin/org/incido/compose/data/settings/SettingsRepository.kt (91%) rename core/settings/src/{main => commonMain}/kotlin/org/incido/compose/data/settings/UserSettings.kt (100%) create mode 100644 core/settings/src/jvmMain/kotlin/org/incido/compose/data/settings/BlockingFirst.jvm.kt diff --git a/core/settings/build.gradle.kts b/core/settings/build.gradle.kts index f5e2d59..69e1c22 100644 --- a/core/settings/build.gradle.kts +++ b/core/settings/build.gradle.kts @@ -2,32 +2,57 @@ // :composeApp so the desktop build reads and writes the SAME preferences // file format the phone uses (`.preferences_pb`, identical keys). // -// Shape follows :core:backup: plain kotlin("jvm"), package names kept at -// org.incido.compose.data.settings so the Android diff is file moves only. -// The one API change: SettingsRepository takes an injected +// Phase 1 (M4.3a): converted kotlin("jvm") → kotlin("multiplatform") with +// androidTarget() + jvm(), the exact M4.2 (:core:domain) playbook. This is +// what makes SettingsRepository/UserSettings visible to a KMP commonMain — +// the gate M4.3's AppGraph needs (plain kotlin("jvm") modules are invisible +// to commonMain). Sources moved UNMODIFIED into commonMain except one two- +// line seam: runBlocking has no commonMain declaration, so snapshot() goes +// through expect/actual `blockingFirst` (both actuals identical — both our +// targets are JVM). Consumers unchanged by design: :desktopApp (plain jvm) +// and :composeApp (AGP + KMP) resolve through variant metadata. +// +// The one M3 API shape stands: SettingsRepository takes an injected // DataStore instead of a Context — Android supplies the // `preferencesDataStore(name = …)` Context-extension store, desktop builds // one with PreferenceDataStoreFactory.create(produceFile = …) (the spike // finding: the multiplatform core artifact has no Context extension). plugins { - alias(libs.plugins.kotlinJvm) -} - -java { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + alias(libs.plugins.androidLibrary) + alias(libs.plugins.kotlinMultiplatform) } kotlin { jvmToolchain(17) + + androidTarget() + jvm() + + sourceSets { + commonMain.dependencies { + // Public API (constructor + flow types) exposes DataStore — + // api, not impl. On the Android classpath this resolves to the same + // android variant :composeApp's datastore-preferences already pulls, so + // there is no duplicate-class situation. The 1.1.1 artifact is itself + // multiplatform (its jvm variant is what the plain-JVM desktop build + // has been resolving all along). + api(libs.androidx.datastore.preferences.core) + implementation(libs.kotlinx.coroutines.core) + } + } } -dependencies { - // Public API (constructor + flow types) exposes DataStore — - // api, not impl. On the Android classpath this resolves to the same - // android variant :composeApp's datastore-preferences already pulls, so - // there is no duplicate-class situation. - api(libs.androidx.datastore.preferences.core) - implementation(libs.kotlinx.coroutines.core) +android { + namespace = "org.incido.compose.data.settings" + compileSdk = 35 + + defaultConfig { + minSdk = 26 + } + + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } } diff --git a/core/settings/src/androidMain/kotlin/org/incido/compose/data/settings/BlockingFirst.android.kt b/core/settings/src/androidMain/kotlin/org/incido/compose/data/settings/BlockingFirst.android.kt new file mode 100644 index 0000000..fc58874 --- /dev/null +++ b/core/settings/src/androidMain/kotlin/org/incido/compose/data/settings/BlockingFirst.android.kt @@ -0,0 +1,7 @@ +package org.incido.compose.data.settings + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking + +internal actual fun blockingFirst(flow: Flow): T = runBlocking { flow.first() } \ No newline at end of file diff --git a/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/BlockingFirst.kt b/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/BlockingFirst.kt new file mode 100644 index 0000000..4fc95fd --- /dev/null +++ b/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/BlockingFirst.kt @@ -0,0 +1,14 @@ +package org.incido.compose.data.settings + +import kotlinx.coroutines.flow.Flow + +/** + * Take the first emission of [flow], blocking the calling thread. + * + * Phase 1 (M4.3a): the only platform seam :core:settings needed to become + * multiplatform — `kotlinx.coroutines.runBlocking` has no commonMain + * declaration. Both actuals are the identical two-liner because both our + * targets (androidTarget, jvm) are JVM. Same startup-safety contract as + * before: call off the UI thread (see SettingsRepository.snapshot KDoc). + */ +internal expect fun blockingFirst(flow: Flow): T \ No newline at end of file diff --git a/core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt b/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/SettingsRepository.kt similarity index 91% rename from core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt rename to core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/SettingsRepository.kt index 3b4fde6..d26d2dd 100644 --- a/core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt +++ b/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/SettingsRepository.kt @@ -7,9 +7,7 @@ import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.intPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking /** * DataStore-backed persistence for [UserSettings]. @@ -27,8 +25,10 @@ import kotlinx.coroutines.runBlocking * * Read-once access (used by [org.incido.compose.IncidoApplication] to * bootstrap the encryption key before the BoxStore is constructed) goes - * through [snapshot] which uses a `runBlocking { first() }` — safe at - * Application.onCreate time, where there's no UI thread to deadlock. + * through [snapshot] which uses a `blockingFirst` — the M4.3a expect/actual + * around `runBlocking { first() }` (`runBlocking` has no commonMain + * declaration; both our targets are JVM so both actuals are identical). + * Safe at Application.onCreate time, where there's no UI thread to deadlock. */ class SettingsRepository(private val dataStore: DataStore) { @@ -38,9 +38,7 @@ class SettingsRepository(private val dataStore: DataStore) { /** Read-once snapshot of the current settings. Blocking — call only * at startup, not from the UI thread. */ - fun snapshot(): UserSettings = runBlocking { - dataStore.data.first().toUserSettings() - } + fun snapshot(): UserSettings = blockingFirst(dataStore.data).toUserSettings() // ---- setters ---- // Each setter does a single .edit { } so we don't trigger spurious diff --git a/core/settings/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt b/core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/UserSettings.kt similarity index 100% rename from core/settings/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt rename to core/settings/src/commonMain/kotlin/org/incido/compose/data/settings/UserSettings.kt diff --git a/core/settings/src/jvmMain/kotlin/org/incido/compose/data/settings/BlockingFirst.jvm.kt b/core/settings/src/jvmMain/kotlin/org/incido/compose/data/settings/BlockingFirst.jvm.kt new file mode 100644 index 0000000..fc58874 --- /dev/null +++ b/core/settings/src/jvmMain/kotlin/org/incido/compose/data/settings/BlockingFirst.jvm.kt @@ -0,0 +1,7 @@ +package org.incido.compose.data.settings + +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking + +internal actual fun blockingFirst(flow: Flow): T = runBlocking { flow.first() } \ No newline at end of file