wearlink: shared wear activity shell for all wear companions
- Commit
97d585bc8c8d4685054b7eea29810bd0df6bfe7c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 18:57 UTC (4 days ago)
- Parents
2881792
The 13 wear main activities repeated the same shell: WearLink permission request, store/bridge warm-up, setContent inside the Wear Material theme, and remember/mutableState route state around a per-app when. That shell now lives once in library/wearlink/WearShellActivity.kt (44 lines): the base owns the permission request, createStore() warm-up, theme and route state, and each concrete activity keeps its manifest name, its route type and its screen when, supplying only its store factory expression. appstore and contacts-wear have no wear route, so they use Unit and keep their onResume behaviour; clock keeps its ring-channel setup inside its store factory. Behaviour is identical: same stores created, same bridge get and publish calls, same screens and navigation callbacks, same theme. contacts-wear's first-launch snapshot request now launches just before composition instead of just after; the coroutine is async and only reads the store snapshot, so nothing observable moves. Counted lines (tools/structure_check.py lines): repo total 1,649,659 -> 1,649,605 (-54). Activities 765 -> 667 lines plus the 44-line shared shell. Verified: :library:wearlink and all 13 wear modules' testDebugUnitTest and lint green; structure lines passes (the exceptions ratchet was already stale on communicate before this change and is untouched).
Diff
appstore/wear/src/main/java/com/vayunmathur/appstore/wear/ui/WearMainActivity.kt
diff --git a/appstore/wear/src/main/java/com/vayunmathur/appstore/wear/ui/WearMainActivity.kt b/appstore/wear/src/main/java/com/vayunmathur/appstore/wear/ui/WearMainActivity.ktindex ac8c9bdd77..539cae55fb 100644--- a/appstore/wear/src/main/java/com/vayunmathur/appstore/wear/ui/WearMainActivity.kt+++ b/appstore/wear/src/main/java/com/vayunmathur/appstore/wear/ui/WearMainActivity.kt@@ -1,8 +1,5 @@ package com.vayunmathur.appstore.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable@@ -23,6 +20,7 @@ import com.vayunmathur.appstore.wear.R import com.vayunmathur.appstore.wear.data.WearStore import com.vayunmathur.library.wearlink.appstore.WatchAppPhase import com.vayunmathur.appstore.wear.platform.WearInstaller+import com.vayunmathur.library.wearlink.WearShellActivity /** * The whole watch store: what the phone offered, what is already downloaded@@ -32,19 +30,16 @@ import com.vayunmathur.appstore.wear.platform.WearInstaller * watch's own persisted state, so it works standalone (phone absent, app * cold-started) with whatever has already been downloaded. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<Unit, WearStore>(Unit) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearStore.get(this)+ override fun createStore() = WearStore.get(this).also { com.vayunmathur.appstore.wear.sync.WearLinkBridge.get(this)- store.publishWatchState()- setContent {- MaterialTheme {- WearStoreScreen(store)- }- }+ it.publishWatchState()+ }++ @Composable+ override fun Screen(route: Unit, navigate: (Unit) -> Unit) {+ WearStoreScreen(store) } override fun onResume() {
calendar-wear/src/main/java/com/vayunmathur/calendar/wear/ui/WearMainActivity.kt
diff --git a/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/ui/WearMainActivity.kt b/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/ui/WearMainActivity.ktindex 73c38b49b3..f8e0f3bfe2 100644--- a/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/ui/WearMainActivity.kt+++ b/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/ui/WearMainActivity.kt@@ -1,45 +1,35 @@ package com.vayunmathur.calendar.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable import com.vayunmathur.calendar.wear.data.WearCalendarStore+import com.vayunmathur.library.wearlink.WearShellActivity /** * Watch calendar entry: agenda list and event detail, rendered only from the * locally stored snapshot so it works standalone (phone absent, cold start). */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearCalendarStore>(WearRoute.Agenda) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearCalendarStore.get(this)+ override fun createStore() = WearCalendarStore.get(this).also { com.vayunmathur.calendar.wear.sync.WearLinkBridge.get(this)- store.publishWatchState()- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Agenda) }- when (val current = route) {- WearRoute.Agenda -> WearAgendaScreen(- store = store,- onOpenEvent = { event ->- route = WearRoute.Event(event.eventId, event.startMillis)- },- )- is WearRoute.Event -> WearEventScreen(- store = store,- eventId = current.eventId,- startMillis = current.startMillis,- onBack = { route = WearRoute.Agenda },- )- }- }+ it.publishWatchState()+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.Agenda -> WearAgendaScreen(+ store = store,+ onOpenEvent = { event ->+ navigate(WearRoute.Event(event.eventId, event.startMillis))+ },+ )+ is WearRoute.Event -> WearEventScreen(+ store = store,+ eventId = current.eventId,+ startMillis = current.startMillis,+ onBack = { navigate(WearRoute.Agenda) },+ ) } } }
camera-wear/src/main/java/com/vayunmathur/camera/wear/ui/WearMainActivity.kt
diff --git a/camera-wear/src/main/java/com/vayunmathur/camera/wear/ui/WearMainActivity.kt b/camera-wear/src/main/java/com/vayunmathur/camera/wear/ui/WearMainActivity.ktindex 74ce79ff9b..ee65498d84 100644--- a/camera-wear/src/main/java/com/vayunmathur/camera/wear/ui/WearMainActivity.kt+++ b/camera-wear/src/main/java/com/vayunmathur/camera/wear/ui/WearMainActivity.kt@@ -1,47 +1,35 @@ package com.vayunmathur.camera.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable import com.vayunmathur.camera.wear.data.WearCameraStore import com.vayunmathur.camera.wear.sync.WearLinkBridge+import com.vayunmathur.library.wearlink.WearShellActivity /** * Camera watch companion entry: a live viewfinder streamed from the phone * camera plus remote controls executed on the phone. The watch has no * camera of its own โ with no phone nearby it says so, honestly. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearCameraStore>(WearRoute.Viewfinder) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearCameraStore.get()- WearLinkBridge.get(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Viewfinder) }- when (route) {- WearRoute.Viewfinder -> WearViewfinderScreen(- store = store,- onOpenControls = { route = WearRoute.Controls },- onOpenLastCapture = { route = WearRoute.LastCapture },- )- WearRoute.Controls -> WearControlsScreen(- store = store,- onBack = { route = WearRoute.Viewfinder },- )- WearRoute.LastCapture -> WearLastCaptureScreen(- store = store,- onBack = { route = WearRoute.Viewfinder },- )- }- }+ override fun createStore() = WearCameraStore.get().also { WearLinkBridge.get(this) }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (route) {+ WearRoute.Viewfinder -> WearViewfinderScreen(+ store = store,+ onOpenControls = { navigate(WearRoute.Controls) },+ onOpenLastCapture = { navigate(WearRoute.LastCapture) },+ )+ WearRoute.Controls -> WearControlsScreen(+ store = store,+ onBack = { navigate(WearRoute.Viewfinder) },+ )+ WearRoute.LastCapture -> WearLastCaptureScreen(+ store = store,+ onBack = { navigate(WearRoute.Viewfinder) },+ ) } } }
clock-wear/src/main/java/com/vayunmathur/clock/wear/ui/WearMainActivity.kt
diff --git a/clock-wear/src/main/java/com/vayunmathur/clock/wear/ui/WearMainActivity.kt b/clock-wear/src/main/java/com/vayunmathur/clock/wear/ui/WearMainActivity.ktindex 6b3350aadb..c8261afe48 100644--- a/clock-wear/src/main/java/com/vayunmathur/clock/wear/ui/WearMainActivity.kt+++ b/clock-wear/src/main/java/com/vayunmathur/clock/wear/ui/WearMainActivity.kt@@ -1,59 +1,49 @@ package com.vayunmathur.clock.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable import com.vayunmathur.clock.wear.data.WearClockStore import com.vayunmathur.clock.wear.platform.WatchRingNotifier import com.vayunmathur.clock.wear.sync.WearLinkBridge+import com.vayunmathur.library.wearlink.WearShellActivity /** * Watch clock entry: home menu over world clocks (synced), alarms (synced, * watch-armed), timers and stopwatch (watch-native). Everything renders * from the locally persisted store so it works with the phone absent. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearClockStore>(WearRoute.Home) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)+ override fun createStore() = WearClockStore.get(this).also { WatchRingNotifier.ensureChannel(this)- val store = WearClockStore.get(this) WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Home) }- when (val current = route) {- WearRoute.Home -> WearHomeScreen(- onOpen = { route = it },- )- WearRoute.World -> WearWorldClockScreen(- store = store, onBack = { route = WearRoute.Home },- )- WearRoute.Alarms -> WearAlarmsScreen(- store = store,- onBack = { route = WearRoute.Home },- onOpenAlarm = { route = WearRoute.AlarmDetail(it) },- )- is WearRoute.AlarmDetail -> WearAlarmDetailScreen(- store = store,- alarmId = current.alarmId,- onBack = { route = WearRoute.Alarms },- )- WearRoute.Timers -> WearTimersScreen(- store = store, onBack = { route = WearRoute.Home },- )- WearRoute.Stopwatch -> WearStopwatchScreen(- store = store, onBack = { route = WearRoute.Home },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.Home -> WearHomeScreen(+ onOpen = { navigate(it) },+ )+ WearRoute.World -> WearWorldClockScreen(+ store = store, onBack = { navigate(WearRoute.Home) },+ )+ WearRoute.Alarms -> WearAlarmsScreen(+ store = store,+ onBack = { navigate(WearRoute.Home) },+ onOpenAlarm = { navigate(WearRoute.AlarmDetail(it)) },+ )+ is WearRoute.AlarmDetail -> WearAlarmDetailScreen(+ store = store,+ alarmId = current.alarmId,+ onBack = { navigate(WearRoute.Alarms) },+ )+ WearRoute.Timers -> WearTimersScreen(+ store = store, onBack = { navigate(WearRoute.Home) },+ )+ WearRoute.Stopwatch -> WearStopwatchScreen(+ store = store, onBack = { navigate(WearRoute.Home) },+ ) } } }
communicate-wear/src/main/java/com/vayunmathur/communicate/wear/ui/WearMainActivity.kt
diff --git a/communicate-wear/src/main/java/com/vayunmathur/communicate/wear/ui/WearMainActivity.kt b/communicate-wear/src/main/java/com/vayunmathur/communicate/wear/ui/WearMainActivity.ktindex 43495d839f..5161fc2014 100644--- a/communicate-wear/src/main/java/com/vayunmathur/communicate/wear/ui/WearMainActivity.kt+++ b/communicate-wear/src/main/java/com/vayunmathur/communicate/wear/ui/WearMainActivity.kt@@ -1,16 +1,11 @@ package com.vayunmathur.communicate.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent+import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme import com.vayunmathur.communicate.wear.data.WearCommunicateStore import com.vayunmathur.communicate.wear.sync.WearLinkBridge+import com.vayunmathur.library.wearlink.WearShellActivity /** * Watch Communicate entry: mirrors the phone's pages (conversation list,@@ -20,58 +15,54 @@ import com.vayunmathur.communicate.wear.sync.WearLinkBridge * An incoming-call alert overlays any route with Decline only โ there is * no answer-on-watch because watch audio routing is not guaranteed. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearCommunicateStore>(WearRoute.ConversationList) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearCommunicateStore.get(this)+ override fun createStore() = WearCommunicateStore.get(this).also { WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.ConversationList) }- val incoming by store.incomingCall.collectAsState()- val call = incoming- if (call != null) {- WearIncomingCallScreen(- store = store,- call = call,- onDone = { store.setIncomingCall(null) },- )- } else {- when (val current = route) {- WearRoute.ConversationList -> WearConversationListScreen(- store = store,- onOpenThread = { route = WearRoute.Thread(it) },- onOpenCalls = { route = WearRoute.CallLog },- onOpenVoicemail = { route = WearRoute.Voicemail },- onOpenPhoneOnly = { route = WearRoute.PhoneOnly },- )- is WearRoute.Thread -> WearThreadScreen(- store = store,- threadKey = current.threadKey,- onReply = { route = WearRoute.Reply(it) },- onBack = { route = WearRoute.ConversationList },- )- is WearRoute.Reply -> WearReplyScreen(- store = store,- threadKey = current.threadKey,- onDone = { route = WearRoute.Thread(current.threadKey) },- )- WearRoute.CallLog -> WearCallLogScreen(- store = store,- onBack = { route = WearRoute.ConversationList },- )- WearRoute.Voicemail -> WearVoicemailScreen(- store = store,- onBack = { route = WearRoute.ConversationList },- )- WearRoute.PhoneOnly -> WearPhoneOnlyScreen(- onBack = { route = WearRoute.ConversationList },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ val incoming by store.incomingCall.collectAsState()+ val call = incoming+ if (call != null) {+ WearIncomingCallScreen(+ store = store,+ call = call,+ onDone = { store.setIncomingCall(null) },+ )+ } else {+ when (val current = route) {+ WearRoute.ConversationList -> WearConversationListScreen(+ store = store,+ onOpenThread = { navigate(WearRoute.Thread(it)) },+ onOpenCalls = { navigate(WearRoute.CallLog) },+ onOpenVoicemail = { navigate(WearRoute.Voicemail) },+ onOpenPhoneOnly = { navigate(WearRoute.PhoneOnly) },+ )+ is WearRoute.Thread -> WearThreadScreen(+ store = store,+ threadKey = current.threadKey,+ onReply = { navigate(WearRoute.Reply(it)) },+ onBack = { navigate(WearRoute.ConversationList) },+ )+ is WearRoute.Reply -> WearReplyScreen(+ store = store,+ threadKey = current.threadKey,+ onDone = { navigate(WearRoute.Thread(current.threadKey)) },+ )+ WearRoute.CallLog -> WearCallLogScreen(+ store = store,+ onBack = { navigate(WearRoute.ConversationList) },+ )+ WearRoute.Voicemail -> WearVoicemailScreen(+ store = store,+ onBack = { navigate(WearRoute.ConversationList) },+ )+ WearRoute.PhoneOnly -> WearPhoneOnlyScreen(+ onBack = { navigate(WearRoute.ConversationList) },+ ) } } }
contacts-wear/src/main/java/com/vayunmathur/contacts/wear/MainActivity.kt
diff --git a/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/MainActivity.kt b/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/MainActivity.ktindex df6ec83fc8..049ac409b5 100644--- a/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/MainActivity.kt+++ b/contacts-wear/src/main/java/com/vayunmathur/contacts/wear/MainActivity.kt@@ -1,8 +1,6 @@ package com.vayunmathur.contacts.wear -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent+import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf@@ -13,33 +11,34 @@ import com.vayunmathur.contacts.wear.platform.WearDeviceCapabilities import com.vayunmathur.contacts.wear.platform.WearDeviceCapabilitiesProbe import com.vayunmathur.contacts.wear.sync.WearWatchStatePublisher import com.vayunmathur.contacts.wear.ui.WearContactsApp+import com.vayunmathur.library.wearlink.WearShellActivity import kotlinx.coroutines.launch /** * Wear Contacts entry: renders the locally synced copy (offline-capable) and, * on an empty first launch, asks the phone to republish its snapshot. */-class MainActivity : ComponentActivity() {+class MainActivity : WearShellActivity<Unit, WearContactsStore>(Unit) { - private lateinit var store: WearContactsStore private var capabilities by mutableStateOf(WearDeviceCapabilities()) - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- store = com.vayunmathur.contacts.wear.sync.WearLinkBridge.store(applicationContext)+ override fun createStore(): WearContactsStore {+ val created = com.vayunmathur.contacts.wear.sync.WearLinkBridge.store(applicationContext) com.vayunmathur.contacts.wear.sync.WearLinkBridge.get(applicationContext) capabilities = WearDeviceCapabilitiesProbe.probeHandlers(this)- setContent {- val snapshot by store.snapshot.collectAsState()- WearContactsApp(snapshot, capabilities)- } lifecycleScope.launch { capabilities = WearDeviceCapabilitiesProbe.probe(this@MainActivity)- if (store.snapshot.value.contacts.isEmpty()) {+ if (created.snapshot.value.contacts.isEmpty()) { WearWatchStatePublisher.requestSnapshot(applicationContext) } }+ return created+ }++ @Composable+ override fun Screen(route: Unit, navigate: (Unit) -> Unit) {+ val snapshot by store.snapshot.collectAsState()+ WearContactsApp(snapshot, capabilities) } override fun onResume() {
findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/ui/WearMainActivity.kt
diff --git a/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/ui/WearMainActivity.kt b/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/ui/WearMainActivity.ktindex f6c8859b6c..43bbdfa228 100644--- a/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/ui/WearMainActivity.kt+++ b/findfamily-wear/src/main/java/com/vayunmathur/findfamily/wear/ui/WearMainActivity.kt@@ -1,50 +1,40 @@ package com.vayunmathur.findfamily.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable import com.vayunmathur.findfamily.wear.data.WearFindFamilyStore import com.vayunmathur.findfamily.wear.sync.WearLinkBridge+import com.vayunmathur.library.wearlink.WearShellActivity /** * Watch FindFamily entry: people home, per-person detail and the * all-people relative map, rendered only from the locally stored * snapshot so it works standalone (phone absent, cold start). */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearFindFamilyStore>(WearRoute.People) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearFindFamilyStore.get(this)+ override fun createStore() = WearFindFamilyStore.get(this).also { WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.People) }- when (val current = route) {- WearRoute.People -> WearPeopleScreen(- store = store,- onOpenPerson = { route = WearRoute.Person(it) },- onOpenMap = { route = WearRoute.AllMap },- )- is WearRoute.Person -> WearPersonScreen(- store = store,- personId = current.personId,- onBack = { route = WearRoute.People },- )- WearRoute.AllMap -> WearAllMapScreen(- store = store,- onOpenPerson = { route = WearRoute.Person(it) },- onBack = { route = WearRoute.People },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.People -> WearPeopleScreen(+ store = store,+ onOpenPerson = { navigate(WearRoute.Person(it)) },+ onOpenMap = { navigate(WearRoute.AllMap) },+ )+ is WearRoute.Person -> WearPersonScreen(+ store = store,+ personId = current.personId,+ onBack = { navigate(WearRoute.People) },+ )+ WearRoute.AllMap -> WearAllMapScreen(+ store = store,+ onOpenPerson = { navigate(WearRoute.Person(it)) },+ onBack = { navigate(WearRoute.People) },+ ) } } }
launcher-wear/src/main/java/com/vayunmathur/launcher/wear/ui/WearMainActivity.kt
diff --git a/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/ui/WearMainActivity.kt b/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/ui/WearMainActivity.ktindex 0103131f84..5ace26a862 100644--- a/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/ui/WearMainActivity.kt+++ b/launcher-wear/src/main/java/com/vayunmathur/launcher/wear/ui/WearMainActivity.kt@@ -1,45 +1,36 @@ package com.vayunmathur.launcher.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable import com.vayunmathur.launcher.wear.data.WearLauncherStore+import com.vayunmathur.library.wearlink.WearShellActivity /** * Watch launcher entry. This is a launcher-style app drawer, not the system * home: stock Wear OS reserves home for the system launcher (see manifest). * It renders from installed apps + the last synced favorites, fully offline. */-class WearMainActivity : ComponentActivity() {- override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearLauncherStore.get(this)+class WearMainActivity : WearShellActivity<WearRoute, WearLauncherStore>(WearRoute.Home) {++ override fun createStore() = WearLauncherStore.get(this).also { com.vayunmathur.launcher.wear.sync.WearLinkBridge.get(this)- store.publishWatchState()- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Home) }- when (route) {- WearRoute.Home -> WearLauncherScreen(- store = store,- onOpenAllApps = { route = WearRoute.AllApps },- onOpenSettings = { route = WearRoute.Settings },- )- WearRoute.AllApps -> WearAllAppsScreen(- store = store,- onBack = { route = WearRoute.Home },- )- WearRoute.Settings -> WearSettingsScreen(- onBack = { route = WearRoute.Home },- )- }- }+ it.publishWatchState()+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (route) {+ WearRoute.Home -> WearLauncherScreen(+ store = store,+ onOpenAllApps = { navigate(WearRoute.AllApps) },+ onOpenSettings = { navigate(WearRoute.Settings) },+ )+ WearRoute.AllApps -> WearAllAppsScreen(+ store = store,+ onBack = { navigate(WearRoute.Home) },+ )+ WearRoute.Settings -> WearSettingsScreen(+ onBack = { navigate(WearRoute.Home) },+ ) } } }
library/wearlink/AGENTS.md
diff --git a/library/wearlink/AGENTS.md b/library/wearlink/AGENTS.mdindex 8f10cd4b8f..f8a9b63c04 100644--- a/library/wearlink/AGENTS.md+++ b/library/wearlink/AGENTS.md@@ -6,6 +6,11 @@ - Package: `com.vayunmathur.library.wearlink` for the transport, plus one per-app protocol subpackage (e.g. `com.vayunmathur.library.wearlink.clock`) - Entry: `WearLinkNode.kt` (Android), `WearLinkProtocol.kt` (pure wire)+- Wear entry shell: `WearShellActivity.kt` โ every wear companion's main+ activity extends `WearShellActivity<route, store>` (permission request,+ `createStore()` warm-up, Wear Material theme + route state held in the+ shell); each wear module keeps its concrete activity name, route type+ and screen `when`. ## Purpose First-party replacement for the proprietary Play-services Wearable Data
library/wearlink/build.gradle.kts
diff --git a/library/wearlink/build.gradle.kts b/library/wearlink/build.gradle.ktsindex f017af81a6..e8de9ed44d 100644--- a/library/wearlink/build.gradle.kts+++ b/library/wearlink/build.gradle.kts@@ -7,6 +7,8 @@ android { } dependencies {+ implementation(libs.androidx.activity.compose)+ implementation(libs.androidx.wear.compose.material) implementation(libs.kotlinx.coroutines.android) testImplementation(libs.kotlin.test) testImplementation(libs.kotlin.test.junit)
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.ktnew file mode 100644index 0000000000..476daac3e7--- /dev/null+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/WearShellActivity.kt@@ -0,0 +1,44 @@+package com.vayunmathur.library.wearlink++import android.os.Bundle+import androidx.activity.ComponentActivity+import androidx.activity.compose.setContent+import androidx.compose.runtime.Composable+import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateOf+import androidx.compose.runtime.remember+import androidx.compose.runtime.setValue+import androidx.wear.compose.material.MaterialTheme++/**+ * Shared shell for every wear companion's main activity: request the+ * WearLink Bluetooth permission, create the app's store (whose factory+ * also gets/bridges/publishes as that app's warm-up), then compose the+ * app's route screen inside the Wear Material theme with the route state+ * held here. Concrete activities keep their manifest names, their own+ * route type and their own `when` over it; apps with no route or store+ * of their own use [Unit].+ */+abstract class WearShellActivity<R, S : Any>(private val initialRoute: R) : ComponentActivity() {++ protected lateinit var store: S+ private set++ override fun onCreate(savedInstanceState: Bundle?) {+ super.onCreate(savedInstanceState)+ WearLinkPermissions.requestIfNeeded(this)+ store = createStore()+ setContent {+ MaterialTheme {+ var route by remember { mutableStateOf(initialRoute) }+ Screen(route) { route = it }+ }+ }+ }++ /** The store/bridge warm-up that used to sit between the permission request and setContent. */+ protected abstract fun createStore(): S++ @Composable+ protected abstract fun Screen(route: R, navigate: (R) -> Unit)+}
maps-wear/src/main/java/com/vayunmathur/maps/wear/ui/WearMainActivity.kt
diff --git a/maps-wear/src/main/java/com/vayunmathur/maps/wear/ui/WearMainActivity.kt b/maps-wear/src/main/java/com/vayunmathur/maps/wear/ui/WearMainActivity.ktindex fd65be5883..2b1fa86749 100644--- a/maps-wear/src/main/java/com/vayunmathur/maps/wear/ui/WearMainActivity.kt+++ b/maps-wear/src/main/java/com/vayunmathur/maps/wear/ui/WearMainActivity.kt@@ -1,13 +1,7 @@ package com.vayunmathur.maps.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable+import com.vayunmathur.library.wearlink.WearShellActivity import com.vayunmathur.maps.wear.data.WearMapsStore import com.vayunmathur.maps.wear.sync.WearLinkBridge @@ -16,33 +10,29 @@ import com.vayunmathur.maps.wear.sync.WearLinkBridge * all from the locally stored snapshot so they work standalone (phone * absent, cold start) with stale/offline honesty. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearMapsStore>(WearRoute.Map) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearMapsStore.get(this)+ override fun createStore() = WearMapsStore.get(this).also { WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Map) }- when (route) {- WearRoute.Map -> WearMapScreen(- store = store,- onOpenRoute = { route = WearRoute.RouteDetail },- onOpenSaved = { route = WearRoute.Saved },- )- WearRoute.RouteDetail -> WearRouteDetailScreen(- store = store,- onBack = { route = WearRoute.Map },- )- WearRoute.Saved -> WearSavedScreen(- store = store,- onBack = { route = WearRoute.Map },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (route) {+ WearRoute.Map -> WearMapScreen(+ store = store,+ onOpenRoute = { navigate(WearRoute.RouteDetail) },+ onOpenSaved = { navigate(WearRoute.Saved) },+ )+ WearRoute.RouteDetail -> WearRouteDetailScreen(+ store = store,+ onBack = { navigate(WearRoute.Map) },+ )+ WearRoute.Saved -> WearSavedScreen(+ store = store,+ onBack = { navigate(WearRoute.Map) },+ ) } } }
music-wear/src/main/java/com/vayunmathur/music/wear/ui/WearMainActivity.kt
diff --git a/music-wear/src/main/java/com/vayunmathur/music/wear/ui/WearMainActivity.kt b/music-wear/src/main/java/com/vayunmathur/music/wear/ui/WearMainActivity.ktindex 97ce549c29..ac24d5a964 100644--- a/music-wear/src/main/java/com/vayunmathur/music/wear/ui/WearMainActivity.kt+++ b/music-wear/src/main/java/com/vayunmathur/music/wear/ui/WearMainActivity.kt@@ -1,13 +1,7 @@ package com.vayunmathur.music.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable+import com.vayunmathur.library.wearlink.WearShellActivity import com.vayunmathur.music.wear.data.WearMusicStore import com.vayunmathur.music.wear.sync.WearLinkBridge @@ -17,19 +11,15 @@ import com.vayunmathur.music.wear.sync.WearLinkBridge * playback state, so visited pages work offline. Playback itself always * happens on the phone; this UI only sends ops and reflects state. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearMusicStore>(WearRoute.Home) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearMusicStore.get(this)+ override fun createStore() = WearMusicStore.get(this).also { WearLinkBridge.get(this)- store.publishWatchState()- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Home) }- WearMusicRoot(store = store, route = route, onRoute = { route = it })- }- }+ it.publishWatchState()+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ WearMusicRoot(store = store, route = route, onRoute = navigate) } }
notes-wear/src/main/java/com/vayunmathur/notes/wear/ui/WearMainActivity.kt
diff --git a/notes-wear/src/main/java/com/vayunmathur/notes/wear/ui/WearMainActivity.kt b/notes-wear/src/main/java/com/vayunmathur/notes/wear/ui/WearMainActivity.ktindex 1266f646c7..0ad6d02ccc 100644--- a/notes-wear/src/main/java/com/vayunmathur/notes/wear/ui/WearMainActivity.kt+++ b/notes-wear/src/main/java/com/vayunmathur/notes/wear/ui/WearMainActivity.kt@@ -1,13 +1,7 @@ package com.vayunmathur.notes.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable+import com.vayunmathur.library.wearlink.WearShellActivity import com.vayunmathur.notes.wear.data.WearNotesStore import com.vayunmathur.notes.wear.sync.WearLinkBridge @@ -17,41 +11,37 @@ import com.vayunmathur.notes.wear.sync.WearLinkBridge * (phone absent, cold start). Watch-originated changes queue in the * store and flow back over WearLink; nothing here claims a phone write. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearNotesStore>(WearRoute.NoteList) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearNotesStore.get(this)+ override fun createStore() = WearNotesStore.get(this).also { WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.NoteList) }- when (val current = route) {- WearRoute.NoteList -> WearNotesListScreen(- store = store,- onOpenNote = { route = WearRoute.NoteView(it) },- onCreateNote = { route = WearRoute.CreateNote },- )- is WearRoute.NoteView -> WearNoteScreen(- store = store,- syncId = current.syncId,- onEdit = { route = WearRoute.EditNote(it) },- onBack = { route = WearRoute.NoteList },- )- WearRoute.CreateNote -> WearNoteEditorScreen(- store = store,- syncId = null,- onDone = { route = WearRoute.NoteList },- )- is WearRoute.EditNote -> WearNoteEditorScreen(- store = store,- syncId = current.syncId,- onDone = { route = WearRoute.NoteView(current.syncId) },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.NoteList -> WearNotesListScreen(+ store = store,+ onOpenNote = { navigate(WearRoute.NoteView(it)) },+ onCreateNote = { navigate(WearRoute.CreateNote) },+ )+ is WearRoute.NoteView -> WearNoteScreen(+ store = store,+ syncId = current.syncId,+ onEdit = { navigate(WearRoute.EditNote(it)) },+ onBack = { navigate(WearRoute.NoteList) },+ )+ WearRoute.CreateNote -> WearNoteEditorScreen(+ store = store,+ syncId = null,+ onDone = { navigate(WearRoute.NoteList) },+ )+ is WearRoute.EditNote -> WearNoteEditorScreen(+ store = store,+ syncId = current.syncId,+ onDone = { navigate(WearRoute.NoteView(current.syncId)) },+ ) } } }
todo-wear/src/main/java/com/vayunmathur/todo/wear/ui/WearMainActivity.kt
diff --git a/todo-wear/src/main/java/com/vayunmathur/todo/wear/ui/WearMainActivity.kt b/todo-wear/src/main/java/com/vayunmathur/todo/wear/ui/WearMainActivity.ktindex 248b78a310..63fa846eb5 100644--- a/todo-wear/src/main/java/com/vayunmathur/todo/wear/ui/WearMainActivity.kt+++ b/todo-wear/src/main/java/com/vayunmathur/todo/wear/ui/WearMainActivity.kt@@ -1,13 +1,7 @@ package com.vayunmathur.todo.wear.ui -import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme+import androidx.compose.runtime.Composable+import com.vayunmathur.library.wearlink.WearShellActivity import com.vayunmathur.todo.wear.data.WearTodoStore import com.vayunmathur.todo.wear.sync.WearLinkBridge @@ -17,64 +11,60 @@ import com.vayunmathur.todo.wear.sync.WearLinkBridge * it works standalone. Watch-originated changes queue in the store and * flow back over WearLink; nothing here claims a phone write. */-class WearMainActivity : ComponentActivity() {+class WearMainActivity : WearShellActivity<WearRoute, WearTodoStore>(WearRoute.Home) { - override fun onCreate(savedInstanceState: Bundle?) {- super.onCreate(savedInstanceState)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store = WearTodoStore.get(this)+ override fun createStore() = WearTodoStore.get(this).also { WearLinkBridge.get(this) WearLinkBridge.publishWatchState(this)- setContent {- MaterialTheme {- var route by remember { mutableStateOf<WearRoute>(WearRoute.Home) }- when (val current = route) {- WearRoute.Home -> WearTodoHomeScreen(- store = store,- onOpenPage = { route = WearRoute.Page(it) },- onQuickAdd = {- route = WearRoute.QuickAdd(store.catalog.value.defaultViewKey)- },- )- is WearRoute.Page -> WearTodoPageScreen(- store = store,- pageKey = current.pageKey,- onOpenTask = { route = WearRoute.TaskDetail(it, current.pageKey) },- onQuickAdd = { route = WearRoute.QuickAdd(current.pageKey) },- onBack = { route = WearRoute.Home },- )- is WearRoute.TaskDetail -> WearTodoTaskScreen(- store = store,- syncId = current.syncId,- pageKey = current.pageKey,- onEdit = { route = WearRoute.EditTask(it, current.pageKey) },- onConfirmDelete = {- route = WearRoute.ConfirmDelete(it, current.pageKey)- },- onBack = { route = WearRoute.Page(current.pageKey) },- )- is WearRoute.QuickAdd -> WearTodoQuickAddScreen(- store = store,- pageKey = current.pageKey,- onDone = { route = WearRoute.Page(current.pageKey) },- )- is WearRoute.EditTask -> WearTodoEditScreen(- store = store,- syncId = current.syncId,- onDone = {- route = WearRoute.TaskDetail(current.syncId, current.pageKey)- },- )- is WearRoute.ConfirmDelete -> WearTodoConfirmDeleteScreen(- store = store,- syncId = current.syncId,- onDone = { route = WearRoute.Page(current.pageKey) },- onCancel = {- route = WearRoute.TaskDetail(current.syncId, current.pageKey)- },- )- }- }+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.Home -> WearTodoHomeScreen(+ store = store,+ onOpenPage = { navigate(WearRoute.Page(it)) },+ onQuickAdd = {+ navigate(WearRoute.QuickAdd(store.catalog.value.defaultViewKey))+ },+ )+ is WearRoute.Page -> WearTodoPageScreen(+ store = store,+ pageKey = current.pageKey,+ onOpenTask = { navigate(WearRoute.TaskDetail(it, current.pageKey)) },+ onQuickAdd = { navigate(WearRoute.QuickAdd(current.pageKey)) },+ onBack = { navigate(WearRoute.Home) },+ )+ is WearRoute.TaskDetail -> WearTodoTaskScreen(+ store = store,+ syncId = current.syncId,+ pageKey = current.pageKey,+ onEdit = { navigate(WearRoute.EditTask(it, current.pageKey)) },+ onConfirmDelete = {+ navigate(WearRoute.ConfirmDelete(it, current.pageKey))+ },+ onBack = { navigate(WearRoute.Page(current.pageKey)) },+ )+ is WearRoute.QuickAdd -> WearTodoQuickAddScreen(+ store = store,+ pageKey = current.pageKey,+ onDone = { navigate(WearRoute.Page(current.pageKey)) },+ )+ is WearRoute.EditTask -> WearTodoEditScreen(+ store = store,+ syncId = current.syncId,+ onDone = {+ navigate(WearRoute.TaskDetail(current.syncId, current.pageKey))+ },+ )+ is WearRoute.ConfirmDelete -> WearTodoConfirmDeleteScreen(+ store = store,+ syncId = current.syncId,+ onDone = { navigate(WearRoute.Page(current.pageKey)) },+ onCancel = {+ navigate(WearRoute.TaskDetail(current.syncId, current.pageKey))+ },+ ) } } }
weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearMainActivity.kt
diff --git a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearMainActivity.kt b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearMainActivity.ktindex c2a4ade5d9..8abfeeec75 100644--- a/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearMainActivity.kt+++ b/weather-wear/src/main/java/com/vayunmathur/weather/wear/ui/WearMainActivity.kt@@ -1,18 +1,26 @@ package com.vayunmathur.weather.wear.ui-import android.os.Bundle-import androidx.activity.ComponentActivity-import androidx.activity.compose.setContent-import androidx.compose.runtime.getValue-import androidx.compose.runtime.mutableStateOf-import androidx.compose.runtime.remember-import androidx.compose.runtime.setValue-import androidx.wear.compose.material.MaterialTheme++import androidx.compose.runtime.Composable+import com.vayunmathur.library.wearlink.WearShellActivity import com.vayunmathur.weather.wear.data.WearWeatherStore import com.vayunmathur.weather.wear.sync.WearLinkBridge-class WearMainActivity:ComponentActivity(){ override fun onCreate(b:Bundle?){ super.onCreate(b)- com.vayunmathur.library.wearlink.WearLinkPermissions.requestIfNeeded(this)- val store=WearWeatherStore.get(this); WearLinkBridge.get(this); store.publishWatchState()- setContent{ MaterialTheme{ var route by remember{mutableStateOf<WearRoute>(WearRoute.Locations)}- when(val cur=route){ WearRoute.Locations->WearLocationsScreen(store){route=WearRoute.Location(it)}- is WearRoute.Location->WearLocationScreen(store,cur.locationId){route=WearRoute.Locations} } } }-} }++class WearMainActivity : WearShellActivity<WearRoute, WearWeatherStore>(WearRoute.Locations) {++ override fun createStore() = WearWeatherStore.get(this).also {+ WearLinkBridge.get(this)+ it.publishWatchState()+ }++ @Composable+ override fun Screen(route: WearRoute, navigate: (WearRoute) -> Unit) {+ when (val current = route) {+ WearRoute.Locations -> WearLocationsScreen(store) {+ navigate(WearRoute.Location(it))+ }+ is WearRoute.Location -> WearLocationScreen(store, current.locationId) {+ navigate(WearRoute.Locations)+ }+ }+ }+}