communicate: a permission ask can never crash a successful send
- Commit
904bdfa124b4c45215727b0478e4c002708eba36- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-22 06:21 UTC (14 days ago)
- Parents
f71d803
The first-send notification ask launched an ActivityResultLauncher from the send coroutine; after disposal the registry launcher throws 'unregistered ActivityResultLauncher', crashing a send that had already succeeded (seen under load in the test harness). Send success now only sets a state flag; a LaunchedEffect - which can only run while the composition and registration are alive - does the checks and launches, wrapped in runCatching since the ask is best-effort by design. Verified: communicate compiles; Y2 top-level screens 15/15 and conversation screen tests pass.
Diff
communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt b/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.ktindex d5a20da2a7..a06dd55e01 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/ui/ConversationScreen.kt@@ -10,6 +10,7 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.core.content.ContextCompat import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf@@ -73,18 +74,30 @@ fun ConversationScreen( val notificationsLauncher = rememberLauncherForActivityResult( ActivityResultContracts.RequestPermission(), ) {}- val askNotificationsOnce = remember(context) {- {- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- val prefs = context.getSharedPreferences(- "communicate_notifications", Context.MODE_PRIVATE,- )- if (!prefs.getBoolean("has_asked_notifications", false)) {- prefs.edit().putBoolean("has_asked_notifications", true).apply()- if (ContextCompat.checkSelfPermission(- context, Manifest.permission.POST_NOTIFICATIONS,- ) != PackageManager.PERMISSION_GRANTED- ) {+ // The send below only flips this flag; the ask itself runs in a+ // LaunchedEffect (the cast/email/taxi/fooddelivery pattern), so it can+ // only run while this composition — and therefore the launcher's+ // registration — is alive. Launching directly from the send-success+ // coroutine instead could outlive the composition: rememberCoroutineScope+ // cancellation is cooperative, and a send resuming off the main thread+ // raced unregistering the launcher (observed crashing the Y2 screen+ // tests with "unregistered ActivityResultLauncher"). The ask is+ // best-effort, so any residual unregistered state skips it silently+ // rather than crashing a successful send.+ var notificationsAskRequested by remember { mutableStateOf(false) }+ LaunchedEffect(notificationsAskRequested) {+ if (!notificationsAskRequested) return@LaunchedEffect+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {+ val prefs = context.getSharedPreferences(+ "communicate_notifications", Context.MODE_PRIVATE,+ )+ if (!prefs.getBoolean("has_asked_notifications", false)) {+ prefs.edit().putBoolean("has_asked_notifications", true).apply()+ if (ContextCompat.checkSelfPermission(+ context, Manifest.permission.POST_NOTIFICATIONS,+ ) != PackageManager.PERMISSION_GRANTED+ ) {+ runCatching { notificationsLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } }@@ -293,7 +306,7 @@ fun ConversationScreen( participants = if (isGroup) participants else emptyList(), ) if (ok) {- askNotificationsOnce()+ notificationsAskRequested = true // Clear only what was sent; anything the user // typed or attached while sending survives. if (draft == sentDraft) draft = ""