diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 9e4aa91..9244584 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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 diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 785f0c3..0167008 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -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) diff --git a/composeApp/src/main/kotlin/org/incido/compose/data/settings/AndroidSettingsStore.kt b/composeApp/src/main/kotlin/org/incido/compose/data/settings/AndroidSettingsStore.kt new file mode 100644 index 0000000..c0689d1 --- /dev/null +++ b/composeApp/src/main/kotlin/org/incido/compose/data/settings/AndroidSettingsStore.kt @@ -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 by preferencesDataStore( + name = "incido_settings", +) diff --git a/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt b/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt index 09d8010..89f463d 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt +++ b/composeApp/src/main/kotlin/org/incido/compose/di/AppContainer.kt @@ -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) diff --git a/core/settings/build.gradle.kts b/core/settings/build.gradle.kts new file mode 100644 index 0000000..f5e2d59 --- /dev/null +++ b/core/settings/build.gradle.kts @@ -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 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 — + // 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) +} diff --git a/composeApp/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt b/core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt similarity index 72% rename from composeApp/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt rename to core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt index 930b47f..3b4fde6 100644 --- a/composeApp/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt +++ b/core/settings/src/main/kotlin/org/incido/compose/data/settings/SettingsRepository.kt @@ -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) { /** Stream of the current [UserSettings] — emit-on-change semantics. */ val userSettingsFlow: Flow = - 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 by preferencesDataStore( - name = "incido_settings", -) diff --git a/composeApp/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt b/core/settings/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt similarity index 100% rename from composeApp/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt rename to core/settings/src/main/kotlin/org/incido/compose/data/settings/UserSettings.kt diff --git a/desktopApp/build.gradle.kts b/desktopApp/build.gradle.kts index 4fc0313..d9f17dd 100644 --- a/desktopApp/build.gradle.kts +++ b/desktopApp/build.gradle.kts @@ -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 diff --git a/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt index fcdde79..d5b35cc 100644 --- a/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt +++ b/desktopApp/src/main/kotlin/org/incido/desktop/DesktopContainer.kt @@ -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 = + 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 diff --git a/settings.gradle.kts b/settings.gradle.kts index 7ca8a72..e6aff7f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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; 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