Modern Apps git

← Modern-Apps.git

securemail: single DataStore instance, single-source onboarded, post-claim biometric

Commit
2466e4cbef3cb04063f759f0844b917632c28191
Author
vayun-mathur <[email protected]>
When
2026-10-24 19:31 UTC (1 month ago)
Parents
b92aa2e

Diff

securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt b/securemail/src/main/java/com/vayunmathur/securemail/Navigation.ktindex 0b61d8eb02..80f7f390d6 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/Navigation.kt@@ -89,7 +89,13 @@ fun Navigation(viewModel: MailViewModel, initialRoute: Route? = null) {             entry<Route.Onboarding> {                 OnboardingScreen(                     state = state,-                    onClaim = viewModel::claimAddress,+                    onClaim = { localPart, passphrase ->+                        viewModel.claimAddress(localPart, passphrase) {+                            // Protection enrolls immediately after claim: run the+                            // biometric gate as soon as the Lock screen lands.+                            (context as? FragmentActivity)?.let { viewModel.unlock(it) }+                        }+                    },                     onDismissMessages = viewModel::dismissMessages,                 )             }
securemail/src/main/java/com/vayunmathur/securemail/data/MailConfig.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/data/MailConfig.kt b/securemail/src/main/java/com/vayunmathur/securemail/data/MailConfig.ktindex dfdbeba265..9a21a6993a 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/data/MailConfig.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/data/MailConfig.kt@@ -20,9 +20,18 @@ data class MailSettings(  private val Context.mailDataStore by preferencesDataStore(name = "securemail_config") -/** DataStore-backed persistence for [MailSettings] (non-secret fields only). */-class MailConfig(private val context: Context) {-    val settings: Flow<MailSettings> = context.mailDataStore.data.map { p ->+/**+ * DataStore-backed persistence for [MailSettings] (non-secret fields only).+ *+ * The DataStore instance is process-wide (held on the application context):+ * a per-[Context] delegate would give the service and the activity SEPARATE+ * DataStore objects on the same file, and the service's copy caches the+ * pre-claim empty state forever ("claimed but offline"). All readers/writers+ * go through [shared] so there is exactly one cache.+ */+class MailConfig(context: Context) {+    private val dataStore = store(context)+    val settings: Flow<MailSettings> = dataStore.data.map { p ->         MailSettings(             address = p[KEY_ADDRESS] ?: "",             publicArmor = p[KEY_PUBLIC] ?: "",@@ -32,7 +41,7 @@ class MailConfig(private val context: Context) {     }      suspend fun setIdentity(address: String, publicArmor: String, fingerprintHex: String) {-        context.mailDataStore.edit {+        dataStore.edit {             it[KEY_ADDRESS] = address             it[KEY_PUBLIC] = publicArmor             it[KEY_FINGERPRINT] = fingerprintHex@@ -40,14 +49,23 @@ class MailConfig(private val context: Context) {     }      suspend fun setLastSync(epochMillis: Long) {-        context.mailDataStore.edit { it[KEY_LAST_SYNC] = epochMillis }+        dataStore.edit { it[KEY_LAST_SYNC] = epochMillis }     }      suspend fun clear() {-        context.mailDataStore.edit { it.clear() }+        dataStore.edit { it.clear() }     }      companion object {+        @Volatile+        private var sharedStore: androidx.datastore.core.DataStore<androidx.datastore.preferences.core.Preferences>? =+            null++        private fun store(app: Context): androidx.datastore.core.DataStore<androidx.datastore.preferences.core.Preferences> =+            sharedStore ?: synchronized(this) {+                sharedStore ?: app.applicationContext.mailDataStore.also { sharedStore = it }+            }+         private val KEY_ADDRESS = stringPreferencesKey("address")         private val KEY_PUBLIC = stringPreferencesKey("public_armor")         private val KEY_FINGERPRINT = stringPreferencesKey("fingerprint_hex")
securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt
diff --git a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.ktindex ec93079198..0ce82c8323 100644--- a/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt+++ b/securemail/src/main/java/com/vayunmathur/securemail/platform/MailViewModel.kt@@ -63,8 +63,12 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {      var state by mutableStateOf(         MailUiState(-            onboarded = keyManager.hasIdentity(),-            locked = keyManager.hasIdentity(),+            // Single source of truth is the DataStore settings flow below+            // (address present = onboarded). Keystore alone must NOT imply+            // onboarded: a present key with no address is a broken state+            // (backup skew) that must land on Onboarding, not Lock.+            onboarded = false,+            locked = false,         ),     )         private set@@ -203,8 +207,16 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {     // Identity: claim + unlock     // ------------------------------------------------------------------ -    /** Generates the PGP identity, claims the address over the socket, persists. */-    fun claimAddress(localPart: String, passphrase: String) {+    /**+     * Generates the PGP identity, claims the address over the socket, persists.+     * On success the caller must run the biometric gate immediately ([onClaimed]+     * โ†’ `unlockIfReady`): protection enrolls at setup, not on next launch.+     */+    fun claimAddress(+        localPart: String,+        passphrase: String,+        onClaimed: () -> Unit = {},+    ) {         val address = AddressValidator.canonicalAddress(localPart)         if (address == null) {             state = state.copy(error = "Use 3โ€“64 lowercase letters, digits, dots or dashes.")@@ -237,10 +249,13 @@ class MailViewModel(app: Application) : AndroidViewModel(app) {             }             state = outcome.fold(                 onSuccess = {+                    // Land LOCKED, not open: Navigation routes to Lock, whose+                    // Unlock button runs the biometric gate immediately โ€” the+                    // user enrolls protection at setup, not next launch.                     state.copy(-                        busy = false, locked = false, onboarded = true,-                        message = "Address $it claimed. Share your public key from Settings.",-                    )+                        busy = false, locked = true, onboarded = true,+                        message = "Address $it claimed. Unlock to protect your key.",+                    ).also { onClaimed() }                 },                 onFailure = {                     Log.status(TAG, "claim failed", it)