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

+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)
}
@@ -0,0 +1,108 @@
package org.incido.compose.data.settings
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 kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
/**
* DataStore-backed persistence for [UserSettings].
*
* v0.3.2.0: the single source of truth for every toggleable preference
* in Incido. The UI layer reads [userSettingsFlow]; writers use the
* dedicated setter methods (which atomically edit the DataStore).
*
* 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 dataStore: DataStore<Preferences>) {
/** Stream of the current [UserSettings] — emit-on-change semantics. */
val userSettingsFlow: Flow<UserSettings> =
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 {
dataStore.data.first().toUserSettings()
}
// ---- setters ----
// Each setter does a single .edit { } so we don't trigger spurious
// emits for unrelated keys.
suspend fun setThemeMode(mode: ThemeMode) {
dataStore.edit { it[KEY_THEME_MODE] = mode.name }
}
suspend fun setDynamicColor(enabled: Boolean) {
dataStore.edit { it[KEY_DYNAMIC_COLOR] = enabled }
}
suspend fun setSecurityEnabled(enabled: Boolean) {
dataStore.edit { it[KEY_SECURITY_ENABLED] = enabled }
}
suspend fun setEncryptionEnabled(enabled: Boolean) {
dataStore.edit { it[KEY_ENCRYPTION_ENABLED] = enabled }
}
suspend fun setPinEnabled(enabled: Boolean) {
dataStore.edit { it[KEY_PIN_ENABLED] = enabled }
}
suspend fun setUseBiometric(enabled: Boolean) {
dataStore.edit { it[KEY_USE_BIOMETRIC] = enabled }
}
suspend fun setAutoLockTimeout(timeout: AutoLockTimeout) {
dataStore.edit { it[KEY_AUTO_LOCK_TIMEOUT] = timeout.minutes }
}
/** v0.3.5.0 — screenshot / screen-recording / app-preview blocking. */
suspend fun setBlockScreenshots(enabled: Boolean) {
dataStore.edit { it[KEY_BLOCK_SCREENSHOTS] = enabled }
}
// ---- private helpers ----
private fun Preferences.toUserSettings(): UserSettings = UserSettings(
themeMode = this[KEY_THEME_MODE]?.let { name ->
runCatching { ThemeMode.valueOf(name) }.getOrDefault(ThemeMode.SYSTEM)
} ?: ThemeMode.SYSTEM,
dynamicColor = this[KEY_DYNAMIC_COLOR] ?: true,
securityEnabled = this[KEY_SECURITY_ENABLED] ?: false,
encryptionEnabled = this[KEY_ENCRYPTION_ENABLED] ?: false,
pinEnabled = this[KEY_PIN_ENABLED] ?: false,
useBiometric = this[KEY_USE_BIOMETRIC] ?: false,
autoLockTimeout = AutoLockTimeout.fromMinutes(this[KEY_AUTO_LOCK_TIMEOUT] ?: 5),
// v0.3.5.0: default ON (true) — absent key means a fresh install blocks screenshots.
blockScreenshots = this[KEY_BLOCK_SCREENSHOTS] ?: true,
)
companion object {
private val KEY_THEME_MODE = stringPreferencesKey("theme_mode")
private val KEY_DYNAMIC_COLOR = booleanPreferencesKey("dynamic_color")
private val KEY_SECURITY_ENABLED = booleanPreferencesKey("security_enabled")
private val KEY_ENCRYPTION_ENABLED = booleanPreferencesKey("encryption_enabled")
private val KEY_PIN_ENABLED = booleanPreferencesKey("pin_enabled")
private val KEY_USE_BIOMETRIC = booleanPreferencesKey("use_biometric")
private val KEY_AUTO_LOCK_TIMEOUT = intPreferencesKey("auto_lock_timeout_minutes")
private val KEY_BLOCK_SCREENSHOTS = booleanPreferencesKey("block_screenshots")
}
}
@@ -0,0 +1,98 @@
package org.incido.compose.data.settings
/**
* Immutable snapshot of every user-toggleable setting in Incido.
*
* v0.3.2.0 introduces this type. UI layer reads it as a `Flow<UserSettings>`
* from [SettingsRepository]; writers update it via the repository's
* setter methods (which mutate DataStore + propagate to all observers).
*
* Defaults are chosen so that a fresh install behaves exactly like
* v0.3.1.x (no security, system theme, dynamic color on) — there are
* no breaking UX changes for users who never open Settings.
*
* v0.3.3.0: lock mode is now a mutually-exclusive choice between
* app-only PIN and device biometric — the two toggles on the settings
* screen grey each other out when one is enabled. `isLockActive`
* reflects whichever is currently active (PIN *or* biometric).
*/
data class UserSettings(
/** Theme mode — see [ThemeMode]. */
val themeMode: ThemeMode = ThemeMode.SYSTEM,
/** Material You dynamic color (Android 12+ only — no-op below). */
val dynamicColor: Boolean = true,
/** Master security switch. When false, none of the security sub-toggles apply. */
val securityEnabled: Boolean = false,
/** ObjectBox store encrypted at rest with a Keystore-wrapped key. */
val encryptionEnabled: Boolean = false,
/** Require an app-only PIN to open the app (when security is enabled).
* v0.3.3.0: mutually exclusive with [useBiometric] — both cannot be true. */
val pinEnabled: Boolean = false,
/** Use the device's biometric (fingerprint / face) to open the app
* (when security is enabled). v0.3.3.0: mutually exclusive with
* [pinEnabled] — both cannot be true. */
val useBiometric: Boolean = false,
/** Auto-lock the app after this many minutes of background time.
* Ignored on cold launch (always locked regardless). */
val autoLockTimeout: AutoLockTimeout = AutoLockTimeout.FIVE_MINUTES,
/**
* v0.3.5.0 — block screenshots / screen recording / app-switcher
* previews while inside Incido (`FLAG_SECURE`). Defaults to ON:
* a privacy app should not leak note content into the system
* screenshot surface unless the user opts out.
*/
val blockScreenshots: Boolean = true,
) {
/** True when a PIN is configured AND the master security switch is on. */
val isPinLockActive: Boolean
get() = securityEnabled && pinEnabled
/** True when biometric is the active lock (master on, biometric on, PIN off). */
val isBiometricLockActive: Boolean
get() = securityEnabled && useBiometric && !pinEnabled
/** True when *any* lock is active (PIN or biometric). Drives the
* lock-screen gate in [org.incido.compose.App]. */
val isLockActive: Boolean
get() = securityEnabled && (pinEnabled || useBiometric)
val isEncryptionActive: Boolean
get() = securityEnabled && encryptionEnabled
}
/** Theme selection for the settings screen. */
enum class ThemeMode {
/** Follow the OS-level light/dark setting (v0.3.1.x default). */
SYSTEM,
LIGHT,
DARK,
;
/**
* Resolve to a concrete boolean for [androidx.compose.foundation.isSystemInDarkTheme].
* Caller passes the current system value so SYSTEM can defer to it.
*/
fun resolveDark(systemInDarkTheme: Boolean): Boolean = when (this) {
SYSTEM -> systemInDarkTheme
LIGHT -> false
DARK -> true
}
}
/** Auto-lock timeout options exposed in the settings UI. */
enum class AutoLockTimeout(val minutes: Int, val label: String) {
IMMEDIATELY(0, "Immediately"),
ONE_MINUTE(1, "1 minute"),
FIVE_MINUTES(5, "5 minutes"),
FIFTEEN_MINUTES(15, "15 minutes"),
ONE_HOUR(60, "1 hour"),
;
/** Milliseconds of background time before re-lock is required. */
val millis: Long get() = minutes * 60_000L
companion object {
fun fromMinutes(min: Int): AutoLockTimeout =
values().firstOrNull { it.minutes == min } ?: FIVE_MINUTES
}
}