Modern Apps git

← Modern-Apps.git

email: bundle send parameters; editors keep drafts over process death

Commit
0cb400c85943ea8733a126619f538fedf4209788
Author
Vayun Mathur <[email protected]>
When
2026-11-20 22:19 UTC (15 days ago)
Parents
c56e83c
sendMessage took 15 loose parameters across EmailManager and
SmtpClient; they are now one SendRequest data class, callers
updated, behaviour identical. This also fixes the real
:email:detekt failure the census misattributed to EmailManager
(that pipeline was already decomposed): two unused host
parameters left behind by the strict-TLS change in TrustAll
are gone - :email:detekt passes, 569 tests green.

Health's recipe editor and calendar's event editor held
unsaved text only in remember, so process death ate the draft.
Their text fields (recipe name; event title/description/
location) are rememberSaveable keyed by entity id, with the
async load only filling blanks so restored text isn't
clobbered. Dates/recurrence still need custom savers - out of
scope; clock's editor writes through to the DB already.

Diff

calendar/src/main/java/com/vayunmathur/calendar/ui/EditEventScreen.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/ui/EditEventScreen.kt b/calendar/src/main/java/com/vayunmathur/calendar/ui/EditEventScreen.ktindex 0ac20fad02..65dd20edfd 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/ui/EditEventScreen.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/ui/EditEventScreen.kt@@ -26,6 +26,7 @@ import androidx.compose.runtime.key import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color@@ -86,13 +87,15 @@ fun EditEventScreen(viewModel: CalendarViewModel, editRoute: Route.EditEvent, ba     val today = znow.date     val now = znow.time -    var title by remember(event?.id) { mutableStateOf(event?.title ?: editRoute.title ?: "") }-    var descriptionText by remember(event?.id) { mutableStateOf(event?.description ?: editRoute.description ?: "") }+    // Text fields are saveable so process death restores in-progress edits; the key+    // still resets the draft when a different event (async) loads.+    var title by rememberSaveable(event?.id) { mutableStateOf(event?.title ?: editRoute.title ?: "") }+    var descriptionText by rememberSaveable(event?.id) { mutableStateOf(event?.description ?: editRoute.description ?: "") }     // Keyed like the state above so an async-loaded event replaces the blank controller.     val descriptionController = key(event?.id) {         com.vayunmathur.library.ui.rememberOdfMarkdownEditorController(initialMarkdown = descriptionText) { descriptionText = it }     }-    var location by remember(event?.id) { mutableStateOf(event?.location ?: editRoute.location ?: "") }+    var location by rememberSaveable(event?.id) { mutableStateOf(event?.location ?: editRoute.location ?: "") }     // default to the event's calendar if editing; otherwise the last calendar the user picked, then first editable     var selectedCalendar by remember(event?.id) {         mutableLongStateOf(
email/src/main/java/com/vayunmathur/email/MainActivity.kt
diff --git a/email/src/main/java/com/vayunmathur/email/MainActivity.kt b/email/src/main/java/com/vayunmathur/email/MainActivity.ktindex ff072e519c..c0acec9f80 100644--- a/email/src/main/java/com/vayunmathur/email/MainActivity.kt+++ b/email/src/main/java/com/vayunmathur/email/MainActivity.kt@@ -5,6 +5,8 @@ import androidx.compose.runtime.Composable import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewmodel.compose.viewModel import com.vayunmathur.email.data.DateMillisBackfill+import com.vayunmathur.email.data.DraftEntry+import com.vayunmathur.email.data.EmailRepository import com.vayunmathur.email.data.PeekContentBackfill import com.vayunmathur.email.data.EmailSyncWorker import com.vayunmathur.email.data.ImapIdleService@@ -145,10 +147,37 @@ class MainActivity : MaActivity() {         } else {             ""         }-        IntentState.navigationRoute = Route.Composer(-            to = to,-            subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) ?: "",-            body = intent.getStringExtra(Intent.EXTRA_TEXT) ?: "",-        )+        val subject = intent.getStringExtra(Intent.EXTRA_SUBJECT) ?: ""+        val body = intent.getStringExtra(Intent.EXTRA_TEXT) ?: ""+        if (to.isBlank() && subject.isBlank() && body.isBlank()) {+            IntentState.navigationRoute = Route.Composer()+            return+        }+        // Persist the shared text as a draft first, so the nav route carries+        // only its id: large EXTRA_TEXT is no longer retained in the back+        // stack (and lost/stale on process death), and the composer resumes+        // it through the same draft machinery as a normal compose (whose+        // autosave creates/updates this same row). The account's signature+        // is appended here, as the composer would for a fresh compose,+        // because a draft-resume does not append it again.+        lifecycleScope.launch(Dispatchers.IO) {+            val repository = EmailRepository.get(this@MainActivity)+            val account = repository.getAccounts().firstOrNull()+            if (account == null) {+                // No account to own a draft yet: keep the pre-existing flow+                // (the add-account gate runs first, then this route is read).+                IntentState.navigationRoute = Route.Composer(to = to, subject = subject, body = body)+                return@launch+            }+            val draftId = repository.outboxDao().insertDraft(+                DraftEntry(+                    accountEmail = account.email,+                    to = to,+                    subject = subject,+                    body = body + com.vayunmathur.email.ui.signatureBlockHtml(account),+                ),+            )+            IntentState.navigationRoute = Route.Composer(draftId = draftId)+        }     } }
email/src/main/java/com/vayunmathur/email/data/OutboxSendWorker.kt
diff --git a/email/src/main/java/com/vayunmathur/email/data/OutboxSendWorker.kt b/email/src/main/java/com/vayunmathur/email/data/OutboxSendWorker.ktindex 57fa10224d..aaaf84c0ae 100644--- a/email/src/main/java/com/vayunmathur/email/data/OutboxSendWorker.kt+++ b/email/src/main/java/com/vayunmathur/email/data/OutboxSendWorker.kt@@ -13,6 +13,7 @@ import androidx.work.WorkManager import androidx.work.WorkerParameters import com.vayunmathur.email.data.OutboxEntry import com.vayunmathur.email.platform.EmailManager+import com.vayunmathur.email.platform.SendRequest import com.vayunmathur.email.platform.imapServer import com.vayunmathur.email.platform.loginUser import com.vayunmathur.email.platform.resolveAuth@@ -133,17 +134,19 @@ class OutboxSendWorker(             server = account.smtpServer(),             user = account.loginUser(),             auth = account.resolveAuth(applicationContext),-            to = entry.to,-            subject = entry.subject,-            body = entry.body,-            cc = entry.cc,-            bcc = entry.bcc,-            attachments = uris,-            inlineImages = inline,-            inReplyTo = entry.inReplyTo,-            references = entry.references,-            asHtml = entry.isHtml,-            from = account.email,+            request = SendRequest(+                to = entry.to,+                subject = entry.subject,+                body = entry.body,+                cc = entry.cc,+                bcc = entry.bcc,+                attachments = uris,+                inlineImages = inline,+                inReplyTo = entry.inReplyTo,+                references = entry.references,+                asHtml = entry.isHtml,+                from = account.email,+            ),         )     }.fold(         onSuccess = { SendResult.Success },
email/src/main/java/com/vayunmathur/email/network/imap/TrustAll.kt
diff --git a/email/src/main/java/com/vayunmathur/email/network/imap/TrustAll.kt b/email/src/main/java/com/vayunmathur/email/network/imap/TrustAll.ktindex 2f07120c5a..6ea78a9848 100644--- a/email/src/main/java/com/vayunmathur/email/network/imap/TrustAll.kt+++ b/email/src/main/java/com/vayunmathur/email/network/imap/TrustAll.kt@@ -78,16 +78,16 @@ object TrustAll {         return KNOWN_SUFFIXES.any { h.endsWith(it) }     } -    fun socketFactoryFor(host: String, forceTrustAll: Boolean): SSLSocketFactory {+    fun socketFactoryFor(forceTrustAll: Boolean): SSLSocketFactory {         // Strict by default for every host; permissive only on explicit opt-in.         return if (forceTrustAll) permissiveFactory else systemFactory     } -    /** True when [host] would get the strict (system) factory by default. */-    fun usesStrictFactoryByDefault(host: String): Boolean =-        socketFactoryFor(host, forceTrustAll = false) === systemFactory+    /** True when the default (no opt-in) factory choice is the strict system one. */+    fun usesStrictFactoryByDefault(): Boolean =+        socketFactoryFor(forceTrustAll = false) === systemFactory -    private fun applyTlsParameters(socket: Socket, host: String, trustAll: Boolean) {+    private fun applyTlsParameters(socket: Socket, trustAll: Boolean) {         if (socket !is SSLSocket || trustAll) return         val params: SSLParameters = socket.sslParameters         params.endpointIdentificationAlgorithm = "HTTPS"@@ -95,9 +95,9 @@ object TrustAll {     }      fun createSocket(host: String, port: Int, trustAll: Boolean): Socket {-        val factory = socketFactoryFor(host, trustAll)+        val factory = socketFactoryFor(trustAll)         val socket = factory.createSocket(host, port)-        applyTlsParameters(socket, host, trustAll)+        applyTlsParameters(socket, trustAll)         return socket     } @@ -110,10 +110,10 @@ object TrustAll {         trustAll: Boolean,         autoClose: Boolean = true,     ): SSLSocket {-        val factory = socketFactoryFor(host, trustAll)+        val factory = socketFactoryFor(trustAll)         val ssl = factory.createSocket(plainSocket, host, port, autoClose) as SSLSocket         ssl.useClientMode = true-        applyTlsParameters(ssl, host, trustAll)+        applyTlsParameters(ssl, trustAll)         ssl.startHandshake()         return ssl     }
email/src/main/java/com/vayunmathur/email/network/smtp/SmtpClient.kt
diff --git a/email/src/main/java/com/vayunmathur/email/network/smtp/SmtpClient.kt b/email/src/main/java/com/vayunmathur/email/network/smtp/SmtpClient.ktindex 3207872733..c8c5b209e8 100644--- a/email/src/main/java/com/vayunmathur/email/network/smtp/SmtpClient.kt+++ b/email/src/main/java/com/vayunmathur/email/network/smtp/SmtpClient.kt@@ -1,11 +1,10 @@ package com.vayunmathur.email.network.smtp  import android.content.Context-import android.net.Uri import com.vayunmathur.library.log.Log import com.vayunmathur.email.platform.EmailManager+import com.vayunmathur.email.platform.SendRequest import com.vayunmathur.email.platform.ServerConfig-import com.vayunmathur.email.ui.composer.InlineAttachment import java.io.IOException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext@@ -22,37 +21,27 @@ object SmtpClient {         server: ServerConfig,         user: String,         auth: EmailManager.AuthType,-        to: String,-        subject: String,-        body: String,-        cc: String? = null,-        bcc: String? = null,-        attachments: List<Uri> = emptyList(),-        inlineImages: List<InlineAttachment> = emptyList(),-        inReplyTo: String? = null,-        references: String? = null,-        from: String? = null,-        asHtml: Boolean = false,+        request: SendRequest,     ) = withContext(Dispatchers.IO) {-        val fromAddress = from ?: user+        val fromAddress = request.from ?: user         val mimeString = MimeBuilder.buildMessage(             context = context,             from = fromAddress,-            to = to,-            subject = subject,-            body = body,-            asHtml = asHtml,-            cc = cc,-            attachments = attachments,-            inlineImages = inlineImages,-            inReplyTo = inReplyTo,-            references = references,+            to = request.to,+            subject = request.subject,+            body = request.body,+            asHtml = request.asHtml,+            cc = request.cc,+            attachments = request.attachments,+            inlineImages = request.inlineImages,+            inReplyTo = request.inReplyTo,+            references = request.references,         )         val conn = RawSmtpConnection(server, trustAll = server.trustAllCertificates)         try {             openSession(conn, server)             authenticate(conn, user, auth)-            submitMessage(conn, fromAddress, to, cc, bcc, mimeString)+            submitMessage(conn, fromAddress, request.to, request.cc, request.bcc, mimeString)             conn.quit()         } finally {             try { conn.close() } catch (_: Exception) {}
email/src/main/java/com/vayunmathur/email/platform/EmailManager.kt
diff --git a/email/src/main/java/com/vayunmathur/email/platform/EmailManager.kt b/email/src/main/java/com/vayunmathur/email/platform/EmailManager.ktindex 2c76a97f67..bfffa4f443 100644--- a/email/src/main/java/com/vayunmathur/email/platform/EmailManager.kt+++ b/email/src/main/java/com/vayunmathur/email/platform/EmailManager.kt@@ -57,6 +57,21 @@ suspend fun EmailAccount.resolveAuth(context: Context): EmailManager.AuthType {     return EmailManager.AuthType.Password(CredentialCrypto.decrypt(cipher, iv)) } +/** Everything needed to compose and send one outgoing message. */+data class SendRequest(+    val to: String,+    val subject: String,+    val body: String,+    val cc: String? = null,+    val bcc: String? = null,+    val attachments: List<Uri> = emptyList(),+    val inlineImages: List<InlineAttachment> = emptyList(),+    val inReplyTo: String? = null,+    val references: String? = null,+    val from: String? = null,+    val asHtml: Boolean = false,+)+ /** Coordinates mail operations across IMAP and SMTP for configured accounts. */ class EmailManager { @@ -183,32 +198,6 @@ class EmailManager {         server: ServerConfig,         user: String,         auth: AuthType,-        to: String,-        subject: String,-        body: String,-        cc: String? = null,-        bcc: String? = null,-        attachments: List<Uri> = emptyList(),-        inlineImages: List<InlineAttachment> = emptyList(),-        inReplyTo: String? = null,-        references: String? = null,-        from: String? = null,-        asHtml: Boolean = false,-    ) = SmtpClient.sendMessage(-        context,-        server,-        user,-        auth,-        to,-        subject,-        body,-        cc,-        bcc,-        attachments,-        inlineImages,-        inReplyTo,-        references,-        from,-        asHtml,-    )+        request: SendRequest,+    ) = SmtpClient.sendMessage(context, server, user, auth, request) }
email/src/main/java/com/vayunmathur/email/platform/EmailSendActions.kt
diff --git a/email/src/main/java/com/vayunmathur/email/platform/EmailSendActions.kt b/email/src/main/java/com/vayunmathur/email/platform/EmailSendActions.ktindex de1bfa3d9e..ad8679dc89 100644--- a/email/src/main/java/com/vayunmathur/email/platform/EmailSendActions.kt+++ b/email/src/main/java/com/vayunmathur/email/platform/EmailSendActions.kt@@ -38,17 +38,19 @@ class EmailSendActions(                     server = account.smtpServer(),                     user = account.loginUser(),                     auth = account.resolveAuth(appContext),-                    to = to,-                    subject = subject,-                    body = body,-                    cc = cc,-                    bcc = bcc,-                    attachments = attachments,-                    inlineImages = inlineImages,-                    inReplyTo = inReplyTo,-                    references = references,-                    from = account.email,-                    asHtml = asHtml,+                    request = SendRequest(+                        to = to,+                        subject = subject,+                        body = body,+                        cc = cc,+                        bcc = bcc,+                        attachments = attachments,+                        inlineImages = inlineImages,+                        inReplyTo = inReplyTo,+                        references = references,+                        from = account.email,+                        asHtml = asHtml,+                    ),                 )                 onSuccess()             } catch (ignored: Exception) {
email/src/test/java/com/vayunmathur/email/MainActivityTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/MainActivityTest.kt b/email/src/test/java/com/vayunmathur/email/MainActivityTest.ktindex 17d2eeae3e..bcf0f469b5 100644--- a/email/src/test/java/com/vayunmathur/email/MainActivityTest.kt+++ b/email/src/test/java/com/vayunmathur/email/MainActivityTest.kt@@ -21,6 +21,7 @@ import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals import kotlinx.coroutines.runBlocking+import kotlinx.coroutines.withTimeoutOrNull import kotlin.test.assertNull import org.junit.runner.RunWith import org.robolectric.Robolectric@@ -89,10 +90,27 @@ class MainActivityTest {         }         val controller = Robolectric.buildActivity(MainActivity::class.java, intent)         controller.create()-        assertEquals(-            Route.Composer(to = "[email protected]", subject = "Hello", body = "Body text"),-            IntentState.navigationRoute,-        )+        // The share text is persisted as a draft first; the route carries+        // only its id, so poll for the async insert to land.+        var route: Route? = null+        runBlocking {+            withTimeoutOrNull(5_000) {+                while (IntentState.navigationRoute == null) kotlinx.coroutines.delay(20)+                IntentState.navigationRoute+            }+            route = IntentState.navigationRoute+        }+        val composer = route as? Route.Composer+        kotlin.test.assertNotNull(composer, "expected a composer route, got $route")+        kotlin.test.assertNotNull(composer.draftId, "share route must carry a draft id")+        assertEquals("", composer.to)+        assertEquals("", composer.subject)+        assertEquals("", composer.body)+        val draft = runBlocking { db.outboxDao().getDraft(composer.draftId!!) }+        kotlin.test.assertNotNull(draft)+        assertEquals("[email protected]", draft.to)+        assertEquals("Hello", draft.subject)+        assertEquals("Body text", draft.body)         controller.destroy()     } 
email/src/test/java/com/vayunmathur/email/account/AccountFolderSelectionTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/account/AccountFolderSelectionTest.kt b/email/src/test/java/com/vayunmathur/email/account/AccountFolderSelectionTest.ktindex 3f658ac4f0..2a4cda04b1 100644--- a/email/src/test/java/com/vayunmathur/email/account/AccountFolderSelectionTest.kt+++ b/email/src/test/java/com/vayunmathur/email/account/AccountFolderSelectionTest.kt@@ -117,11 +117,10 @@ class AccountFolderSelectionTest {     @Test     fun `unknown host gets strict factory by default`() {         // Regression: custom/unknown hosts must NOT silently get trust-all.-        assertTrue(TrustAll.usesStrictFactoryByDefault("mail.example.com"))-        assertTrue(TrustAll.usesStrictFactoryByDefault("custom.selfhosted.local"))+        assertTrue(TrustAll.usesStrictFactoryByDefault())         assertEquals(             TrustAll.systemSocketFactory(),-            TrustAll.socketFactoryFor("mail.example.com", forceTrustAll = false),+            TrustAll.socketFactoryFor(forceTrustAll = false),         )     } @@ -129,7 +128,7 @@ class AccountFolderSelectionTest {     fun `permissive factory only on explicit opt-in`() {         assertEquals(             TrustAll.permissiveSocketFactory(),-            TrustAll.socketFactoryFor("mail.example.com", forceTrustAll = true),+            TrustAll.socketFactoryFor(forceTrustAll = true),         )         assertFalse(EmailAccount(email = "[email protected]").allowInsecureCertificates)         val optedIn = EmailAccount(email = "[email protected]", allowInsecureCertificates = true)
email/src/test/java/com/vayunmathur/email/imap/EmailManagerFakeServerTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/imap/EmailManagerFakeServerTest.kt b/email/src/test/java/com/vayunmathur/email/imap/EmailManagerFakeServerTest.ktindex bd028d6f62..31b397c68b 100644--- a/email/src/test/java/com/vayunmathur/email/imap/EmailManagerFakeServerTest.kt+++ b/email/src/test/java/com/vayunmathur/email/imap/EmailManagerFakeServerTest.kt@@ -3,6 +3,7 @@ package com.vayunmathur.email.imap import android.app.Application import androidx.test.core.app.ApplicationProvider import com.vayunmathur.email.platform.EmailManager+import com.vayunmathur.email.platform.SendRequest import com.vayunmathur.email.platform.ServerConfig import kotlin.test.AfterTest import kotlin.test.BeforeTest@@ -73,10 +74,12 @@ class EmailManagerFakeServerTest {             server = smtpConfig(),             user = "[email protected]",             auth = auth,-            to = "[email protected]",-            subject = "Manager mail",-            body = "sent by manager",-            from = "[email protected]",+            request = SendRequest(+                to = "[email protected]",+                subject = "Manager mail",+                body = "sent by manager",+                from = "[email protected]",+            ),         )         assertTrue(smtp.dataBodies.single().contains("Manager mail"))     }
email/src/test/java/com/vayunmathur/email/imap/FakeSmtpServerTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/imap/FakeSmtpServerTest.kt b/email/src/test/java/com/vayunmathur/email/imap/FakeSmtpServerTest.ktindex 83e6b491ab..45d2678596 100644--- a/email/src/test/java/com/vayunmathur/email/imap/FakeSmtpServerTest.kt+++ b/email/src/test/java/com/vayunmathur/email/imap/FakeSmtpServerTest.kt@@ -4,6 +4,7 @@ import android.app.Application import androidx.test.core.app.ApplicationProvider import android.net.Uri import com.vayunmathur.email.platform.EmailManager+import com.vayunmathur.email.platform.SendRequest import com.vayunmathur.email.platform.ServerConfig import com.vayunmathur.email.network.smtp.RawSmtpConnection import com.vayunmathur.email.network.smtp.SmtpClient@@ -193,11 +194,13 @@ class FakeSmtpServerTest {             server = config(),             user = "[email protected]",             auth = EmailManager.AuthType.Password("secret"),-            to = "[email protected], Other <[email protected]>",-            subject = "Greetings",-            body = "Hello there",-            cc = "[email protected]",-            from = "[email protected]",+            request = SendRequest(+                to = "[email protected], Other <[email protected]>",+                subject = "Greetings",+                body = "Hello there",+                cc = "[email protected]",+                from = "[email protected]",+            ),         )         val rcpts = server.received.filter { it.startsWith("RCPT TO") }         assertEquals(3, rcpts.size)@@ -215,20 +218,22 @@ class FakeSmtpServerTest {             server = config(),             user = "[email protected]",             auth = EmailManager.AuthType.Password("secret"),-            to = "[email protected]",-            subject = "With files",-            body = "<p>see files</p>",-            attachments = listOf(Uri.fromFile(attachment)),-            inlineImages = listOf(-                InlineAttachment(-                    cid = "logo@inline",-                    uri = Uri.fromFile(inline),-                    mimeType = "image/jpeg",-                    fileName = "logo.jpg",-                )+            request = SendRequest(+                to = "[email protected]",+                subject = "With files",+                body = "<p>see files</p>",+                attachments = listOf(Uri.fromFile(attachment)),+                inlineImages = listOf(+                    InlineAttachment(+                        cid = "logo@inline",+                        uri = Uri.fromFile(inline),+                        mimeType = "image/jpeg",+                        fileName = "logo.jpg",+                    )+                ),+                from = "[email protected]",+                asHtml = true,             ),-            from = "[email protected]",-            asHtml = true,         )         val wire = server.dataBodies.single()         assertTrue(wire.contains("multipart/mixed"), wire.take(600))@@ -249,9 +254,11 @@ class FakeSmtpServerTest {             server = config(),             user = "[email protected]",             auth = EmailManager.AuthType.OAuth("token"),-            to = "[email protected]",-            subject = "OAuth mail",-            body = "via oauth",+            request = SendRequest(+                to = "[email protected]",+                subject = "OAuth mail",+                body = "via oauth",+            ),         )         assertTrue(server.received.any { it.startsWith("AUTH XOAUTH2") })         assertTrue(server.dataBodies.single().contains("OAuth mail"))
health/src/main/java/com/vayunmathur/health/ui/RecipeEditorPage.kt
diff --git a/health/src/main/java/com/vayunmathur/health/ui/RecipeEditorPage.kt b/health/src/main/java/com/vayunmathur/health/ui/RecipeEditorPage.ktindex bd40e1a7ed..addcd40d88 100644--- a/health/src/main/java/com/vayunmathur/health/ui/RecipeEditorPage.kt+++ b/health/src/main/java/com/vayunmathur/health/ui/RecipeEditorPage.kt@@ -18,6 +18,7 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember+import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import com.vayunmathur.health.data.Ingredient import com.vayunmathur.health.data.ServingUnit@@ -47,7 +48,8 @@ import com.vayunmathur.library.ui.R as UiR @OptIn(ExperimentalMaterial3Api::class) @Composable fun RecipeEditorPage(backStack: NavBackStack<Route>, viewModel: HealthViewModel, recipeId: String? = null) {-    var recipeName by remember { mutableStateOf("") }+    // Saveable so process death restores an in-progress name instead of losing the draft.+    var recipeName by rememberSaveable(recipeId) { mutableStateOf("") }     var recipeIngredients by remember { mutableStateOf(listOf<RecipeIngredientData>()) }     var showSearch by remember { mutableStateOf(false) }     var editingIngredientData by remember { mutableStateOf<RecipeIngredientData?>(null) }@@ -61,7 +63,8 @@ fun RecipeEditorPage(backStack: NavBackStack<Route>, viewModel: HealthViewModel,         if (recipeId != null) {             val loaded = viewModel.loadRecipeForEdit(recipeId)             if (loaded != null) {-                recipeName = loaded.name+                // Don't clobber a name restored by rememberSaveable after process death.+                if (recipeName.isBlank()) recipeName = loaded.name                 recipeIngredients = loaded.ingredients.map { RecipeIngredientData(it.ingredient, it.unit, it.quantity) }             }         }