feat(phase-1/m3.1): :core:settings — SettingsRepository shared, store injected

Milestone M3 task 1 (DESKTOP_PHASE_PLAN §5): move UserSettings +
SettingsRepository into a plain kotlin("jvm") module so the desktop
build reads/writes the SAME preferences keys and .preferences_pb format
the phone uses. Package name org.incido.compose.data.settings kept, so
the Android diff is file moves + one constructor change.

Seam: SettingsRepository now takes an injected DataStore<Preferences>.
Android supplies the preferencesDataStore(name = "incido_settings")
Context-extension store (new AndroidSettingsStore.kt, moved verbatim);
desktop builds it with PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler, produceFile) — the
Phase-0 API delta (no Context extension on the multiplatform core
artifact). snapshot() keeps runBlocking for startup-only use.

DesktopContainer grows settingsRepository first, mirroring AppContainer
construction order ahead of M3's cipher install. dynamic_color /
use_biometric / block_screenshots keys ride along but have no desktop
UI — hidden, not faked (M3 exit rule).

Verified: ./gradlew assembleDebug (green; :app needed :core:settings on
its classpath — MainActivity reads userSettingsFlow, same pattern as
:core:domain) + ./gradlew :desktopApp:run --args="--data-dir
/tmp/incido-desktop-smoke --self-test --exit-after 6" (RENDER/SELFTEST
sequence intact) + ./gradlew :core:data-jvm:smoke (green).
This commit is contained in:
Incido Developer committed 2026-10-06 06:20:13 +13:00
1 parent 561d16eb0c
commit 8db835640e
10 files changed
+131 -30

No files matched your search

