feat(phase-1/m4.3c): ViewModels off APPLICATION_KEY; DesktopContainer implements AppGraph
Completes the M4.3 container seam:
* NoteListViewModel + NoteEditorViewModel (UiStates and SharedImage
included) move to commonMain. NoteEditorViewModel had zero android.*
imports; its four withContext(Dispatchers.IO) sites become
withContext(ioDispatcher) — a new internal expect/actual whose ACTUALS
are literally Dispatchers.IO on both targets, so Android behaviour is
byte-identical (invariant §2.1), merely spelled through the seam.
* New commonMain EncryptionBadge/LibraryVerification: the read-only
mirror of :core:data's EncryptionVerification. Required because
:core:data is an androidLibrary invisible to commonMain and must stay
so (invariant §2.3 — objectbox-android vs objectbox-java classpaths).
AppGraph gains encryptionBadge; both containers map the real verifier
result 1:1 at their own seam.
* SettingsViewModel stays in androidMain (SecurityManager, Keystore key
provider, BackupManager Uri IO, CarnetImporter are genuine Android-only
services — the graph was NOT widened to fake them, §9) but its factory
now takes the concrete AppContainer via a new LocalAndroidContainer,
provided alongside LocalAppGraph in IncidoApp. No APPLICATION_KEY /
IncidoApplication reference survives in ANY ViewModel: all six call
sites (drawer, list, editor, settings, pin setup, pin change) build
factories from the graph/container locals at composition time.
* M4.3 exit proof: DesktopContainer : AppGraph — the same face
(noteRepository/settingsRepository/encryptionBadge) the commonMain
ViewModels consume, compiler-checked by :desktopApp building against
:composeApp's jvm target. repository → noteRepository rename.
The stand-in two-pane UI still runs (M4.5 consumes the real screens).
Verification (all green, one suite):
./gradlew assembleDebug :composeApp:compileKotlinJvm :desktopApp:build
→ BUILD SUCCESSFUL
./gradlew :desktopApp:run … --self-test --exit-after 6 → SELFTEST id=1, clean exit
./gradlew :core:data-jvm:smoke → RESULT: M1a PASS
crypto legs (separate processes): leg-A ALL-OK → LEG-B PASS/ALL-OK
git diff '*/objectbox-models/*' → empty
This commit is contained in:
1 parent
cd363c7aa5
commit
59bd6e106b
21 files changed
+176
-37
No files matched your search
@@ -31,6 +31,7 @@ import androidx.navigation.navArgument
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.incido.compose.data.security.SecurityManager
|
import org.incido.compose.data.security.SecurityManager
|
||||||
import org.incido.compose.data.settings.SettingsRepository
|
import org.incido.compose.data.settings.SettingsRepository
|
||||||
|
import org.incido.compose.di.LocalAndroidContainer
|
||||||
import org.incido.compose.di.LocalAppGraph
|
import org.incido.compose.di.LocalAppGraph
|
||||||
import org.incido.compose.share.SharedIntentBuffer
|
import org.incido.compose.share.SharedIntentBuffer
|
||||||
import org.incido.compose.ui.drawer.IncidoDrawer
|
import org.incido.compose.ui.drawer.IncidoDrawer
|
||||||
@@ -112,7 +113,10 @@ fun IncidoApp(
|
|||||||
|
|
||||||
// M4.3: the platform graph reaches the screens through the composition —
|
// M4.3: the platform graph reaches the screens through the composition —
|
||||||
// the same face DesktopContainer implements (commonMain di/AppGraph.kt).
|
// the same face DesktopContainer implements (commonMain di/AppGraph.kt).
|
||||||
CompositionLocalProvider(LocalAppGraph provides app.container) {
|
CompositionLocalProvider(
|
||||||
|
LocalAppGraph provides app.container,
|
||||||
|
LocalAndroidContainer provides app.container,
|
||||||
|
) {
|
||||||
IncidoTheme(
|
IncidoTheme(
|
||||||
darkTheme = settings.themeMode.resolveDark(isSystemInDarkTheme()),
|
darkTheme = settings.themeMode.resolveDark(isSystemInDarkTheme()),
|
||||||
dynamicColor = settings.dynamicColor,
|
dynamicColor = settings.dynamicColor,
|
||||||
|
|||||||
@@ -8,8 +8,10 @@ import org.incido.compose.di.AppContainer
|
|||||||
* Application class. Constructs the singleton [AppContainer] holding the
|
* Application class. Constructs the singleton [AppContainer] holding the
|
||||||
* ObjectBox store and the note repository.
|
* ObjectBox store and the note repository.
|
||||||
*
|
*
|
||||||
* ViewModels reach the container via `(application as IncidoApplication).container`
|
* M4.3: the screens reach the container through `LocalAppGraph` (provided
|
||||||
* (see `NoteListViewModel.Factory` and `NoteEditorViewModel.factory`).
|
* by `IncidoApp`); the graph-based ViewModel factories (`factory(graph)`)
|
||||||
|
* no longer touch the Application through CreationExtras. The Application
|
||||||
|
* still owns construction order.
|
||||||
*
|
*
|
||||||
* v0.3.2.0 — initialises the [SecurityManager] lock state during
|
* v0.3.2.0 — initialises the [SecurityManager] lock state during
|
||||||
* `onCreate` so the first composition of the root Compose tree
|
* `onCreate` so the first composition of the root Compose tree
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package org.incido.compose.di
|
||||||
|
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.compositionLocalOf
|
||||||
|
|
||||||
|
/**
|
||||||
|
* M4.3c — Android-only bridge beside [LocalAppGraph].
|
||||||
|
*
|
||||||
|
* `SettingsViewModel` still needs the concrete [AppContainer] (Security
|
||||||
|
* Manager, Keystore key provider, BackupManager, CarnetImporter — the
|
||||||
|
* Android services M4.3 deliberately did NOT widen the graph with). The
|
||||||
|
* screen reads this local until M4.4/4.5 ports or hides that surface;
|
||||||
|
* `IncidoApp` provides the SAME container instance through both locals.
|
||||||
|
*/
|
||||||
|
val LocalAndroidContainer = compositionLocalOf<AppContainer?> { null }
|
||||||
|
|
||||||
|
/** Read [LocalAndroidContainer] or throw with context — call at composition. */
|
||||||
|
@Composable
|
||||||
|
fun requireAndroidContainer(): AppContainer = checkNotNull(LocalAndroidContainer.current) {
|
||||||
|
"No AppContainer provided — IncidoApp provides it at the root."
|
||||||
|
}
|
||||||
@@ -20,6 +20,9 @@ import org.incido.compose.data.security.SecurityManager
|
|||||||
import org.incido.compose.data.settings.SettingsRepository
|
import org.incido.compose.data.settings.SettingsRepository
|
||||||
import org.incido.compose.data.settings.settingsDataStore
|
import org.incido.compose.data.settings.settingsDataStore
|
||||||
import org.incido.compose.di.AppGraph
|
import org.incido.compose.di.AppGraph
|
||||||
|
import org.incido.compose.di.EncryptionBadge
|
||||||
|
import org.incido.compose.di.LibraryVerification
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hand-rolled DI container. Replaces Hilt/Dagger for v0.1 — keeps the
|
* Hand-rolled DI container. Replaces Hilt/Dagger for v0.1 — keeps the
|
||||||
@@ -110,6 +113,18 @@ class AppContainer(context: Context) : AppGraph {
|
|||||||
/** Runs on every app open — feeds the verified-encryption badge. */
|
/** Runs on every app open — feeds the verified-encryption badge. */
|
||||||
val encryptionVerifier = EncryptionVerifier(noteCryptoService, appScope)
|
val encryptionVerifier = EncryptionVerifier(noteCryptoService, appScope)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* M4.3c: [AppGraph]'s shared face over the concrete verifier — the
|
||||||
|
* 3-field copy is why `LibraryVerification` mirrors the data class
|
||||||
|
* (see EncryptionBadge.kt: :core:data is an androidLibrary, invisible
|
||||||
|
* to commonMain, and must stay that way per invariant §2.3).
|
||||||
|
*/
|
||||||
|
override val encryptionBadge: EncryptionBadge = object : EncryptionBadge {
|
||||||
|
override val verification = encryptionVerifier.verification.map { result ->
|
||||||
|
result?.let { LibraryVerification(it.verifiedNoteIds, it.failedNoteIds, it.checkedAt) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Kick the startup verification off immediately. */
|
/** Kick the startup verification off immediately. */
|
||||||
init {
|
init {
|
||||||
encryptionVerifier.checkNow()
|
encryptionVerifier.checkNow()
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package org.incido.compose.di
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
internal actual val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||||
@@ -117,6 +117,7 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.incido.compose.data.security.SecurityManager
|
import org.incido.compose.data.security.SecurityManager
|
||||||
|
import org.incido.compose.di.requireAppGraph
|
||||||
import org.incido.compose.ui.theme.AllNoteColors
|
import org.incido.compose.ui.theme.AllNoteColors
|
||||||
import org.incido.compose.ui.theme.swatchBorderColor
|
import org.incido.compose.ui.theme.swatchBorderColor
|
||||||
import org.incido.compose.ui.theme.toComposeColor
|
import org.incido.compose.ui.theme.toComposeColor
|
||||||
@@ -150,6 +151,7 @@ fun NoteEditorScreen(
|
|||||||
securityManager: SecurityManager? = null,
|
securityManager: SecurityManager? = null,
|
||||||
viewModel: NoteEditorViewModel = viewModel(
|
viewModel: NoteEditorViewModel = viewModel(
|
||||||
factory = NoteEditorViewModel.factory(
|
factory = NoteEditorViewModel.factory(
|
||||||
|
graph = requireAppGraph(),
|
||||||
noteId = noteId,
|
noteId = noteId,
|
||||||
initialSharedText = initialSharedText,
|
initialSharedText = initialSharedText,
|
||||||
initialSharedImages = initialSharedImages,
|
initialSharedImages = initialSharedImages,
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ import androidx.compose.ui.text.style.TextOverflow
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import org.incido.compose.di.requireAppGraph
|
||||||
import org.incido.compose.ui.theme.toComposeColor
|
import org.incido.compose.ui.theme.toComposeColor
|
||||||
import org.incido.compose.util.rememberRelativeTimestamp
|
import org.incido.compose.util.rememberRelativeTimestamp
|
||||||
import org.incido.domain.model.Note
|
import org.incido.domain.model.Note
|
||||||
@@ -127,7 +128,7 @@ fun NoteListScreen(
|
|||||||
onOpenEditor: (Long?) -> Unit,
|
onOpenEditor: (Long?) -> Unit,
|
||||||
/** v0.3.1.0 — used to fetch image bytes for card thumbnails. */
|
/** v0.3.1.0 — used to fetch image bytes for card thumbnails. */
|
||||||
repository: NoteRepository? = null,
|
repository: NoteRepository? = null,
|
||||||
viewModel: NoteListViewModel = viewModel(factory = NoteListViewModel.Factory),
|
viewModel: NoteListViewModel = viewModel(factory = NoteListViewModel.factory(requireAppGraph())),
|
||||||
) {
|
) {
|
||||||
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
val state by viewModel.uiState.collectAsStateWithLifecycle()
|
||||||
var localQuery by remember { mutableStateOf("") }
|
var localQuery by remember { mutableStateOf("") }
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ import kotlinx.coroutines.launch
|
|||||||
import org.incido.compose.data.backup.BackupPreview
|
import org.incido.compose.data.backup.BackupPreview
|
||||||
import org.incido.compose.data.backup.EncryptedBackupCodec
|
import org.incido.compose.data.backup.EncryptedBackupCodec
|
||||||
import org.incido.compose.data.security.SecurityManager
|
import org.incido.compose.data.security.SecurityManager
|
||||||
|
import org.incido.compose.di.requireAndroidContainer
|
||||||
import org.incido.compose.data.settings.AutoLockTimeout
|
import org.incido.compose.data.settings.AutoLockTimeout
|
||||||
import org.incido.compose.data.settings.ThemeMode
|
import org.incido.compose.data.settings.ThemeMode
|
||||||
|
|
||||||
@@ -93,7 +94,7 @@ fun SettingsScreen(
|
|||||||
* suppression (the pre-0.3.4.0 behaviour).
|
* suppression (the pre-0.3.4.0 behaviour).
|
||||||
*/
|
*/
|
||||||
securityManager: SecurityManager? = null,
|
securityManager: SecurityManager? = null,
|
||||||
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.Factory),
|
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.factory(requireAndroidContainer())),
|
||||||
) {
|
) {
|
||||||
val settings by viewModel.userSettings.collectAsStateWithLifecycle()
|
val settings by viewModel.userSettings.collectAsStateWithLifecycle()
|
||||||
val migrationState by viewModel.migrationState.collectAsStateWithLifecycle()
|
val migrationState by viewModel.migrationState.collectAsStateWithLifecycle()
|
||||||
|
|||||||
+11
-5
@@ -2,7 +2,6 @@ package org.incido.compose.ui.settings
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
|
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.lifecycle.viewmodel.initializer
|
import androidx.lifecycle.viewmodel.initializer
|
||||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||||
@@ -12,7 +11,7 @@ import kotlinx.coroutines.flow.StateFlow
|
|||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.incido.compose.IncidoApplication
|
import org.incido.compose.di.AppContainer
|
||||||
import org.incido.compose.data.backup.BackupManager
|
import org.incido.compose.data.backup.BackupManager
|
||||||
import org.incido.compose.data.backup.CarnetImporter
|
import org.incido.compose.data.backup.CarnetImporter
|
||||||
import org.incido.compose.data.security.ObjectBoxKeyProvider
|
import org.incido.compose.data.security.ObjectBoxKeyProvider
|
||||||
@@ -362,10 +361,17 @@ class SettingsViewModel(
|
|||||||
* — if you bump one, bump both. */
|
* — if you bump one, bump both. */
|
||||||
const val APP_VERSION = "0.4.2.0"
|
const val APP_VERSION = "0.4.2.0"
|
||||||
|
|
||||||
val Factory: ViewModelProvider.Factory = viewModelFactory {
|
/**
|
||||||
|
* M4.3c: graph-based factory — but this ViewModel genuinely needs
|
||||||
|
* the ANDROID services (SecurityManager, Keystore key provider,
|
||||||
|
* BackupManager's Uri IO, CarnetImporter), so it takes the
|
||||||
|
* concrete AppContainer, not just the AppGraph face. It stays in
|
||||||
|
* androidMain until those services gain desktop answers (or the
|
||||||
|
* UI hides them — plan §6.5).
|
||||||
|
*/
|
||||||
|
fun factory(container: AppContainer): ViewModelProvider.Factory = viewModelFactory {
|
||||||
initializer {
|
initializer {
|
||||||
val app = this[APPLICATION_KEY] as IncidoApplication
|
val c = container
|
||||||
val c = app.container
|
|
||||||
SettingsViewModel(
|
SettingsViewModel(
|
||||||
settings = c.settingsRepository,
|
settings = c.settingsRepository,
|
||||||
security = c.securityManager,
|
security = c.securityManager,
|
||||||
|
|||||||
+3
-2
@@ -40,6 +40,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.incido.compose.di.requireAndroidContainer
|
||||||
import org.incido.compose.ui.settings.SettingsViewModel
|
import org.incido.compose.ui.settings.SettingsViewModel
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,7 +58,7 @@ import org.incido.compose.ui.settings.SettingsViewModel
|
|||||||
* stored hash + salt. The screen pops on success and surfaces a
|
* stored hash + salt. The screen pops on success and surfaces a
|
||||||
* "PIN updated." snackbar via the existing viewModel.snackbar flow.
|
* "PIN updated." snackbar via the existing viewModel.snackbar flow.
|
||||||
*
|
*
|
||||||
* Reuses the existing [SettingsViewModel.Factory] — the new screen
|
* Reuses the existing [SettingsViewModel.factory] — the new screen
|
||||||
* doesn't need its own ViewModel because every operation goes through
|
* doesn't need its own ViewModel because every operation goes through
|
||||||
* the existing security manager.
|
* the existing security manager.
|
||||||
*/
|
*/
|
||||||
@@ -66,7 +67,7 @@ import org.incido.compose.ui.settings.SettingsViewModel
|
|||||||
fun PinChangeScreen(
|
fun PinChangeScreen(
|
||||||
onDone: () -> Unit,
|
onDone: () -> Unit,
|
||||||
onCancel: () -> Unit,
|
onCancel: () -> Unit,
|
||||||
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.Factory),
|
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.factory(requireAndroidContainer())),
|
||||||
) {
|
) {
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
var step by remember { mutableStateOf(Step.VerifyOld) }
|
var step by remember { mutableStateOf(Step.VerifyOld) }
|
||||||
|
|||||||
+2
-1
@@ -36,6 +36,7 @@ import androidx.compose.ui.text.input.PasswordVisualTransformation
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import org.incido.compose.di.requireAndroidContainer
|
||||||
import org.incido.compose.ui.settings.SettingsViewModel
|
import org.incido.compose.ui.settings.SettingsViewModel
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,7 +61,7 @@ import org.incido.compose.ui.settings.SettingsViewModel
|
|||||||
fun PinSetupScreen(
|
fun PinSetupScreen(
|
||||||
onDone: () -> Unit,
|
onDone: () -> Unit,
|
||||||
onCancel: () -> Unit,
|
onCancel: () -> Unit,
|
||||||
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.Factory),
|
viewModel: SettingsViewModel = viewModel(factory = SettingsViewModel.factory(requireAndroidContainer())),
|
||||||
) {
|
) {
|
||||||
val scope = rememberCoroutineScope()
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,13 @@ import org.incido.domain.repository.NoteRepository
|
|||||||
interface AppGraph {
|
interface AppGraph {
|
||||||
val noteRepository: NoteRepository
|
val noteRepository: NoteRepository
|
||||||
val settingsRepository: SettingsRepository
|
val settingsRepository: SettingsRepository
|
||||||
|
/**
|
||||||
|
* M4.3c: the encryption-badge feed (verified/unverified note ids from
|
||||||
|
* the trial-decrypt sweep). Both platforms genuinely run
|
||||||
|
* `EncryptionVerifier` on their own store, so this is a shared face,
|
||||||
|
* not an Android concept — see EncryptionBadge.kt for the mapping.
|
||||||
|
*/
|
||||||
|
val encryptionBadge: EncryptionBadge
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package org.incido.compose.di
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phase 1 (M4.3c) — the commonMain mirror of `EncryptionVerification`
|
||||||
|
* (`org.incido.data.crypto`, :core:data shared sources).
|
||||||
|
*
|
||||||
|
* Why a mirror instead of the real type: :core:data is an androidLibrary —
|
||||||
|
* its classes are invisible to a KMP commonMain, and it can never become
|
||||||
|
* multiplatform without colliding with invariant §2.3
|
||||||
|
* (objectbox-android and objectbox-java may never share a classpath).
|
||||||
|
* A ViewModel only ever READS the verification result, so the graph
|
||||||
|
* exposes this read-only view; both containers map the real class into it
|
||||||
|
* with a 3-field copy at the seam.
|
||||||
|
*/
|
||||||
|
data class LibraryVerification(
|
||||||
|
val verifiedNoteIds: Set<Long>,
|
||||||
|
val failedNoteIds: Set<Long>,
|
||||||
|
/** [System.currentTimeMillis] when the check ran (set by the platform). */
|
||||||
|
val checkedAt: Long,
|
||||||
|
) {
|
||||||
|
val allOk: Boolean get() = failedNoteIds.isEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The encryption-badge feed the note list binds to: `null` until the
|
||||||
|
* first verification sweep completes; empty sets when the library is
|
||||||
|
* unencrypted. `EncryptionVerifier`'s common face (see [LibraryVerification]
|
||||||
|
* for why the concrete class can't cross the seam).
|
||||||
|
*/
|
||||||
|
interface EncryptionBadge {
|
||||||
|
val verification: Flow<LibraryVerification?>
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.incido.compose.di
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Phase 1 (M4.3c): `Dispatchers.IO` has no commonMain declaration
|
||||||
|
* (coroutines 1.9 keeps it JVM-only). NoteEditorViewModel swaps its four
|
||||||
|
* `withContext(Dispatchers.IO)` sites for this seam — both actuals are
|
||||||
|
* literally `Dispatchers.IO`, so the Android behaviour is unchanged
|
||||||
|
* (invariant §2.1), it is merely spelled through an expect/actual.
|
||||||
|
*/
|
||||||
|
internal expect val ioDispatcher: CoroutineDispatcher
|
||||||
+8
-9
@@ -2,11 +2,9 @@ package org.incido.compose.ui.editor
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
|
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.lifecycle.viewmodel.initializer
|
import androidx.lifecycle.viewmodel.initializer
|
||||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.FlowPreview
|
import kotlinx.coroutines.FlowPreview
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
@@ -21,7 +19,8 @@ import kotlinx.coroutines.flow.stateIn
|
|||||||
import kotlinx.coroutines.flow.update
|
import kotlinx.coroutines.flow.update
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import org.incido.compose.IncidoApplication
|
import org.incido.compose.di.AppGraph
|
||||||
|
import org.incido.compose.di.ioDispatcher
|
||||||
import org.incido.compose.share.LinkedShareComposer
|
import org.incido.compose.share.LinkedShareComposer
|
||||||
import org.incido.domain.model.ChecklistItem
|
import org.incido.domain.model.ChecklistItem
|
||||||
import org.incido.domain.model.Hashtag
|
import org.incido.domain.model.Hashtag
|
||||||
@@ -614,7 +613,7 @@ class NoteEditorViewModel(
|
|||||||
val existingId = _state.value.existing?.id
|
val existingId = _state.value.existing?.id
|
||||||
if (existingId != null && existingId > 0L) {
|
if (existingId != null && existingId > 0L) {
|
||||||
// Persist straight to the DB; observeAttachments will surface it.
|
// Persist straight to the DB; observeAttachments will surface it.
|
||||||
withContext(Dispatchers.IO) {
|
withContext(ioDispatcher) {
|
||||||
repository.addAttachment(
|
repository.addAttachment(
|
||||||
noteId = existingId,
|
noteId = existingId,
|
||||||
bytes = bytes,
|
bytes = bytes,
|
||||||
@@ -656,7 +655,7 @@ class NoteEditorViewModel(
|
|||||||
/** Delete a persisted attachment by id (used by the ✕ on the thumbnail strip). */
|
/** Delete a persisted attachment by id (used by the ✕ on the thumbnail strip). */
|
||||||
fun deletePersistedAttachment(id: Long) {
|
fun deletePersistedAttachment(id: Long) {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
withContext(Dispatchers.IO) { repository.removeAttachment(id) }
|
withContext(ioDispatcher) { repository.removeAttachment(id) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -685,7 +684,7 @@ class NoteEditorViewModel(
|
|||||||
val stalePersisted = _state.value.attachments
|
val stalePersisted = _state.value.attachments
|
||||||
.filter { it.mimeType.startsWith("audio/") }
|
.filter { it.mimeType.startsWith("audio/") }
|
||||||
if (stalePersisted.isNotEmpty()) {
|
if (stalePersisted.isNotEmpty()) {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(ioDispatcher) {
|
||||||
stalePersisted.forEach { repository.removeAttachment(it.id) }
|
stalePersisted.forEach { repository.removeAttachment(it.id) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -799,7 +798,7 @@ class NoteEditorViewModel(
|
|||||||
current.pendingAttachments.forEach { pending ->
|
current.pendingAttachments.forEach { pending ->
|
||||||
val bytes = current.pendingAttachmentBytes[pending.tempId]
|
val bytes = current.pendingAttachmentBytes[pending.tempId]
|
||||||
if (bytes != null) {
|
if (bytes != null) {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(ioDispatcher) {
|
||||||
repository.addAttachment(
|
repository.addAttachment(
|
||||||
noteId = saved.id,
|
noteId = saved.id,
|
||||||
bytes = bytes,
|
bytes = bytes,
|
||||||
@@ -847,6 +846,7 @@ class NoteEditorViewModel(
|
|||||||
const val DRAFT_AUTOSAVE_DEBOUNCE_MS = 400L
|
const val DRAFT_AUTOSAVE_DEBOUNCE_MS = 400L
|
||||||
|
|
||||||
fun factory(
|
fun factory(
|
||||||
|
graph: AppGraph,
|
||||||
noteId: Long?,
|
noteId: Long?,
|
||||||
initialSharedText: String? = null,
|
initialSharedText: String? = null,
|
||||||
initialSharedImages: List<SharedImage> = emptyList(),
|
initialSharedImages: List<SharedImage> = emptyList(),
|
||||||
@@ -858,9 +858,8 @@ class NoteEditorViewModel(
|
|||||||
initialSharedSubject: String? = null,
|
initialSharedSubject: String? = null,
|
||||||
): ViewModelProvider.Factory = viewModelFactory {
|
): ViewModelProvider.Factory = viewModelFactory {
|
||||||
initializer {
|
initializer {
|
||||||
val app = this[APPLICATION_KEY] as IncidoApplication
|
|
||||||
NoteEditorViewModel(
|
NoteEditorViewModel(
|
||||||
repository = app.container.noteRepository,
|
repository = graph.noteRepository,
|
||||||
noteId = noteId,
|
noteId = noteId,
|
||||||
initialSharedText = initialSharedText,
|
initialSharedText = initialSharedText,
|
||||||
initialSharedImages = initialSharedImages,
|
initialSharedImages = initialSharedImages,
|
||||||
+11
-9
@@ -2,7 +2,6 @@ package org.incido.compose.ui.notelist
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
|
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import androidx.lifecycle.viewmodel.initializer
|
import androidx.lifecycle.viewmodel.initializer
|
||||||
import androidx.lifecycle.viewmodel.viewModelFactory
|
import androidx.lifecycle.viewmodel.viewModelFactory
|
||||||
@@ -16,7 +15,8 @@ import kotlinx.coroutines.flow.debounce
|
|||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.stateIn
|
import kotlinx.coroutines.flow.stateIn
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.incido.compose.IncidoApplication
|
import org.incido.compose.di.AppGraph
|
||||||
|
import org.incido.compose.di.EncryptionBadge
|
||||||
import org.incido.domain.model.HashtagUsage
|
import org.incido.domain.model.HashtagUsage
|
||||||
import org.incido.domain.model.Note
|
import org.incido.domain.model.Note
|
||||||
import org.incido.domain.model.NoteFilter
|
import org.incido.domain.model.NoteFilter
|
||||||
@@ -52,8 +52,11 @@ data class NoteListUiState(
|
|||||||
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
|
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
|
||||||
class NoteListViewModel(
|
class NoteListViewModel(
|
||||||
private val repository: NoteRepository,
|
private val repository: NoteRepository,
|
||||||
/** v0.3.5.0 — source of the verified-encryption badge set. */
|
/** v0.3.5.0 — source of the verified-encryption badge set.
|
||||||
private val encryptionVerifier: org.incido.data.crypto.EncryptionVerifier? = null,
|
* M4.3c: the concrete `EncryptionVerifier` (androidLibrary sources)
|
||||||
|
* becomes the commonMain [EncryptionBadge] face — same StateFlow
|
||||||
|
* contents, mapped 1:1 by each container. */
|
||||||
|
private val encryptionBadge: EncryptionBadge? = null,
|
||||||
) : ViewModel() {
|
) : ViewModel() {
|
||||||
|
|
||||||
private val filterMode = MutableStateFlow<NoteFilter>(NoteFilter.Home)
|
private val filterMode = MutableStateFlow<NoteFilter>(NoteFilter.Home)
|
||||||
@@ -112,7 +115,7 @@ class NoteListViewModel(
|
|||||||
*/
|
*/
|
||||||
val uiState: StateFlow<NoteListUiState> = combine(
|
val uiState: StateFlow<NoteListUiState> = combine(
|
||||||
baseUiState,
|
baseUiState,
|
||||||
encryptionVerifier?.verification
|
encryptionBadge?.verification
|
||||||
?: kotlinx.coroutines.flow.flowOf(null),
|
?: kotlinx.coroutines.flow.flowOf(null),
|
||||||
) { state, verification ->
|
) { state, verification ->
|
||||||
state.copy(
|
state.copy(
|
||||||
@@ -175,12 +178,11 @@ class NoteListViewModel(
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val Factory: ViewModelProvider.Factory = viewModelFactory {
|
fun factory(graph: AppGraph): ViewModelProvider.Factory = viewModelFactory {
|
||||||
initializer {
|
initializer {
|
||||||
val app = this[APPLICATION_KEY] as IncidoApplication
|
|
||||||
NoteListViewModel(
|
NoteListViewModel(
|
||||||
repository = app.container.noteRepository,
|
repository = graph.noteRepository,
|
||||||
encryptionVerifier = app.container.encryptionVerifier,
|
encryptionBadge = graph.encryptionBadge,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package org.incido.compose.di
|
||||||
|
|
||||||
|
import kotlinx.coroutines.CoroutineDispatcher
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
internal actual val ioDispatcher: CoroutineDispatcher = Dispatchers.IO
|
||||||
@@ -39,6 +39,11 @@ dependencies {
|
|||||||
// `.preferences_pb` store from a file path (no Context extension).
|
// `.preferences_pb` store from a file path (no Context extension).
|
||||||
implementation(project(":core:settings"))
|
implementation(project(":core:settings"))
|
||||||
implementation(libs.androidx.datastore.preferences.core)
|
implementation(libs.androidx.datastore.preferences.core)
|
||||||
|
// Phase 1 (M4.3c): composeApp's jvm target — the AppGraph face and
|
||||||
|
// the commonMain ViewModels. DesktopContainer implementing AppGraph
|
||||||
|
// is the M4.3 exit proof, checked by the compiler. The stand-in UI
|
||||||
|
// keeps running until M4.5 consumes the real screens.
|
||||||
|
implementation(project(":composeApp"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Packaging works (proven in Phase 0), but per-OS installers and icons are
|
// Packaging works (proven in Phase 0), but per-OS installers and icons are
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ suspend fun runCryptoSelfTest(app: DesktopContainer, passphrase: String): Boolea
|
|||||||
// 1. A probe note whose exact content we can look for again later.
|
// 1. A probe note whose exact content we can look for again later.
|
||||||
val marker = "M3-PROBE-${System.currentTimeMillis()} secret-at-rest"
|
val marker = "M3-PROBE-${System.currentTimeMillis()} secret-at-rest"
|
||||||
val now = System.currentTimeMillis()
|
val now = System.currentTimeMillis()
|
||||||
val probe = app.repository.saveNoteWithHashtags(
|
val probe = app.noteRepository.saveNoteWithHashtags(
|
||||||
Note(id = 0L, content = marker, createdAt = now, updatedAt = now),
|
Note(id = 0L, content = marker, createdAt = now, updatedAt = now),
|
||||||
hashtagsToAdd = emptySet(),
|
hashtagsToAdd = emptySet(),
|
||||||
hashtagsToRemove = emptySet(),
|
hashtagsToRemove = emptySet(),
|
||||||
@@ -65,7 +65,7 @@ suspend fun runCryptoSelfTest(app: DesktopContainer, passphrase: String): Boolea
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 4. Repository reads decrypt transparently.
|
// 4. Repository reads decrypt transparently.
|
||||||
val readBack = app.repository.observeAll().first()
|
val readBack = app.noteRepository.observeAll().first()
|
||||||
check(
|
check(
|
||||||
"repository decrypts transparently",
|
"repository decrypts transparently",
|
||||||
readBack.any { it.content == marker },
|
readBack.any { it.content == marker },
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.cancel
|
import kotlinx.coroutines.cancel
|
||||||
import org.incido.compose.data.settings.SettingsRepository
|
import org.incido.compose.data.settings.SettingsRepository
|
||||||
|
import org.incido.compose.di.AppGraph
|
||||||
|
import org.incido.compose.di.EncryptionBadge
|
||||||
|
import org.incido.compose.di.LibraryVerification
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
import org.incido.data.ImageOrientationNormalizer
|
import org.incido.data.ImageOrientationNormalizer
|
||||||
import org.incido.data.ObjectBoxStore
|
import org.incido.data.ObjectBoxStore
|
||||||
import org.incido.data.crypto.AesGcmFieldCipher
|
import org.incido.data.crypto.AesGcmFieldCipher
|
||||||
@@ -45,12 +49,12 @@ import org.incido.domain.repository.NoteRepository
|
|||||||
* EXIF rotation remains the one absent Android behaviour (M4 — needs a
|
* EXIF rotation remains the one absent Android behaviour (M4 — needs a
|
||||||
* JVM image decoder).
|
* JVM image decoder).
|
||||||
*/
|
*/
|
||||||
class DesktopContainer(val dataDir: File) {
|
class DesktopContainer(val dataDir: File) : AppGraph {
|
||||||
|
|
||||||
/** Process-wide scope for the verifier, mirroring AppContainer.appScope. */
|
/** Process-wide scope for the verifier, mirroring AppContainer.appScope. */
|
||||||
val appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
val appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||||
|
|
||||||
val settingsRepository: SettingsRepository =
|
override val settingsRepository: SettingsRepository =
|
||||||
SettingsRepository(preferencesStore(dataDir, "incido_settings"))
|
SettingsRepository(preferencesStore(dataDir, "incido_settings"))
|
||||||
|
|
||||||
val keyProvider = DesktopKeyProvider(dataDir)
|
val keyProvider = DesktopKeyProvider(dataDir)
|
||||||
@@ -74,7 +78,7 @@ class DesktopContainer(val dataDir: File) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val repository: NoteRepository = NoteRepositoryImpl(
|
override val noteRepository: NoteRepository = NoteRepositoryImpl(
|
||||||
boxStore = boxStore,
|
boxStore = boxStore,
|
||||||
cipherHolder = cipherHolder,
|
cipherHolder = cipherHolder,
|
||||||
orientationNormalizer = ImageOrientationNormalizer.NoOp,
|
orientationNormalizer = ImageOrientationNormalizer.NoOp,
|
||||||
@@ -86,6 +90,16 @@ class DesktopContainer(val dataDir: File) {
|
|||||||
/** Runs on every launch — feeds the "Encrypted" badge state. */
|
/** Runs on every launch — feeds the "Encrypted" badge state. */
|
||||||
val encryptionVerifier = EncryptionVerifier(noteCryptoService, appScope)
|
val encryptionVerifier = EncryptionVerifier(noteCryptoService, appScope)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* M4.3c [AppGraph] face — same 3-field mapping AppContainer does;
|
||||||
|
* the desktop sweep is the real M3 verifier over the desktop store.
|
||||||
|
*/
|
||||||
|
override val encryptionBadge: EncryptionBadge = object : EncryptionBadge {
|
||||||
|
override val verification = encryptionVerifier.verification.map { result ->
|
||||||
|
result?.let { LibraryVerification(it.verifiedNoteIds, it.failedNoteIds, it.checkedAt) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
encryptionVerifier.checkNow()
|
encryptionVerifier.checkNow()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ fun main(args: Array<String>) {
|
|||||||
) {
|
) {
|
||||||
NotesWindow(
|
NotesWindow(
|
||||||
app = app,
|
app = app,
|
||||||
repository = app.repository,
|
repository = app.noteRepository,
|
||||||
windowStore = windowStore,
|
windowStore = windowStore,
|
||||||
windowState = windowState,
|
windowState = windowState,
|
||||||
exitAfterSeconds = exitAfterSeconds,
|
exitAfterSeconds = exitAfterSeconds,
|
||||||
|
|||||||
Reference in new issue
Block a user