Modern Apps git

← Modern-Apps.git

calendar-wear: all-day events live by date, not by the UTC end instant

Commit
6a7fb735a6dc295a4767efdcad23039f850b8488
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:47 UTC (5 days ago)
Parents
533f88d
wearAgenda applied instant liveness (endMillis > now) to
every event, but all-day events carry UTC-midnight bounds:
in Denver the UTC end falls at 17:00 local, so an all-day
event vanished from Today partway through its local day;
in Kiritimati it lingered into the next local day. The day
bucketing beside the liveness check was already date-based
— only liveness was instant-based.

All-day events are now live by date: kept while their last
covered UTC day (end-at-midnight belongs to the previous
day, a zero-duration midnight event keeps its start day —
the phone's span-day rule) is on/after the watch's local
today. Timed events keep instant liveness. Two regression
tests with fixed epochs (Denver 18:00 still-today; Kiritimati
next-day) failed pre-fix. Verified: :calendar:wear:
testDebugUnitTest and lint BUILD SUCCESSFUL.

Diff

calendar-wear/src/main/java/com/vayunmathur/calendar/wear/domain/WearAgenda.kt
diff --git a/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/domain/WearAgenda.kt b/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/domain/WearAgenda.ktindex f38eed6a2b..729ca52871 100644--- a/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/domain/WearAgenda.kt+++ b/calendar-wear/src/main/java/com/vayunmathur/calendar/wear/domain/WearAgenda.kt@@ -1,7 +1,10 @@ package com.vayunmathur.calendar.wear.domain +import kotlinx.datetime.DatePeriod import kotlinx.datetime.LocalDate+import kotlinx.datetime.LocalTime import kotlinx.datetime.TimeZone+import kotlinx.datetime.minus import kotlinx.datetime.toLocalDateTime import kotlin.time.Instant @@ -19,10 +22,25 @@ data class WearAgenda(  */ fun wearAgenda(snapshot: WearCalendarSnapshot, nowMillis: Long, zone: TimeZone): WearAgenda {     val today = Instant.fromEpochMilliseconds(nowMillis).toLocalDateTime(zone).date-    val live = snapshot.events.filter { it.endMillis > nowMillis }     fun startDate(event: WearCalendarEvent): LocalDate =         Instant.fromEpochMilliseconds(event.startMillis)             .toLocalDateTime(if (event.allDay) TimeZone.UTC else zone).date+    // Last day an all-day event covers, in its UTC wall dates: an end exactly+    // at midnight belongs to the previous day, mirroring the phone's span-day+    // rule. A zero-duration event at midnight still covers its start day.+    fun lastDate(event: WearCalendarEvent): LocalDate {+        val end = Instant.fromEpochMilliseconds(event.endMillis).toLocalDateTime(TimeZone.UTC)+        val endDate = if (end.time == LocalTime(0, 0)) end.date - DatePeriod(days = 1) else end.date+        val start = startDate(event)+        return if (endDate < start) start else endDate+    }+    // Timed events are live until their end instant. All-day events carry+    // UTC-midnight bounds, so instant liveness would drop them partway through+    // their local day west of UTC (and linger past it east of UTC): they are+    // live by date instead, for their whole local day.+    val live = snapshot.events.filter { event ->+        if (event.allDay) lastDate(event) >= today else event.endMillis > nowMillis+    }     return WearAgenda(         today = live.filter { startDate(it) <= today },         upcoming = live.filter { startDate(it) > today },
calendar-wear/src/test/java/com/vayunmathur/calendar/wear/domain/WearAgendaTest.kt
diff --git a/calendar-wear/src/test/java/com/vayunmathur/calendar/wear/domain/WearAgendaTest.kt b/calendar-wear/src/test/java/com/vayunmathur/calendar/wear/domain/WearAgendaTest.ktindex b4b79db0ea..3ab1aa2646 100644--- a/calendar-wear/src/test/java/com/vayunmathur/calendar/wear/domain/WearAgendaTest.kt+++ b/calendar-wear/src/test/java/com/vayunmathur/calendar/wear/domain/WearAgendaTest.kt@@ -33,6 +33,44 @@ class WearAgendaTest {         assertEquals(listOf(3L), agenda.upcoming.map { it.eventId })     } +    @Test+    fun allDayEventStaysInTodayForItsWholeLocalDayWestOfUtc() {+        // All-day 2026-11-25: UTC-midnight bounds as the phone stores them.+        val allDay = WearCalendarEvent(+            eventId = 10L,+            title = "Holiday",+            startMillis = 1_795_564_800_000L, // 2026-11-25T00:00:00Z+            endMillis = 1_795_651_200_000L, // 2026-11-26T00:00:00Z+            allDay = true,+            timezone = "UTC",+        )+        val snapshot = WearCalendarSnapshot(events = listOf(allDay))+        // 2026-11-25 18:00 in America/Denver (MST, UTC-7) = 2026-11-26T01:00:00Z:+        // still the event's local day, so it must still be in today's agenda.+        val agenda = wearAgenda(snapshot, 1_795_654_800_000L, TimeZone.of("America/Denver"))+        assertEquals(listOf(10L), agenda.today.map { it.eventId })+        assertEquals(emptyList(), agenda.upcoming)+    }++    @Test+    fun allDayEventLeavesTodayAfterItsLocalDayEastOfUtc() {+        // All-day 2026-11-25: UTC-midnight bounds as the phone stores them.+        val allDay = WearCalendarEvent(+            eventId = 10L,+            title = "Holiday",+            startMillis = 1_795_564_800_000L, // 2026-11-25T00:00:00Z+            endMillis = 1_795_651_200_000L, // 2026-11-26T00:00:00Z+            allDay = true,+            timezone = "UTC",+        )+        val snapshot = WearCalendarSnapshot(events = listOf(allDay))+        // 2026-11-26 08:00 in Pacific/Kiritimati (UTC+14) = 2026-11-25T18:00:00Z:+        // the local day has moved past the event's day, so it must be gone.+        val agenda = wearAgenda(snapshot, 1_795_629_600_000L, TimeZone.of("Pacific/Kiritimati"))+        assertEquals(emptyList(), agenda.today)+        assertEquals(emptyList(), agenda.upcoming)+    }+     @Test     fun emptySnapshotHasEmptyAgenda() {         val agenda = wearAgenda(WearCalendarSnapshot(), 0L, TimeZone.UTC)