Modern Apps git

← Modern-Apps.git

communicate: route the in-app call screen as Route.Call (#nav)

Commit
ead746c61e9ac004d9124f1bbf6ce86697b1529e
Author
Vayun Mathur <[email protected]>
When
2026-11-16 20:16 UTC (19 days ago)
Parents
04b14e6
The full-screen call UI was drawn by InAppCallOverlay on top of
MainNavigation whenever InAppCallRegistry.phase left Idle. That is
exactly the implicit screen state machine the owner directive bans:
a session state flow decided which major screen was showing, and the
back stack never saw the call.

Add Route.Call (a data object; the live call state changes under it
and is read from the registry inside the screen, so the destination
carries no arguments) and project the phase onto the stack with
CallRouteEffect/syncCallRoute: leaving Idle pushes Route.Call with
FullscreenPage metadata over whatever the user was doing, returning
to Idle pops it and reveals that screen again. A destination pushed
over the call (a deep link) re-tops the existing Call instead of
stacking a second one, and a back pop mid-call is undone by the same
projection, so the call UI cannot be lost while the call continues.

The registry, the line bridges and every phase writer are unchanged.
The phase-to-controls choice inside the call screen stays as it was
(C): Incoming versus ongoing controls are one screen's content, not
navigation. InAppCallScreen.onClose now only clears the terminal
registry state, with the stack pop guarded so the sync pop and the
close cannot pop twice into the screen underneath.

Based on origin/main d0e6a0a75, which already carries the landed
communicate coverage/test-seam work; the existing suite stays green
with it. Tests: RouteSlice6Test now pins eleven route subtypes
including the Call singleton, and CallRouteSyncTest pins push on
leaving Idle, a single call destination across the live phases,
pop-on-Idle revealing the previous screen, Idle no-op, and the
no-burying re-top.

Verified: ./gradlew :communicate:compileDevKotlin
:communicate:testDevUnitTest green (1555 tests).

Diff

communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt b/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.ktindex 17b3927cc8..0bfc81d27c 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/MainActivity.kt@@ -20,6 +20,7 @@ import com.vayunmathur.library.ui.IconHistory import com.vayunmathur.library.ui.IconSms import com.vayunmathur.library.ui.RouteTab import com.vayunmathur.library.ui.TabbedPagerScaffold+import com.vayunmathur.library.util.FullscreenPage import com.vayunmathur.library.util.ListDetailPage import com.vayunmathur.library.util.ListPage import com.vayunmathur.library.util.MainNavigation@@ -93,6 +94,16 @@ sealed interface Route : NavKey {     @Serializable     data class RcsGroupMembers(val conversationId: String) : Route +    /**+     * The full-screen in-app call. Pushed by the call-route sync while+     * [InAppCallRegistry] reports a phase other than Idle, and popped when+     * the call ends: the registry is the session engine, this destination+     * is only its screen. It carries no arguments because the live call+     * state (peer, capabilities, media) changes under it and is read from+     * the registry inside the call screen.+     */+    @Serializable data object Call : Route+     /** One open conversation: its thread, peer address and line. */     @Serializable     data class Conversation(@@ -181,8 +192,8 @@ private fun CommunicateApp(initialDeepLink: DeepLink? = null) {     val backStack = rememberNavBackStack<Route>(Route.Messages)      DeepLinkEffect(backStack, initialDeepLink)+    CallRouteEffect(backStack)     CommunicateTabs(backStack)-    InAppCallOverlay() }  /** Deep links open the conversation they name, including links arriving at a running instance. */@@ -215,12 +226,58 @@ private fun DeepLinkEffect(     } } +/**+ * Keeps [Route.Call] on top of the stack exactly while a call is live.+ *+ * [InAppCallRegistry] stays the session engine: lines bind, ring, connect+ * and end without touching navigation, and this effect projects its phase+ * onto the stack. Leaving Idle pushes the call screen over whatever the+ * user was doing; returning to Idle pops it and reveals that screen again.+ * A back press that pops the call screen mid-call is undone by the same+ * projection (the phase is still live), so the call UI cannot be lost+ * while the call itself continues.+ */+@Composable+private fun CallRouteEffect(backStack: NavBackStack<Route>) {+    val inAppCallState by InAppCallRegistry.state.collectAsState()+    val top = backStack.backStack.lastOrNull()+    LaunchedEffect(inAppCallState.phase, top) {+        syncCallRoute(backStack, inAppCallState.phase)+    }+}++/**+ * The phase-to-stack projection behind [CallRouteEffect], kept free of+ * composition so unit tests can drive it with a plain [NavBackStack].+ *+ * Idempotent: a live phase with the call already on top, and Idle with no+ * call on top, both leave the stack untouched.+ */+internal fun syncCallRoute(backStack: NavBackStack<Route>, phase: InAppCallPhase) {+    val top = backStack.backStack.lastOrNull()+    if (phase != InAppCallPhase.Idle) {+        when {+            top is Route.Call -> Unit+            // A deep link pushed over the call: bring the existing call+            // destination back on top instead of stacking a second one.+            backStack.backStack.any { it is Route.Call } -> backStack.reset(+                *backStack.backStack.filterNot { it is Route.Call }.toTypedArray(),+                Route.Call,+            )+            else -> backStack.add(Route.Call)+        }+    } else if (top is Route.Call) {+        backStack.pop()+    }+}+ @Composable private fun CommunicateNavGraph(backStack: NavBackStack<Route>) {     MainNavigation(backStack) {         coreEntries(backStack)         registrationEntries(backStack)         conversationEntries(backStack)+        callEntries(backStack)     } } @@ -343,13 +400,21 @@ private fun EntryProviderScope<Route>.conversationEntries(backStack: NavBackStac  /**  * One call screen for every in-app line. Google Voice, WhatsApp and Signal all publish into- * InAppCallRegistry, and the screen renders whatever the active line supports.+ * InAppCallRegistry, and the screen renders whatever the active line supports. The phase-to-controls+ * choice stays inside [InAppCallScreen]; only the screen's presence is navigation, synced by+ * [CallRouteEffect]. Closing is the stack pop back to Idle already performs, so the screen's+ * [InAppCallScreen.onClose] only clears the terminal registry state.  */-@Composable-private fun InAppCallOverlay() {-    val inAppCallState by InAppCallRegistry.state.collectAsState()-    if (inAppCallState.phase != InAppCallPhase.Idle) {-        InAppCallScreen(onClose = { InAppCallRegistry.clearEnded() })+private fun EntryProviderScope<Route>.callEntries(backStack: NavBackStack<Route>) {+    entry<Route.Call>(metadata = FullscreenPage()) {+        InAppCallScreen(+            onClose = {+                InAppCallRegistry.clearEnded()+                // If the registry is already Idle the sync pop may have run;+                // only pop while the call destination is actually on top.+                if (backStack.backStack.lastOrNull() is Route.Call) backStack.pop()+            },+        )     } } 
communicate/src/test/java/com/vayunmathur/communicate/CallRouteSyncTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/CallRouteSyncTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/CallRouteSyncTest.ktnew file mode 100644index 0000000000..6c453f9808--- /dev/null+++ b/communicate/src/test/java/com/vayunmathur/communicate/CallRouteSyncTest.kt@@ -0,0 +1,62 @@+package com.vayunmathur.communicate++import com.vayunmathur.communicate.data.call.InAppCallPhase+import com.vayunmathur.library.util.NavBackStack+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * The call screen is a destination, not an overlay: [syncCallRoute]+ * projects the registry's phase onto the stack. Leaving Idle pushes+ * [Route.Call] over whatever was showing; returning to Idle pops it and+ * reveals the screen underneath. The registry/engine is untouched.+ */+@RunWith(RobolectricTestRunner::class)+class CallRouteSyncTest {++    private fun stackOf(vararg routes: Route) = NavBackStack(routes)++    @Test+    fun `leaving idle pushes the call over the current screen`() {+        val stack = stackOf(Route.Main, Route.Accounts)+        syncCallRoute(stack, InAppCallPhase.Incoming)+        assertEquals(listOf(Route.Main, Route.Accounts, Route.Call), stack.backStack)+    }++    @Test+    fun `live phases keep a single call destination on top`() {+        val stack = stackOf(Route.Main)+        syncCallRoute(stack, InAppCallPhase.Outgoing)+        syncCallRoute(stack, InAppCallPhase.Connecting)+        syncCallRoute(stack, InAppCallPhase.Active)+        syncCallRoute(stack, InAppCallPhase.Ended)+        assertEquals(listOf(Route.Main, Route.Call), stack.backStack)+    }++    @Test+    fun `returning to idle pops the call and reveals the previous screen`() {+        val conversation = Route.Conversation(threadId = 7L, address = "+15551234567")+        val stack = stackOf(Route.Main, conversation)+        syncCallRoute(stack, InAppCallPhase.Active)+        syncCallRoute(stack, InAppCallPhase.Idle)+        assertEquals(listOf(Route.Main, conversation), stack.backStack)+    }++    @Test+    fun `idle without a call on top leaves the stack untouched`() {+        val stack = stackOf(Route.Main)+        syncCallRoute(stack, InAppCallPhase.Idle)+        assertEquals(listOf(Route.Main), stack.backStack)+    }++    @Test+    fun `a destination pushed over the call does not bury it`() {+        val stack = stackOf(Route.Main)+        syncCallRoute(stack, InAppCallPhase.Active)+        stack.add(Route.Accounts)+        syncCallRoute(stack, InAppCallPhase.Active)+        assertEquals(listOf(Route.Main, Route.Accounts, Route.Call), stack.backStack)+    }+}
communicate/src/test/java/com/vayunmathur/communicate/RouteSlice6Test.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/RouteSlice6Test.kt b/communicate/src/test/java/com/vayunmathur/communicate/RouteSlice6Test.ktindex 54c8cd6a47..39250c1991 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/RouteSlice6Test.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/RouteSlice6Test.kt@@ -72,6 +72,8 @@ class RouteSlice6Test {         assertTrue(Route.GoogleVoiceSignIn != Route.WhatsAppRegistration)         assertTrue(Route.WhatsAppBackupImport != Route.SignalRegistration)         assertTrue(Route.RcsStatus != Route.Messages)+        assertEquals(Route.Call, Route.Call)+        assertTrue(Route.Call != Route.Messages)     }      @Test@@ -97,8 +99,9 @@ class RouteSlice6Test {             Route.RcsVerify("+15551234567"),             Route.RcsGroupMembers("conv-1"),             Route.Conversation(threadId = 1L, address = "x"),+            Route.Call,         )-        assertEquals(12, routes.size)+        assertEquals(13, routes.size)         routes.forEach { assertIs<com.vayunmathur.library.util.NavKey>(it) }     } @@ -130,7 +133,7 @@ class RouteSlice6Test {         // The back stack persists routes by kotlinx serialization; each subtype must keep         // its @Serializable annotation or restoring navigation state breaks at runtime.         val subtypes = Route::class.sealedSubclasses-        assertEquals(12, subtypes.size)+        assertEquals(13, subtypes.size)         subtypes.forEach { subtype ->             assertTrue(                 subtype.annotations.any { it.annotationClass.qualifiedName == "kotlinx.serialization.Serializable" },@@ -140,7 +143,7 @@ class RouteSlice6Test {     }      @Test-    fun allTwelveRouteSubtypesArePresent() {+    fun allThirteenRouteSubtypesArePresent() {         val names = Route::class.sealedSubclasses.mapNotNull { it.simpleName }.toSet()         assertEquals(             setOf(@@ -156,6 +159,7 @@ class RouteSlice6Test {                 "RcsVerify",                 "RcsGroupMembers",                 "Conversation",+                "Call",             ),             names,         )