weather: the watch's daily forecast is no longer last week's weather
- Commit
4fd95db78cbffeca7de536bfdbc5fb60d393c83e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 04:05 UTC (5 days ago)
- Parents
c518e81
The bundle's daily array is 15 days with today at index 7, and the phone->watch sync sliced hourly correctly from the current hour but mapped daily from index 0, then capped at 7. The watch therefore received the seven days ending yesterday (Jul 8-14 for a Jul 15 'today') rendered as its Daily forecast — today was not even included. mapLocation now routes daily through wearDailyForecast, which drops to todayIndex(...) before mapping (keeping every field attached to its own date index); hourly behaviour is unchanged. Four new tests pin the today-anchored slice, the snapshot's today..today+6 window, and the no-current/empty fallbacks. Verified: :weather:testDevUnitTest 240 tests pass; lint errors are pre-existing on untouched files.
Diff
weather/src/main/java/com/vayunmathur/weather/sync/WearWeatherSync.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/sync/WearWeatherSync.kt b/weather/src/main/java/com/vayunmathur/weather/sync/WearWeatherSync.ktindex cf0cd4fc73..872527f673 100644--- a/weather/src/main/java/com/vayunmathur/weather/sync/WearWeatherSync.kt+++ b/weather/src/main/java/com/vayunmathur/weather/sync/WearWeatherSync.kt@@ -15,7 +15,9 @@ import com.vayunmathur.weather.domain.WearWeatherSnapshot import com.vayunmathur.weather.domain.WearWatchState import com.vayunmathur.weather.domain.buildWearWeatherSnapshot import com.vayunmathur.weather.domain.roundCoord+import com.vayunmathur.weather.domain.todayIndex import com.vayunmathur.weather.domain.wearCompanionPresent+import com.vayunmathur.weather.network.Daily import com.vayunmathur.weather.network.ForecastResponse import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers@@ -57,7 +59,24 @@ class WearWeatherSync private constructor(context:Context){ private fun mapLocation(loc:com.vayunmathur.weather.data.SavedLocation, f:ForecastResponse):WearWeatherLocation{ val cur=f.current; val hourlyDto=f.hourly; val dailyDto=f.daily; val startIdx=if(cur!=null&&hourlyDto!=null) hourlyDto.time.indexOfFirst{it>=cur.time}.let{if(it<0)0 else it} else 0 val hourly=hourlyDto?.time?.drop(startIdx)?.mapIndexedNotNull{ i, t-> val idx=startIdx+i; WearWeatherHour(t, hourlyDto.temperature.getOrElse(idx){0.0}, hourlyDto.weatherCode.getOrElse(idx){-1}, hourlyDto.precipitationProbability.getOrElse(idx){0}, hourlyDto.isDay.getOrElse(idx){1}==1) }?:emptyList()- val daily=dailyDto?.time?.mapIndexed{ i,d-> WearWeatherDay(d, dailyDto.weatherCode.getOrElse(i){-1}, dailyDto.temperatureMax.getOrElse(i){0.0}, dailyDto.temperatureMin.getOrElse(i){0.0}, dailyDto.precipitationProbabilityMax.getOrElse(i){0}) }?:emptyList()+ val daily=wearDailyForecast(dailyDto, cur?.time) return WearWeatherLocation(loc.id, loc.name, loc.country, loc.isCurrent, f.timezone?:"UTC", cur?.let{WearWeatherCurrent(it.time,it.temperature,it.apparentTemperature,it.relativeHumidity,it.weatherCode,it.isDay==1,it.windSpeed,it.windDirection,it.pressureMsl)}, hourly, daily) } }++/**+ * The watch's daily forecast starting today. The bundle's daily array+ * carries 7 past days before today (15 entries: today-7 .. today+7), so+ * slicing must start at [todayIndex] — taking from index 0 would publish+ * the past week as the forecast once the snapshot caps the list at+ * `MAX_DAILY` entries. Mirrors how the hourly series above starts at the+ * current hour, not the first (past) hour.+ */+internal fun wearDailyForecast(daily: Daily?, currentTime: String?): List<WearWeatherDay> {+ val dto = daily ?: return emptyList()+ val start = todayIndex(dto, currentTime)+ return dto.time.drop(start).mapIndexed { j, d ->+ val i = start + j+ WearWeatherDay(d, dto.weatherCode.getOrElse(i){-1}, dto.temperatureMax.getOrElse(i){0.0}, dto.temperatureMin.getOrElse(i){0.0}, dto.precipitationProbabilityMax.getOrElse(i){0})+ }+}
weather/src/test/java/com/vayunmathur/weather/sync/WearDailyForecastTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/sync/WearDailyForecastTest.kt b/weather/src/test/java/com/vayunmathur/weather/sync/WearDailyForecastTest.ktnew file mode 100644index 0000000000..6405df1a14--- /dev/null+++ b/weather/src/test/java/com/vayunmathur/weather/sync/WearDailyForecastTest.kt@@ -0,0 +1,62 @@+package com.vayunmathur.weather.sync++import com.vayunmathur.weather.domain.WearWeatherLocation+import com.vayunmathur.weather.domain.WearWeatherProtocol+import com.vayunmathur.weather.domain.buildWearWeatherSnapshot+import com.vayunmathur.weather.network.Daily+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class WearDailyForecastTest {++ // Bundle daily window: 15 days, today (2026-07-15) at index 7.+ private val daily = Daily(+ time = (8..22).map { "2026-07-%02d".format(it) },+ weatherCode = (0 until 15).toList(),+ temperatureMax = (0 until 15).map { 20.0 + it },+ temperatureMin = (0 until 15).map { 10.0 + it },+ precipitationProbabilityMax = (0 until 15).map { it * 5 },+ )++ @Test+ fun dailyForecastStartsTodayNotSevenDaysAgo() {+ val days = wearDailyForecast(daily, "2026-07-15T14:00")+ assertEquals("2026-07-15", days.first().dateIso)+ assertEquals(8, days.size)+ // Fields stay attached to their own date: today is index 7.+ assertEquals(7, days.first().weatherCode)+ assertEquals(27.0, days.first().temperatureMaxC)+ assertEquals(17.0, days.first().temperatureMinC)+ assertEquals(35, days.first().precipitationProbabilityMaxPercent)+ assertEquals("2026-07-22", days.last().dateIso)+ }++ @Test+ fun snapshotPublishesUpcomingWeekNotPastWeek() {+ val location = WearWeatherLocation(+ locationId = 1L,+ name = "Loc",+ daily = wearDailyForecast(daily, "2026-07-15T14:00"),+ )+ val snapshot = buildWearWeatherSnapshot(listOf(location), 0L)+ val published = snapshot.locations.single().daily+ assertEquals(WearWeatherProtocol.MAX_DAILY, published.size)+ assertEquals("2026-07-15", published.first().dateIso)+ assertEquals("2026-07-21", published.last().dateIso)+ assertTrue(published.none { it.dateIso < "2026-07-15" })+ }++ @Test+ fun dailyForecastWithoutCurrentKeepsWholeListFromFallbackIndex() {+ // No current time: todayIndex centres the 15-day window at index 7.+ val days = wearDailyForecast(daily, null)+ assertEquals("2026-07-15", days.first().dateIso)+ }++ @Test+ fun dailyForecastMissingDailyIsEmpty() {+ assertEquals(emptyList(), wearDailyForecast(null, "2026-07-15T14:00"))+ assertEquals(emptyList(), wearDailyForecast(Daily(), "2026-07-15T14:00"))+ }+}