fix(native): widgets turn to the next trip day by themselves

TestFlight feedback: during a trip the widgets stayed on day 1 until the app
was opened again. The app wrote only today's day into the widget snapshot,
and only while it ran, so every timeline entry after midnight still showed
the day it last ran, with its stops all ahead again.

The snapshot now also carries the trip's days from today on (a month at
most, sixteen stops a day), each with its date. On iOS every timeline entry
takes the day of its own date, so the quarter-hour entry at midnight turns
the widget to the next day, and the bookings list drops the days behind; the
Android widgets do the same on each update. A snapshot from an app without
the days reads as it was written, and an older app binary ignores them.
This commit is contained in:
Maurice
2026-11-18 21:28:00 +01:00
parent fd35f79c36
commit 3e0a2ef8ed
10 changed files with 377 additions and 33 deletions
@@ -82,10 +82,12 @@ abstract class TrekWidget : GlanceAppWidget() {
// Read inside the composition: a session outlives one update, and every
// write (the dashboard's, then each trip's) must reach it.
val version by WidgetStore.version.collectAsState()
val snapshot = remember(version) {
val stored = remember(version) {
val tripId = if (perTrip && appWidgetId >= 0) WidgetStore.tripFor(context, appWidgetId) else WidgetStore.AUTO
WidgetStore.load(context, tripId)
}
// The day of the trip is today's on every update, not the one the app last wrote.
val snapshot = stored?.on(LocalDate.now())
TrekWidgetTheme.Themed { Content(snapshot) }
}
}
@@ -27,7 +27,28 @@ data class WidgetSnapshot(
val labels: Labels,
/** The trip's next flight or the one in the air; last and defaulted, so older callers keep compiling. */
val flight: Flight? = null,
/**
* The trip's days from the day the app wrote this on, each with its date, so
* the widget turns to the next day itself (see [on]); empty from an app
* that predates them.
*/
val days: List<Today> = emptyList(),
) {
/**
* The snapshot as it reads on [date]: that date's trip day out of [days],
* none before or after the trip, and only the bookings from that date on.
* The app writes its snapshot only while it runs, so without this a widget
* showed the day it last ran. A snapshot without [days] reads as written.
*/
fun on(date: LocalDate): WidgetSnapshot {
if (days.isEmpty()) return this
val key = date.toString()
return copy(
today = days.firstOrNull { it.date == key },
bookings = bookings.filter { (it.date ?: key) >= key },
)
}
data class Trip(
val title: String,
val start: String,
@@ -52,7 +73,8 @@ data class WidgetSnapshot(
val symbol: String?,
)
data class Today(val dayLabel: String, val stops: List<Stop>, val index: Int?, val total: Int?)
/** A trip day; [date] (yyyy-MM-dd) is absent from an app that predates the days ahead. */
data class Today(val dayLabel: String, val stops: List<Stop>, val index: Int?, val total: Int?, val date: String? = null)
data class Weather(
val temp: Int,
@@ -212,6 +234,16 @@ data class WidgetSnapshot(
null
}
private fun dayOf(t: JSONObject): Today = Today(
dayLabel = t.optString("dayLabel"),
stops = t.optJSONArray("stops").objects().map {
Stop(it.str("time"), it.optString("name"), it.int("minutes"), it.str("color"), it.str("symbol"))
},
index = t.int("index"),
total = t.int("total"),
date = t.str("date"),
)
fun fromJson(o: JSONObject): WidgetSnapshot = WidgetSnapshot(
trip = o.optJSONObject("trip")?.let {
Trip(
@@ -226,16 +258,8 @@ data class WidgetSnapshot(
memberCount = it.int("memberCount"),
)
},
today = o.optJSONObject("today")?.let { t ->
Today(
dayLabel = t.optString("dayLabel"),
stops = t.optJSONArray("stops").objects().map {
Stop(it.str("time"), it.optString("name"), it.int("minutes"), it.str("color"), it.str("symbol"))
},
index = t.int("index"),
total = t.int("total"),
)
},
today = o.optJSONObject("today")?.let(::dayOf),
days = o.optJSONArray("days").objects().map(::dayOf),
weather = o.optJSONObject("weather")?.let {
Weather(it.optInt("temp"), it.int("high"), it.int("low"), it.optString("main"), it.optString("label"), it.str("place"), it.int("rain"))
},
@@ -99,4 +99,34 @@ class WidgetSnapshotTest {
assertEquals("ferry.fill", TrekIcons.booking("cruise").first)
assertEquals("bookmark.fill", TrekIcons.booking("something").first)
}
@Test
fun turnsToTheDayOfTheDateItIsReadOn() {
val json = """{"trip":{"title":"T","start":"2026-11-06","end":"2026-11-08","path":"/trips/1"},
"today":{"date":"2026-11-06","dayLabel":"Day 1 of 3","stops":[{"time":"09:00","name":"Museum","minutes":540}]},
"days":[
{"date":"2026-11-06","dayLabel":"Day 1 of 3","stops":[{"time":"09:00","name":"Museum","minutes":540}]},
{"date":"2026-11-07","dayLabel":"Day 2 of 3","stops":[{"time":"10:00","name":"Harbour","minutes":600}]}
],
"bookings":[
{"title":"Hotel","kind":"hotel","date":"2026-11-06","confirmed":true,"path":"/trips/1"},
{"title":"Train","kind":"train","date":"2026-11-07","confirmed":true,"path":"/trips/1"}
],
"labels":{}}"""
val snapshot = WidgetSnapshot.parse(json)!!
assertEquals("Day 1 of 3", snapshot.on(LocalDate.of(2026, 11, 6)).today!!.dayLabel)
val second = snapshot.on(LocalDate.of(2026, 11, 7))
assertEquals("Day 2 of 3", second.today!!.dayLabel)
assertEquals("Harbour", second.today!!.stops.single().name)
assertEquals(listOf("Train"), second.bookings.map { it.title })
// A day the app wrote nothing for (the trip is over) shows no day.
assertNull(snapshot.on(LocalDate.of(2026, 11, 9)).today)
}
@Test
fun aSnapshotWithoutDaysReadsAsWritten() {
val snapshot = WidgetSnapshot.parse(WidgetSamples.json(running = true, now = today))!!
assertTrue(snapshot.days.isEmpty())
assertEquals(snapshot, snapshot.on(today.plusDays(3)))
}
}