Modern Apps git

← Modern-Apps.git

library: themed watch shell, back stack and filled watch buttons

Commit
c6bf91a4e2344e69257a5c886b5dbc8ee1ab77ee
Author
Vayun Mathur <[email protected]>
When
2026-12-05 14:37 UTC (18 hours ago)
Parents
7471d7d
WearShellActivity now hosts every watch screen in a Wear
Scaffold filling the Material theme background (the flat gray
came from screens drawing only a ScalingLazyColumn over the
window background), and holds a real WearBackStack: system back
pops one watch level at a time and only finishes the activity
at the root, instead of exiting from deep navigation.

Add WearBackStack (pure, unit-tested: deep pops, root exits,
navigate-to-existing unwinds), centered filled WearFilledButton
/ red WearDeleteButton and wearOpenOnPhoneItem/wearDeleteItem
helpers in wearlink, and IconMobileArrowRight (Filled.Smartphone)
in icons.tsv for the Open on phone button.

Verified: :library:wearlink:lint :library:wearlink:testDebugUnitTest
:library:ui:compileDebugKotlin and app compiles listed per app.

Diff

library/ui/icons.tsv
diff --git a/library/ui/icons.tsv b/library/ui/icons.tsvindex b816b79a81..06f1d14d63 100644--- a/library/ui/icons.tsv+++ b/library/ui/icons.tsv@@ -340,3 +340,4 @@ IconBookmark	S	Filled.Bookmark	Saved IconBookmarkBorder	S	Filled.BookmarkBorder	Save for later	 IconNewspaper	S	Filled.Newspaper	News	 IconOpenInNew	S	Filled.OpenInNew	Open in browser	+IconMobileArrowRight	S	Filled.Smartphone	Open on phone	Open on phone - hand off to the phone app.
library/wearlink/AGENTS.md
diff --git a/library/wearlink/AGENTS.md b/library/wearlink/AGENTS.mdindex f60fad2d93..3ab6caf1ff 100644--- a/library/wearlink/AGENTS.md+++ b/library/wearlink/AGENTS.md@@ -10,9 +10,16 @@   except the Watch apps) - Wear entry shell: `WearShellActivity.kt` โ€” every wear companion's main   activity extends `WearShellActivity<route, store>` (`createStore()`-  warm-up, Wear Material theme + route state held in the shell; no-  permission request โ€” the Watch conduit owns Bluetooth); each wear-  module keeps its concrete activity name, route type and screen `when`.+  warm-up, Wear Material theme + `Scaffold` host filling the theme+  background, and a `WearBackStack` route stack held in the shell:+  system back pops one watch level at a time and only finishes the+  activity at the root; no permission request โ€” the Watch conduit owns+  Bluetooth); each wear module keeps its concrete activity name, route+  type and screen `when`. Shared watch action buttons live in+  `WearActionButtons.kt` (centered filled `WearFilledButton`, red+  `WearDeleteButton`, and the `wearOpenOnPhoneItem`/`wearDeleteItem`+  list helpers; the open-on-phone conduit path suffix is+  `WEAR_OPEN_ON_PHONE_PATH_SUFFIX`).  ## Purpose First-party replacement for the proprietary Play-services Wearable Data
library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearActionButtons.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearActionButtons.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearActionButtons.ktnew file mode 100644index 0000000000..f9fef1468b--- /dev/null+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearActionButtons.kt@@ -0,0 +1,106 @@+package com.vayunmathur.library.wearlink++import androidx.compose.foundation.layout.Arrangement+import androidx.compose.foundation.layout.Row+import androidx.compose.foundation.layout.Spacer+import androidx.compose.foundation.layout.fillMaxWidth+import androidx.compose.foundation.layout.height+import androidx.compose.foundation.layout.padding+import androidx.compose.foundation.layout.width+import androidx.compose.runtime.Composable+import androidx.compose.ui.Alignment+import androidx.compose.ui.Modifier+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.text.style.TextAlign+import androidx.compose.ui.unit.dp+import androidx.wear.compose.foundation.lazy.ScalingLazyListScope+import androidx.wear.compose.material.Button+import androidx.wear.compose.material.ButtonDefaults+import androidx.wear.compose.material.MaterialTheme+import androidx.wear.compose.material.Text++/**+ * Centered filled action button for watch screens. Every tappable action+ * on a watch screen that is a button (not a list/nav chip or a toggle)+ * goes through this: filled container, centered icon+label, watch-width.+ * The leading [icon] is supplied by the caller from `library/ui`+ * (`IconMobileArrowRight()` for open-on-phone, `IconDelete()` for delete)+ * so this transport library stays free of the phone icon set.+ */+@Composable+fun WearFilledButton(+    label: String,+    onClick: () -> Unit,+    modifier: Modifier = Modifier,+    icon: (@Composable () -> Unit)? = null,+    containerColor: Color = MaterialTheme.colors.primary,+    contentColor: Color = MaterialTheme.colors.onPrimary,+) {+    Button(+        onClick = onClick,+        colors = ButtonDefaults.buttonColors(+            backgroundColor = containerColor,+            contentColor = contentColor,+        ),+        modifier = modifier.fillMaxWidth().padding(horizontal = 8.dp).height(48.dp),+    ) {+        Row(+            horizontalArrangement = Arrangement.Center,+            verticalAlignment = Alignment.CenterVertically,+            modifier = Modifier.fillMaxWidth(),+        ) {+            if (icon != null) {+                icon()+                Spacer(Modifier.width(8.dp))+            }+            Text(text = label, maxLines = 1, textAlign = TextAlign.Center)+        }+    }+}++/** Red filled delete button: leading delete icon, centered, below open-on-phone. */+@Composable+fun WearDeleteButton(+    label: String,+    onClick: () -> Unit,+    modifier: Modifier = Modifier,+    icon: (@Composable () -> Unit)? = null,+) {+    WearFilledButton(+        label = label,+        onClick = onClick,+        modifier = modifier,+        icon = icon,+        containerColor = MaterialTheme.colors.error,+        contentColor = Color.White,+    )+}++/** Bottom "Open on phone" filled button in a [ScalingLazyListScope]. */+fun ScalingLazyListScope.wearOpenOnPhoneItem(+    onClick: () -> Unit,+    icon: (@Composable () -> Unit)? = null,+    label: String = "Open on phone",+) {+    item {+        WearFilledButton(label = label, onClick = onClick, icon = icon)+    }+}++/** Bottom red delete filled button in a [ScalingLazyListScope], below open-on-phone. */+fun ScalingLazyListScope.wearDeleteItem(+    label: String,+    onClick: () -> Unit,+    icon: (@Composable () -> Unit)? = null,+) {+    item {+        WearDeleteButton(label = label, onClick = onClick, icon = icon)+    }+}++/**+ * Conduit path every merged app uses for the open-on-phone handoff.+ * The phone counterpart opens its main activity (deep-linking to+ * [entityId] when it understands it). Body is the entity id, or empty.+ */+const val WEAR_OPEN_ON_PHONE_PATH_SUFFIX: String = "/open-on-phone"
library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearBackStack.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearBackStack.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearBackStack.ktnew file mode 100644index 0000000000..ddbcff9902--- /dev/null+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearBackStack.kt@@ -0,0 +1,32 @@+package com.vayunmathur.library.wearlink++/**+ * Pure watch navigation back stack: [navigate] pushes a new route, or+ * pops back to it when it is already in the stack (so an explicit+ * "go home/list" navigation unwinds instead of duplicating the root).+ * [pop] removes one level; it returns false at the root, where the+ * host should let the system finish the activity.+ */+class WearBackStack<T>(initial: T) {+    private val stack = mutableListOf(initial)++    val current: T get() = stack.last()+    val depth: Int get() = stack.size+    val entries: List<T> get() = stack.toList()++    fun navigate(route: T) {+        val existing = stack.indexOf(route)+        if (existing >= 0) {+            while (stack.size > existing + 1) stack.removeAt(stack.lastIndex)+        } else {+            stack.add(route)+        }+    }++    /** Pop one level. False at the root: caller exits the app. */+    fun pop(): Boolean {+        if (stack.size <= 1) return false+        stack.removeAt(stack.lastIndex)+        return true+    }+}
library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.ktindex a1109267cb..5e22050158 100644--- a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.kt+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.kt@@ -8,7 +8,13 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue+import androidx.compose.foundation.background+import androidx.compose.foundation.layout.Box+import androidx.compose.foundation.layout.fillMaxSize+import androidx.compose.ui.Modifier import androidx.wear.compose.material.MaterialTheme+import androidx.activity.compose.BackHandler+import androidx.wear.compose.material.Scaffold  /**  * Shared shell for every wear companion's main activity: create the@@ -30,8 +36,28 @@ abstract class WearShellActivity<R, S : Any>(private val initialRoute: R) : Comp         store = createStore()         setContent {             MaterialTheme {-                var route by remember { mutableStateOf(initialRoute) }-                Screen(route) { route = it }+                // Themed host: Wear Scaffold + theme background fill.+                // Without this the window's flat gray shows through+                // screens that only draw a ScalingLazyColumn.+                Scaffold {+                    Box(+                        modifier = Modifier.fillMaxSize()+                            .background(MaterialTheme.colors.background),+                    ) {+                        // Real back stack: system back pops one watch+                        // level at a time; only at the root does it+                        // fall through and finish the activity.+                        val backStack = remember { WearBackStack(initialRoute) }+                        var route by remember { mutableStateOf(backStack.current) }+                        BackHandler(enabled = backStack.depth > 1) {+                            if (backStack.pop()) route = backStack.current+                        }+                        Screen(route) { next ->+                            backStack.navigate(next)+                            route = backStack.current+                        }+                    }+                }             }         }     }
library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearBackStackTest.kt
diff --git a/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearBackStackTest.kt b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearBackStackTest.ktnew file mode 100644index 0000000000..c7399d627e--- /dev/null+++ b/library/wearlink/src/test/java/com/vayunmathur/library/wearlink/WearBackStackTest.kt@@ -0,0 +1,48 @@+package com.vayunmathur.library.wearlink++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++class WearBackStackTest {++    @Test+    fun deepStackPopsOneLevelAtATime() {+        val stack = WearBackStack("home")+        stack.navigate("list")+        stack.navigate("detail")+        stack.navigate("sub")+        assertEquals("sub", stack.current)+        assertTrue(stack.pop()); assertEquals("detail", stack.current)+        assertTrue(stack.pop()); assertEquals("list", stack.current)+        assertTrue(stack.pop()); assertEquals("home", stack.current)+    }++    @Test+    fun backAtRootExits() {+        val stack = WearBackStack("home")+        assertFalse(stack.pop())+        assertEquals("home", stack.current)+        assertEquals(1, stack.depth)+    }++    @Test+    fun navigateToExistingRouteUnwinds() {+        val stack = WearBackStack("home")+        stack.navigate("list")+        stack.navigate("detail")+        stack.navigate("home")+        assertEquals(listOf("home"), stack.entries)+        assertFalse(stack.pop())+    }++    @Test+    fun repeatedBackPopsToRootThenExits() {+        val stack = WearBackStack("home")+        stack.navigate("a"); stack.navigate("b"); stack.navigate("c")+        repeat(3) { assertTrue(stack.pop()) }+        assertEquals("home", stack.current)+        assertFalse(stack.pop())+    }+}