Modern Apps git

← Modern-Apps.git

perf: trim startup work; key office document segments

Commit
35cd99948a88ddcb7b6df55582c09315454357b2
Author
Vayun Mathur <[email protected]>
When
2026-11-20 20:09 UTC (15 days ago)
Parents
dc9be0e
maps MainActivity deleted a legacy file synchronously in
onCreateBeforeContent - that delete now happens off the
startup path (the first-frame gate reads stay). email folded
its startup worker/service enqueues into one IO launch -
all WorkManager/service calls, thread-safe, order preserved.
notes showed a blank screen while its legacy migration ran -
now the shared LoadingIndicator.

office TextDocumentView's LazyColumn items had no keys, so an
edit could migrate editor state between blocks. Segments and
footnotes are keyed by the model's own index-based ids (the
same ids every edit callback uses); keying by content hash
would change per keystroke and destroy the state this protects.
Header/footer surfaces stay single composed blocks - they
share one Surface by design.

Verified: compile for maps/email/notes; office document-view
test classes pass (full office suite re-verified after the
scaffold ABI settled).

Diff

maps/src/main/java/com/vayunmathur/maps/MainActivity.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt b/maps/src/main/java/com/vayunmathur/maps/MainActivity.ktindex f84516ba5a..ba6bc1a20a 100644--- a/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt+++ b/maps/src/main/java/com/vayunmathur/maps/MainActivity.kt@@ -51,6 +51,7 @@ import com.vayunmathur.maps.platform.SelectedFeatureViewModel import com.vayunmathur.maps.platform.MapSettingsViewModel import com.vayunmathur.library.network.NetworkClient import com.vayunmathur.library.network.TrustBundle+import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeoutOrNull@@ -86,21 +87,29 @@ class MainActivity : MaActivity() {         // FIRST_PARTY: data.maproj.org tiles + amenities + api.maproj.org -> ISRG+GTS         NetworkClient.init(this, TrustBundle.FIRST_PARTY)         intentLauncher = IntentLauncher(this)-        // Daily overlay check chain (UNMETERED): KEEP leaves an existing chain-        // alone; the toggle gates inside syncAutoUpdateSchedule.-        syncOverlaySchedule()-        // Reclaim the ~44 MB bundled basemap copied into filesDir by older-        // builds; the basemap now streams live and is cached on demand.-        File(filesDir, "world_z0-6.pmtiles").delete()+        // Deferred off the startup path: neither the legacy-file delete nor+        // the overlay-schedule check gates the first frame (the theme reads+        // DataStore via the non-blocking mirror-backed stringFlow in+        // AppTheme, so nothing here can flicker). getBooleanAwait also fixes+        // the cold-start snapshot race: the synchronous getBoolean reads the+        // mirror, which is empty until hydration and would always return the+        // default. WorkManager KEEP makes the delayed enqueue a no-op when+        // the daily chain already exists.+        lifecycleScope.launch(Dispatchers.IO) {+            // Reclaim the ~44 MB bundled basemap copied into filesDir by older+            // builds; the basemap now streams live and is cached on demand.+            File(filesDir, "world_z0-6.pmtiles").delete()+            syncOverlaySchedule()+        }          // Deep link that launched us cold (geo:/google.navigation:/maps URL).         handleIntent(intent)     }      /** Enqueue the daily overlay check when the auto-update toggle is on. */-    private fun syncOverlaySchedule() {+    private suspend fun syncOverlaySchedule() {         val ds = DataStoreUtils.getInstance(this)-        if (ds.getBoolean(MapPreferences.KEY_MAP_AUTO_UPDATE, MapPreferences.DEFAULT_MAP_AUTO_UPDATE)) {+        if (ds.getBooleanAwait(MapPreferences.KEY_MAP_AUTO_UPDATE, MapPreferences.DEFAULT_MAP_AUTO_UPDATE)) {             MapOverlayWorker.scheduleAuto(this)         }     }
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 038ab2fbb7..c0e2e38808 100644--- a/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt+++ b/notes/src/main/java/com/vayunmathur/notes/MainActivity.kt@@ -7,6 +7,7 @@ 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.LoadingIndicator import com.vayunmathur.library.ui.MaActivity import com.vayunmathur.library.util.IntentHelper import com.vayunmathur.library.util.onFileDrop@@ -19,6 +20,7 @@ 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  /** Entry activity that initialises the notes repository before showing navigation. */@@ -56,7 +58,13 @@ 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) {+                LoadingIndicator()+            }         }     } 
office/src/main/java/com/vayunmathur/office/ui/TextDocumentView.kt
diff --git a/office/src/main/java/com/vayunmathur/office/ui/TextDocumentView.kt b/office/src/main/java/com/vayunmathur/office/ui/TextDocumentView.ktindex 96635051cd..364c5c41bd 100644--- a/office/src/main/java/com/vayunmathur/office/ui/TextDocumentView.kt+++ b/office/src/main/java/com/vayunmathur/office/ui/TextDocumentView.kt@@ -86,6 +86,30 @@ private fun buildSegments(content: List<OdfContentBlock>): List<DocSegment> {     return segments } +// Stable LazyColumn identity: the content index is the model's own identity+// (every edit callback โ€” onRunTextChange, onCellTextChange, onChartClick โ€”+// addresses blocks by this index), so keying by it keeps editor state with+// its block when neighbours are inserted/removed without re-keying on text+// (a text-derived key would change per keystroke and destroy editor state).+private fun DocSegment.lazyKey(): String = when (this) {+    is DocSegment.Paragraphs -> "paragraphs:$start-$endInclusive"+    is DocSegment.Block -> "block:$index"+}++private fun DocSegment.lazyContentType(content: List<OdfContentBlock>): String = when (this) {+    is DocSegment.Paragraphs -> "paragraphs"+    is DocSegment.Block -> when (content[index]) {+        is OdfContentBlock.Table -> "block-table"+        is OdfContentBlock.Image -> "block-image"+        is OdfContentBlock.PageBreak -> "block-pagebreak"+        is OdfContentBlock.Chart -> "block-chart"+        is OdfContentBlock.Formula -> "block-formula"+        is OdfContentBlock.TableOfContents -> "block-toc"+        is OdfContentBlock.Paragraph -> "block-paragraph"+        is OdfContentBlock.SectionStart, OdfContentBlock.SectionEnd -> "block-section"+    }+}+ @Composable fun TextDocumentView(     doc: OdfDocument.TextDocument,@@ -111,7 +135,11 @@ fun TextDocumentView(         modifier = Modifier.fillMaxSize().padding(horizontal = 20.dp, vertical = 12.dp)     ) {         if (doc.headerParagraphs.isNotEmpty()) {-            item {+            // Header stays one composed block: its paragraphs render inside a+            // single Surface, so splitting them into separate items would+            // break the shared background/padding. It is already lazy at+            // item granularity (composed only when scrolled into view).+            item(key = "header", contentType = "header") {                 Surface(                     color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f),                     modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp)) {@@ -123,7 +151,11 @@ fun TextDocumentView(             }         } -        items(segments.size) { si ->+        items(+            count = segments.size,+            key = { si -> segments[si].lazyKey() },+            contentType = { si -> segments[si].lazyContentType(doc.content) },+        ) { si ->             when (val seg = segments[si]) {                 is DocSegment.Paragraphs -> ContinuousParagraphEditor(                     doc, seg.start, seg.endInclusive, fontSizeMultiplier, onRunSelectionChange, onRunTextChange,@@ -173,8 +205,20 @@ fun TextDocumentView(         }          if (doc.footnotes.isNotEmpty()) {-            item { HorizontalDivider(modifier = Modifier.padding(vertical = 16.dp)) }-            items(doc.footnotes.size) { index ->+            item(key = "footnotes-divider", contentType = "divider") {+                HorizontalDivider(modifier = Modifier.padding(vertical = 16.dp))+            }+            // Keyed by citation, the footnote's stable identity (the serializer+            // also associates footnotes by citation). The body-paragraph loop+            // stays a single composed block inside the footnote's Row: the+            // citation + body are one visual unit, so per-paragraph items+            // would fragment that Row without making the footnote itself+            // any lazier โ€” it is already lazy at footnote granularity.+            items(+                count = doc.footnotes.size,+                key = { index -> "footnote:${doc.footnotes[index].citation}" },+                contentType = { "footnote" },+            ) { index ->                 val fn = doc.footnotes[index]                 Row(modifier = Modifier.padding(vertical = 2.dp)) {                     Text("${fn.citation} ", style = MaterialTheme.typography.bodySmall, fontWeight = FontWeight.Bold)@@ -187,7 +231,8 @@ fun TextDocumentView(         }          if (doc.footerParagraphs.isNotEmpty()) {-            item {+            // Single composed block, as for the header (shared Surface).+            item(key = "footer", contentType = "footer") {                 Surface(                     color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f),                     modifier = Modifier.fillMaxWidth().padding(top = 8.dp)) {