Modern Apps git

← Modern-Apps.git

notes: compose the list on the first frame, migrate in background

Commit
5b18ae5f321d1c873357a7c735fd4afeecc9fe99
Author
Vayun Mathur <[email protected]>
When
2026-12-05 18:26 UTC (16 hours ago)
Parents
d204bff
MainActivity gated Navigation behind a ready flag that only flipped
after the IO startup block finished: repository open (SQLCipher
keystore decrypt on first DB access), legacy-notes clear,
content-addressed image migration and tombstone GC. The first frame
was therefore a centred Loading label for the whole migration,
however long the database took to open.

Repository construction itself is cheap (RoomRepository opens the
database lazily on first query), so assign it synchronously and
compose Navigation immediately: the list renders its real scaffold
on frame one and populates from the notes Flow as the database
opens. The migrations still run at start on Dispatchers.IO and land
through the same Flow; WearNotesSync still starts only after they
complete. Accepted trade-off, documented in AGENTS.md: a note
opened in the brief window before the image-reference rewrite
lands may briefly miss an image, then updates when the rewrite
re-emits its row.

Verified: :notes:lint :notes:lintVitalRelease :notes:testDevUnitTest pass.

Diff

notes/AGENTS.md
diff --git a/notes/AGENTS.md b/notes/AGENTS.mdindex 4cfd615d2f..a379be9f80 100644--- a/notes/AGENTS.md+++ b/notes/AGENTS.md@@ -16,7 +16,7 @@ _Install: ./install notes (dev by default)._ - Block-based editor (`Note`/`NoteBlock`) with stylus ink (`:library:ink`) and images (`:library:image`, content-addressed `sha256.jpg`) - `.md` import/export — opens files shared from other apps, desktop file-drop, shares notes back out - OpenAssistant read/add via `intents/`; local backup/restore via the in-app backup buttons (`BackupButtons`); platform backup via `platform/AppBackupAgent` (key/value `BaseBackupAgent` + `SqlCipherDbCodec`: the DB is exported to plaintext in the backup payload and re-encrypted under the new device's passphrase on restore; `data_extraction_rules.xml` excludes the raw `notes-db`/`passwords-db` files and `secure_prefs` from file copy on both channels, and carries `note_images`/settings/watch store by device transfer)-**Architecture / data-flow:** `ui/` Compose list/detail → `platform/NotesViewModel` (+ `NotesUiContract`) → `data/NotesRepository` singleton over Room (`Note`/`NoteBlock`/`NoteDatabase`). `MainActivity.onCreateBeforeContent`: repo init → one-time legacy-notes clear + image `img_<uuid>.jpg → sha256.jpg` migration with block-reference rewrite → `ready` gate before `Navigation`.+**Architecture / data-flow:** `ui/` Compose list/detail → `platform/NotesViewModel` (+ `NotesUiContract`) → `data/NotesRepository` singleton over Room (`Note`/`NoteBlock`/`NoteDatabase`). `MainActivity.onCreateBeforeContent`: cheap repo construction (Room opens lazily on first query) so `Navigation` composes on the first frame; the one-time legacy-notes clear + image `img_<uuid>.jpg → sha256.jpg` migration with block-reference rewrite + tombstone GC run on `Dispatchers.IO` in the background and land through the notes Flow. **Entry points + reading order:** `Route` (`NotesList` / `Note(id)` / `ExternalNote(uri)`) → `MainActivity.kt` → `Navigation.kt` → `platform/NotesViewModel.kt` → `data/NotesRepository.kt`.  ## One APK, two form factors (phone + watch)@@ -51,7 +51,7 @@ routing, so every entry point is inert on the wrong form factor. - `ui/WearMainActivity` (a `WearShellActivity`) routes `ui/WearRoute` over the recent-notes list / note view / quick-note editor, rendered only from `data/WearNotesStore` (`wear-notes-snapshot.json` + `wear-notes-pending.json`) so it works with the phone absent. - Watch listing screenshots are the `group = "wear"` previews in `src/screenshotTest/.../ui/WearMetadataPreviews.kt`, collected by `:notes:metadata` into `metadata_data/photos/notes/wear/`; phone shots use the `phone` bucket. **Gotchas:**-- Migrations must finish before `ready = true` — image references break if skipped+- Migrations run at every start until they are no-ops; a note opened before the image-reference rewrite lands may briefly miss an image, then updates when the rewrite re-emits the row - External/shared URIs enter via the `ExternalNote` route, not the list - `intents/` DTOs are the copy-pattern for cross-app IPC; keep them thin 
notes/src/main/java/com/vayunmathur/notes/MainActivity.kt
diff --git a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt b/notes/src/main/java/com/vayunmathur/notes/MainActivity.ktindex 3f533629cd..e47f617759 100644--- a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt+++ b/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt@@ -6,10 +6,8 @@ import androidx.activity.viewModels import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable-import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.lifecycleScope import com.vayunmathur.library.ui.MaActivity-import com.vayunmathur.library.ui.Text import com.vayunmathur.library.util.IntentHelper import com.vayunmathur.library.util.onFileDrop import com.vayunmathur.notes.data.NotesRepository@@ -21,9 +19,6 @@ import com.vayunmathur.notes.ui.WearMainActivity import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext-import androidx.compose.runtime.getValue-import androidx.compose.runtime.setValue-import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier  /**@@ -51,17 +46,21 @@ class MainActivity : MaActivity() {     private val notesViewModel: NotesViewModel by viewModels {         NotesViewModelFactory(application, notesRepository)     }-    private var ready by mutableStateOf(false)      override fun onCreateBeforeContent() {         if (routedToWear) return+        // Repository construction is cheap (the Room database itself opens+        // lazily on first query), so assign it synchronously: the list can+        // compose on the first frame and populate from the notes Flow as+        // soon as the database opens, instead of waiting behind a+        // "Loading…" gate for the one-time migrations below.+        notesRepository = NotesRepository.get(applicationContext)         // Wear companion sync over the Watch conduit client (no Play         // services, no Bluetooth prompt here — prompting is the Watch         // app's job) lives and dies with this process (the conduit has         // no system wakeup); it publishes once the repository is ready below.         com.vayunmathur.notes.sync.WearNotesSync.get(this)         lifecycleScope.launch(Dispatchers.IO) {-            notesRepository = NotesRepository.get(applicationContext)             val legacyNotes = notesRepository.readAndClearLegacyNotes()             // Legacy rows carry primary keys from the old database file; writing             // them verbatim would overwrite any current note with the same id.@@ -82,7 +81,6 @@ class MainActivity : MaActivity() {             withContext(Dispatchers.Main) {                 handleIntent(intent)                 com.vayunmathur.notes.sync.WearNotesSync.get(applicationContext).start()-                ready = true             }         }     }@@ -95,13 +93,14 @@ class MainActivity : MaActivity() {         Box(Modifier.fillMaxSize().onFileDrop { uris ->             notesViewModel.importFiles(uris)         }) {-            // Migration (legacy-notes clear + content-addressed image rewrite)-            // must finish before Navigation: show the shared loading indicator-            // instead of a blank screen while it runs on Dispatchers.IO.-            if (ready) Navigation(notesViewModel)-            else Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {-                Text("Loading…")-            }+            // The one-time migrations (legacy-notes clear + content-addressed+            // image rewrite + tombstone GC) run on Dispatchers.IO without+            // gating this frame: the list renders from the notes Flow, which+            // re-emits as migrated rows land. A note opened in the brief+            // window before the image-reference rewrite lands may briefly+            // miss an image; the rewrite updates the row (and the open note)+            // as soon as it completes.+            Navigation(notesViewModel)         }     }