+4
View File
@@ -198,6 +198,10 @@ dependencies {
// viewer + the list card thumbnails). It's the *interface* in
// :core:domain, not the impl, so this stays clean of ObjectBox.
implementation(project(":core:domain"))
// Phase 1 (M3 task 1): same reason for SettingsRepository — MainActivity
// reads userSettingsFlow / snapshot() and its type moved with the class
// into the shared :core:settings module.
implementation(project(":core:settings"))
implementation(libs.androidx.core.ktx)
implementation(libs.kotlinx.coroutines.android)
// v0.3.2.0: MainActivity now extends FragmentActivity so the
+3
View File
@@ -47,6 +47,9 @@ dependencies {
// org.incido.compose.data.backup package name was kept, so no imports
// changed — only this dependency line.
implementation(project(":core:backup"))
// Phase 1 (M3 task 1): settings repository, same treatment — package
// kept, the Context-based construction moved to AndroidSettingsStore.kt.
implementation(project(":core:settings"))
// Core / lifecycle
implementation(libs.androidx.core.ktx)
@@ -0,0 +1,22 @@
package org.incido.compose.data.settings
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStore
/**
* Phase 1 (M3 task 1): the Android half of the settings seam.
*
* v0.3.2.0: Application-wide DataStore Preferences instance. Declared
* as a Context extension property so the same backing file is shared
* by every call site. Backed by `incido_settings.preferences_pb` in
* the app's `noBackupFiles` directory.
*
* Lives on the Android side now that [SettingsRepository] is platform
* neutral (it takes an injected DataStore); the desktop build creates the
* same-format store from a file path instead of a Context.
*/
val Context.settingsDataStore: DataStore<Preferences> by preferencesDataStore(
name = "incido_settings",
)
@@ -18,6 +18,7 @@ import org.incido.compose.data.security.EncryptionVerifier
import org.incido.compose.data.security.ObjectBoxKeyProvider
import org.incido.compose.data.security.SecurityManager
import org.incido.compose.data.settings.SettingsRepository
import org.incido.compose.data.settings.settingsDataStore
/**
* Hand-rolled DI container. Replaces Hilt/Dagger for v0.1 — keeps the
@@ -49,7 +50,11 @@ class AppContainer(context: Context) {
* (verification sweeps, etc.). SupervisorJob isolates failures. */
val appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
val settingsRepository: SettingsRepository = SettingsRepository(appContext)
// Phase 1 (M3 task 1): the store is injected — SettingsRepository moved
// to :core:settings so the desktop build can share it. Android supplies
// the Context-backed `incido_settings.preferences_pb` store here.
val settingsRepository: SettingsRepository =
SettingsRepository(appContext.settingsDataStore)
val objectBoxKeyProvider: ObjectBoxKeyProvider = ObjectBoxKeyProvider(appContext)
+33
View File
@@ -0,0 +1,33 @@
// core/settings — Phase 1 (M3 task 1): user settings, extracted from
// :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
// DataStore<Preferences> 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
}
kotlin {
jvmToolchain(17)
}
dependencies {
// Public API (constructor + flow types) exposes DataStore<Preferences> —
// 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)
}
@@ -1,13 +1,11 @@
package org.incido.compose.data.settings
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
@@ -20,24 +18,28 @@ import kotlinx.coroutines.runBlocking
* in Incido. The UI layer reads [userSettingsFlow]; writers use the
* dedicated setter methods (which atomically edit the DataStore).
*
* Singleton wrapper around a process-wide [DataStore] declared as a
* Context extension property — created lazily on first access.
* Phase 1 (M3 task 1): the store is INJECTED, which is what makes this
* class platform-neutral. Android wires the `preferencesDataStore(
* name = "incido_settings")` Context-extension store (see
* `AndroidSettingsStore.kt` in :composeApp); the desktop build creates the
* same-format store from a file via PreferenceDataStoreFactory. Same keys,
* same `.preferences_pb` file on both platforms.
*
* 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.
*/
class SettingsRepository(private val context: Context) {
class SettingsRepository(private val dataStore: DataStore<Preferences>) {
/** Stream of the current [UserSettings] — emit-on-change semantics. */
val userSettingsFlow: Flow<UserSettings> =
context.dataStore.data.map { prefs -> prefs.toUserSettings() }
dataStore.data.map { prefs -> prefs.toUserSettings() }
/** Read-once snapshot of the current settings. Blocking — call only
* at startup, not from the UI thread. */
fun snapshot(): UserSettings = runBlocking {
context.dataStore.data.first().toUserSettings()
dataStore.data.first().toUserSettings()
}
// ---- setters ----
@@ -45,36 +47,36 @@ class SettingsRepository(private val context: Context) {
// emits for unrelated keys.
suspend fun setThemeMode(mode: ThemeMode) {
context.dataStore.edit { it[KEY_THEME_MODE] = mode.name }
dataStore.edit { it[KEY_THEME_MODE] = mode.name }
}
suspend fun setDynamicColor(enabled: Boolean) {
context.dataStore.edit { it[KEY_DYNAMIC_COLOR] = enabled }
dataStore.edit { it[KEY_DYNAMIC_COLOR] = enabled }
}
suspend fun setSecurityEnabled(enabled: Boolean) {
context.dataStore.edit { it[KEY_SECURITY_ENABLED] = enabled }
dataStore.edit { it[KEY_SECURITY_ENABLED] = enabled }
}
suspend fun setEncryptionEnabled(enabled: Boolean) {
context.dataStore.edit { it[KEY_ENCRYPTION_ENABLED] = enabled }
dataStore.edit { it[KEY_ENCRYPTION_ENABLED] = enabled }
}
suspend fun setPinEnabled(enabled: Boolean) {
context.dataStore.edit { it[KEY_PIN_ENABLED] = enabled }
dataStore.edit { it[KEY_PIN_ENABLED] = enabled }
}
suspend fun setUseBiometric(enabled: Boolean) {
context.dataStore.edit { it[KEY_USE_BIOMETRIC] = enabled }
dataStore.edit { it[KEY_USE_BIOMETRIC] = enabled }
}
suspend fun setAutoLockTimeout(timeout: AutoLockTimeout) {
context.dataStore.edit { it[KEY_AUTO_LOCK_TIMEOUT] = timeout.minutes }
dataStore.edit { it[KEY_AUTO_LOCK_TIMEOUT] = timeout.minutes }
}
/** v0.3.5.0 — screenshot / screen-recording / app-preview blocking. */
suspend fun setBlockScreenshots(enabled: Boolean) {
context.dataStore.edit { it[KEY_BLOCK_SCREENSHOTS] = enabled }
dataStore.edit { it[KEY_BLOCK_SCREENSHOTS] = enabled }
}
// ---- private helpers ----
@@ -104,13 +106,3 @@ class SettingsRepository(private val context: Context) {
private val KEY_BLOCK_SCREENSHOTS = booleanPreferencesKey("block_screenshots")
}
}
/**
* v0.3.2.0: Application-wide DataStore Preferences instance. Declared
* as a Context extension property so the same backing file is shared
* by every call site. Backed by `incido_settings.preferences_pb` in
* the app's `noBackupFiles` directory.
*/
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
name = "incido_settings",
)
+5
View File
@@ -34,6 +34,11 @@ dependencies {
// Phase 1 (M2 task 2): the backup bridge — the desktop "Import from
// file… / Export" menu speaks the phone's format through it.
implementation(project(":core:backup"))
// Phase 1 (M3 task 1): shared settings repository + the multiplatform
// DataStore core artifact, which is where a JVM builds a
// `.preferences_pb` store from a file path (no Context extension).
implementation(project(":core:settings"))
implementation(libs.androidx.datastore.preferences.core)
}
// Packaging works (proven in Phase 0), but per-OS installers and icons are
@@ -1,6 +1,12 @@
package org.incido.desktop
import androidx.datastore.core.DataStore
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.emptyPreferences
import java.io.File
import org.incido.compose.data.settings.SettingsRepository
import org.incido.data.ImageOrientationNormalizer
import org.incido.data.ObjectBoxStore
import org.incido.data.crypto.CipherHolder
@@ -11,10 +17,10 @@ import org.incido.domain.repository.NoteRepository
* Hand-rolled DI container for the desktop build — the JVM counterpart of
* the Android app's `AppContainer` (Development Plan §6, Phase 1, M1b).
*
* Deliberately mirrors Android's construction order: open the store, then
* build the repository over a shared [CipherHolder] so installing or removing
* at-rest encryption later is a one-line change here instead of a rebuild of
* the graph.
* Deliberately mirrors Android's construction order: settings first (M3),
* then open the store, then build the repository over a shared
* [CipherHolder] so installing or removing at-rest encryption later is a
* one-line change here instead of a rebuild of the graph.
*
* Two Android behaviours are intentionally absent, both planned work:
* - at-rest encryption (milestone M3 — needs a JVM KeyProvider; the
@@ -24,6 +30,18 @@ import org.incido.domain.repository.NoteRepository
*/
class DesktopContainer(dataDir: File) {
/**
* Phase 1 (M3 task 1): the same settings the phone stores, in the same
* `.preferences_pb` format, living inside this library's data dir.
* SPIKE FINDING applied: the multiplatform DataStore has no
* `preferencesDataStore(name = …)` extension — the store is created
* from a file with PreferenceDataStoreFactory + a corruption handler
* that replaces an unreadable file with empty preferences (same
* semantics as Android's default).
*/
val settingsRepository: SettingsRepository =
SettingsRepository(preferencesStore(dataDir, "incido_settings"))
val cipherHolder = CipherHolder()
val boxStore = ObjectBoxStore.init(dataDir)
@@ -37,6 +55,19 @@ class DesktopContainer(dataDir: File) {
fun shutdown() = ObjectBoxStore.reset()
}
/**
* One DataStore Preferences file inside [dataDir]. Each call site owns a
* DISTINCT file (settings, window state, …) — the JVM factory is not a
* name-keyed singleton like Android's `preferencesDataStore` delegate, so
* uniqueness of the produced file is the caller's job (two DataStore
* instances on one file would fight over the lock).
*/
fun preferencesStore(dataDir: File, name: String): DataStore<Preferences> =
PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler { emptyPreferences() },
produceFile = { File(dataDir, "$name.preferences_pb") },
)
/**
* Where the desktop library lives: `$XDG_DATA_HOME/incido`, falling back to
* `~/.local/share/incido` (Development Plan §6 — a *separate* library per
+6
View File
@@ -43,6 +43,12 @@ include(":desktopApp")
// consumed by both platforms.
include(":core:backup")
// Phase 1 (M3 task 1): user settings, same extraction logic as :core:backup.
// SettingsRepository now takes an injected DataStore<Preferences>; Android
// supplies the Context-extension store, the desktop build a file-based one —
// both producing the same `.preferences_pb` format, same keys.
include(":core:settings")
// --- Desktop feasibility spike (Development Plan §6.3, throwaway) ---
// Throwaway probes that answer "can the existing stack run on the JVM"
// before any of Phases 1-6 are committed to. They are additive: no module