Modern Apps git

← Modern-Apps.git

calendar: add Delete event after Open on phone on watch

Commit
f8f4662caee1d18517aa7b0eccc7ee0b96c6077a
Author
Vayun Mathur <[email protected]>
When
2026-12-06 04:27 UTC (6 hours ago)
Parents
7f66455
The watch event detail was read-only with Open on phone last and no delete, although the phone deletes events from the provider. It now shows the red Delete event button directly after Open on phone (nothing follows it), with an inline confirmation. Delete sends a new /calendar/op delete-event message; the phone applies the same provider delete as its own UI (row delete, DELETED flag fallback for read-only calendars) and republishes the snapshot. Open-on-phone icon now tints to the button content color.

Verified: :calendar:lint, :calendar:lintVitalRelease, :calendar:testDevUnitTest green; wear metadata regenerated.

Diff

calendar/AGENTS.md
diff --git a/calendar/AGENTS.md b/calendar/AGENTS.mdindex b3bb6e418e..129a9b10a7 100644--- a/calendar/AGENTS.md+++ b/calendar/AGENTS.md@@ -39,11 +39,11 @@ any phone state loads; phones get the month/agenda UI. The phone ## Wear sync (Watch conduit client) **Protocol:** shared wire types live in `:library:wearlink` at `com.vayunmathur.library.wearlink.calendar.WearCalendarProtocol`; the golden-JSON tests in this module pin the shared code's wire format. - Snapshot + watch-state sync: conduit data items (`/calendar/snapshot` phone -> watch, `/calendar/watch-state` watch -> phone)-- Sync request: conduit message (`/calendar/request-sync`), watch -> phone only+- Sync request and event delete: conduit messages (`/calendar/request-sync`, `/calendar/op` with a delete-event op the phone applies to the provider), watch -> phone only - **Transport: Watch conduit client** โ€” phone side `sync/WearCalendarSync.kt` and watch side `sync/WearLinkBridge.kt` both use `WearLinkClientNode` (Messenger to the Watch app's `watch_conduit` service, appId `calendar`); this app owns no RFCOMM link and holds no Bluetooth permission for this channel, so neither entry activity prompts for Bluetooth. With the Watch app absent the node reports `NotInstalled`/`Unavailable` and the sync stays idle; snapshots republish on conduit `Connected` and on calendar changes. `FormFactor` picks the side at runtime, so only one of the two nodes runs on a given device. - **Push triggers:** the publish also runs on the phone Watch app's scheduled sync-trigger intent (`sync/ConduitSyncTriggerReceiver`, exported only under the `CONDUIT` signature permission, invoking `WearCalendarSync.syncNow()` โ€” no UI, no prompt). Data arriving while the receiving process is dead lands in the conduit host's persistent store and replays on registration. **Watch experience:**-- `ui/WearMainActivity` routes `ui/WearRoute` over the agenda (`ui/WearAgendaScreen`) and read-only event detail (`ui/WearEventScreen`), rendered from `data/WearCalendarStore` (`wear-calendar-snapshot.json`); sectioning is `domain/wearAgenda`. The watch has no event editing or RSVP and no manual sync action; snapshots arrive automatically over the conduit.+- `ui/WearMainActivity` routes `ui/WearRoute` over the agenda (`ui/WearAgendaScreen`) and event detail (`ui/WearEventScreen`: read-only fields plus a Delete event button after Open on phone, applied by the phone over the conduit), rendered from `data/WearCalendarStore` (`wear-calendar-snapshot.json`); sectioning is `domain/wearAgenda`. The watch has no event editing or RSVP and no manual sync action; snapshots arrive automatically over the conduit. - Watch listing screenshots are the `group = "wear"` previews in `src/screenshotTest/.../ui/WearMetadataPreviews.kt`, collected by `:calendar:metadata` into `metadata_data/photos/calendar/wear/`; phone shots use the `phone` bucket. **Gotchas:** - `util/` is NOT a closed package root โ€” don't add more files there
calendar/src/main/java/com/vayunmathur/calendar/sync/WearCalendarSync.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/sync/WearCalendarSync.kt b/calendar/src/main/java/com/vayunmathur/calendar/sync/WearCalendarSync.ktindex 62353118ca..63dff686e3 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/sync/WearCalendarSync.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/sync/WearCalendarSync.kt@@ -5,7 +5,9 @@ import android.content.Intent import com.vayunmathur.calendar.data.Calendar import com.vayunmathur.calendar.data.Event import com.vayunmathur.calendar.data.Instance+import android.provider.CalendarContract import com.vayunmathur.library.wearlink.calendar.WearCalendarEvent+import com.vayunmathur.library.wearlink.calendar.WearCalendarOp import com.vayunmathur.library.wearlink.calendar.WearCalendarProtocol import com.vayunmathur.library.wearlink.calendar.WearCalendarSnapshot import com.vayunmathur.library.wearlink.calendar.WearWatchState@@ -97,6 +99,15 @@ class WearCalendarSync private constructor(context: Context) {                 runCatching { appContext.startActivity(Intent(appContext, com.vayunmathur.calendar.MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("wear_open_id", body.decodeToString())) }                 return             }+            if (path == WearCalendarProtocol.OP_PATH) {+                val op = runCatching {+                    WearCalendarProtocol.json.decodeFromString<WearCalendarOp>(body.decodeToString())+                }.getOrNull() ?: return+                if (op.kind == WearCalendarOp.OP_DELETE_EVENT && op.eventId != 0L) {+                    scope.launch { deleteEvent(op.eventId) }+                }+                return+            }             if (path != WearCalendarProtocol.REQUEST_SYNC_PATH) return             scope.launch { publishSnapshot() }         }@@ -161,6 +172,31 @@ class WearCalendarSync private constructor(context: Context) {         }     } +    /** Delete one event exactly as the phone UI does, then republish so the watch converges. */+    private suspend fun deleteEvent(eventId: Long) {+        withContext(Dispatchers.IO) {+            runCatching {+                val deleted = appContext.contentResolver.delete(+                    CalendarContract.Events.CONTENT_URI,+                    "${CalendarContract.Events._ID} = ?",+                    arrayOf(eventId.toString()),+                )+                if (deleted == 0) {+                    // Provider refused (read-only calendar): flag it, as the phone UI does.+                    appContext.contentResolver.update(+                        CalendarContract.Events.CONTENT_URI,+                        android.content.ContentValues().apply {+                            put(CalendarContract.Events.DELETED, 1)+                        },+                        "${CalendarContract.Events._ID} = ?",+                        arrayOf(eventId.toString()),+                    )+                }+            }.onFailure { Log.status(TAG, "watch delete failed: ${it.message}") }+        }+        publishSnapshot()+    }+     private fun buildSnapshot(): WearCalendarSnapshot? =         runCatching {             val now = Clock.System.now()
calendar/src/main/java/com/vayunmathur/calendar/ui/WearEventScreen.kt
diff --git a/calendar/src/main/java/com/vayunmathur/calendar/ui/WearEventScreen.kt b/calendar/src/main/java/com/vayunmathur/calendar/ui/WearEventScreen.ktindex f9ebfd6c62..7ec5aaa252 100644--- a/calendar/src/main/java/com/vayunmathur/calendar/ui/WearEventScreen.kt+++ b/calendar/src/main/java/com/vayunmathur/calendar/ui/WearEventScreen.kt@@ -5,6 +5,7 @@ import com.vayunmathur.library.ui.IconMobileArrowRight import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.platform.LocalContext import kotlinx.coroutines.launch+import kotlinx.serialization.encodeToString import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.wearlink.wearDeleteItem @@ -13,7 +14,9 @@ import androidx.compose.foundation.layout.padding 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.compose.ui.Modifier import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign@@ -21,6 +24,7 @@ import androidx.compose.ui.unit.dp import androidx.wear.compose.foundation.lazy.ScalingLazyColumn import androidx.wear.compose.foundation.lazy.rememberScalingLazyListState import androidx.wear.compose.material.Chip+import androidx.wear.compose.material.LocalContentColor import androidx.wear.compose.material.ChipDefaults import androidx.wear.compose.material.MaterialTheme import androidx.wear.compose.material.Text@@ -43,7 +47,26 @@ fun WearEventScreen(     val event = snapshot.events.firstOrNull {         it.eventId == eventId && it.startMillis == startMillis     }-    WearEventContent(event = event, onBack = onBack)+    val context = LocalContext.current+    val scope = rememberCoroutineScope()+    WearEventContent(+        event = event,+        onDeleteEvent = { eventId ->+            scope.launch {+                WearLinkBridge.get(context).sendMessage(+                    com.vayunmathur.library.wearlink.calendar.WearCalendarProtocol.OP_PATH,+                    com.vayunmathur.library.wearlink.calendar.WearCalendarProtocol.json+                        .encodeToString(+                            com.vayunmathur.library.wearlink.calendar.WearCalendarOp(+                                eventId = eventId,+                            ),+                        ).encodeToByteArray(),+                )+            }+            onBack()+        },+        onBack = onBack,+    ) }  /**@@ -51,7 +74,12 @@ fun WearEventScreen(  * Compose previews (src/screenshotTest) render a literal fixture with no store.  */ @Composable-internal fun WearEventContent(event: WearCalendarEvent?, onBack: () -> Unit) {+internal fun WearEventContent(+    event: WearCalendarEvent?,+    onDeleteEvent: (Long) -> Unit = {},+    onBack: () -> Unit,+) {+    var confirmingDelete by remember { mutableStateOf(false) }     val zone = remember { TimeZone.currentSystemDefault() }     val listState = rememberScalingLazyListState() @@ -124,6 +152,40 @@ internal fun WearEventContent(event: WearCalendarEvent?, onBack: () -> Unit) {                 }             }         }-        wearOpenOnPhoneItem(onClick = { scope.launch { WearLinkBridge.get(context).sendMessage("/calendar/open-on-phone", ByteArray(0)) } }, icon = { IconMobileArrowRight(tint = androidx.compose.ui.graphics.Color.White) })+        wearOpenOnPhoneItem(+            onClick = {+                scope.launch {+                    WearLinkBridge.get(context).sendMessage("/calendar/open-on-phone", ByteArray(0))+                }+            },+            icon = { IconMobileArrowRight(tint = LocalContentColor.current) },+        )+        if (event != null) {+            if (confirmingDelete) {+                item {+                    Chip(+                        onClick = { onDeleteEvent(event.eventId) },+                        label = { Text("Confirm delete", maxLines = 1) },+                        icon = { IconDelete(tint = LocalContentColor.current) },+                        colors = ChipDefaults.primaryChipColors(),+                        modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp),+                    )+                }+                item {+                    Chip(+                        onClick = { confirmingDelete = false },+                        label = { Text("Cancel", maxLines = 1) },+                        colors = ChipDefaults.secondaryChipColors(),+                        modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp),+                    )+                }+            } else {+                wearDeleteItem(+                    label = "Delete event",+                    onClick = { confirmingDelete = true },+                    icon = { IconDelete(tint = LocalContentColor.current) },+                )+            }+        }     } }
library/wearlink/src/main/java/com/vayunmathur/library/wearlink/calendar/WearCalendarProtocol.kt
diff --git a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/calendar/WearCalendarProtocol.kt b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/calendar/WearCalendarProtocol.ktindex 94b5d1d1e7..7d6152abc6 100644--- a/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/calendar/WearCalendarProtocol.kt+++ b/library/wearlink/src/main/java/com/vayunmathur/library/wearlink/calendar/WearCalendarProtocol.kt@@ -15,11 +15,12 @@ import kotlinx.serialization.json.Json  * - Data items carry the snapshot: [SNAPSHOT_PATH] phone -> watch  *   and [WATCH_STATE_PATH] watch -> phone. Delivery needs a live session;  *   each side persists what it receives and republishes on reconnect.- * - Messages carry one ephemeral command, [REQUEST_SYNC_PATH]- *   watch -> phone: the watch asks for a fresh snapshot. That is the only- *   watch-originated write. There is no event editing or RSVP: the phone's- *   calendar model has no attendee/self-status for its local calendars, so- *   inventing an RSVP write would fake a change the provider never made.+ * - Messages carry ephemeral commands, watch -> phone: [REQUEST_SYNC_PATH]+ *   asks for a fresh snapshot, and [OP_PATH] asks the phone to delete one+ *   event (the provider delete the phone UI performs). There is no event+ *   editing or RSVP: the phone's calendar model has no attendee/self-status+ *   for its local calendars, so inventing an RSVP write would fake a change+ *   the provider never made.  */ object WearCalendarProtocol {     /** WearLink appId the phone requires in the peer handshake. */@@ -34,6 +35,9 @@ object WearCalendarProtocol {     /** Message path (watch -> phone): ask the phone to publish a fresh snapshot. */     const val REQUEST_SYNC_PATH = "/calendar/request-sync" +    /** Message path (watch -> phone): one [WearCalendarOp] for the phone to apply. */+    const val OP_PATH = "/calendar/op"+     /** Days ahead of the window start included in a snapshot. */     const val WINDOW_DAYS = 30 @@ -74,6 +78,19 @@ data class WearCalendarEvent(     val timezone: String = "UTC", ) +/** One watch-originated operation ([WearCalendarProtocol.OP_PATH]). */+@Serializable+data class WearCalendarOp(+    /** Only [OP_DELETE_EVENT] exists; anything else is ignored by the phone. */+    val kind: String = OP_DELETE_EVENT,+    /** Phone provider event id to delete (the whole event, as the phone UI's series delete). */+    val eventId: Long = 0L,+) {+    companion object {+        const val OP_DELETE_EVENT = "delete_event"+    }+}+ /** The phone's bounded calendar snapshot for the watch ([WearCalendarProtocol.SNAPSHOT_PATH]). */ @Serializable data class WearCalendarSnapshot(