weather: add past-week history and today outline
- Commit
33a4409a3f024776d2eda33644bf379739a3bd8a- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-22 00:41 UTC (2 months ago)
- Parents
c46f6b6
Diff
weather/src/main/java/com/vayunmathur/weather/domain/ForecastIdentity.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/ForecastIdentity.kt b/weather/src/main/java/com/vayunmathur/weather/domain/ForecastIdentity.ktnew file mode 100644index 0000000000..6e2f6d6570--- /dev/null+++ b/weather/src/main/java/com/vayunmathur/weather/domain/ForecastIdentity.kt@@ -0,0 +1,42 @@+package com.vayunmathur.weather.domain++import com.vayunmathur.weather.network.Daily+import com.vayunmathur.weather.network.ForecastResponse+import com.vayunmathur.weather.network.Hourly+import kotlinx.datetime.TimeZone+import kotlinx.datetime.toLocalDateTime++/**+ * Identity of "today" and "now" within a [ForecastResponse].+ *+ * The forecast endpoint returns `past_days` of history prepended to the+ * hourly/daily arrays, so today is no longer at index 0. Identity is derived+ * from `current.time` (already in the location's zone) by date/hour prefix+ * match, with a [ForecastResponse.utcOffsetSeconds]-based fallback when+ * `current` is missing or malformed.+ */++/** Today's date as an ISO `yyyy-MM-dd` string in the location's zone. */+fun todayIsoDate(forecast: ForecastResponse): String {+ val fromCurrent = forecast.current?.time?.substringBefore('T')?.takeIf { it.length == 10 }+ if (fromCurrent != null) return fromCurrent+ val now = System.currentTimeMillis() / 1000+ return kotlin.time.Instant.fromEpochSeconds(now + forecast.utcOffsetSeconds)+ .toLocalDateTime(TimeZone.UTC).date.toString()+}++/** Index of today within [Daily.time], or 0 when absent. */+fun todayIndex(daily: Daily?, currentTime: String?): Int {+ if (daily == null || daily.time.isEmpty()) return 0+ val today = currentTime?.substringBefore('T')?.takeIf { it.length == 10 }+ ?: return daily.time.size.coerceAtMost(8).dec().coerceAtLeast(0)+ return daily.time.indexOf(today).takeIf { it >= 0 } ?: 0+}++/** Index of the current hour within [Hourly.time], or 0 when absent. */+fun nowIndex(hourly: Hourly?, currentTime: String?): Int {+ if (hourly == null || hourly.time.isEmpty()) return 0+ val hourPrefix = currentTime?.substring(0, 13)?.takeIf { it.length == 13 }+ ?: return 0+ return hourly.time.indexOfFirst { it.startsWith(hourPrefix) }.takeIf { it >= 0 } ?: 0+}
weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt b/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.ktindex 9f7728e4fb..2e7629aa1c 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/MetricSeries.kt@@ -177,7 +177,7 @@ fun metricSeries( is SelectedDateOrTime.Time -> selected.isoTime.substringBefore('T') // No selection: plot today (the location's local calendar day) so the // graph always runs midnight-to-midnight, never a rolling 24h window.- null -> forecast.daily?.time?.firstOrNull() ?: localDate(forecast.utcOffsetSeconds)+ null -> todayIsoDate(forecast) } val out = ArrayList<MetricPoint>()@@ -190,10 +190,3 @@ fun metricSeries( } return out }--/** Today's date in the location's local time, as an ISO `yyyy-MM-dd` string. */-private fun localDate(utcOffsetSeconds: Int): String {- val now = System.currentTimeMillis() / 1000- return kotlin.time.Instant.fromEpochSeconds(now + utcOffsetSeconds)- .toLocalDateTime(kotlinx.datetime.TimeZone.UTC).date.toString()-}
weather/src/main/java/com/vayunmathur/weather/domain/SelectedData.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/domain/SelectedData.kt b/weather/src/main/java/com/vayunmathur/weather/domain/SelectedData.ktindex 424b76f54b..fe16be36bb 100644--- a/weather/src/main/java/com/vayunmathur/weather/domain/SelectedData.kt+++ b/weather/src/main/java/com/vayunmathur/weather/domain/SelectedData.kt@@ -54,23 +54,26 @@ fun resolveConditions( daily?.time?.indexOf(isoDate)?.takeIf { it >= 0 } return when (selected) {- null -> ResolvedConditions(- weatherCode = current.weatherCode,- isDay = current.isDay == 1,- temperature = current.temperature,- apparentTemperature = current.apparentTemperature,- high = daily?.temperatureMax?.firstOrNull(),- low = daily?.temperatureMin?.firstOrNull(),- uvIndexMax = daily?.uvIndexMax?.firstOrNull(),- sunriseIso = daily?.sunrise?.firstOrNull(),- sunsetIso = daily?.sunset?.firstOrNull(),- precipitationSum = daily?.precipitationSum?.firstOrNull(),- daylightDurationSec = daily?.daylightDuration?.firstOrNull(),- moonPhase = daily?.moonPhase?.firstOrNull(),- moonriseIso = daily?.moonrise?.firstOrNull(),- moonsetIso = daily?.moonset?.firstOrNull(),- blockCurrent = current,- )+ null -> {+ val t = todayIndex(daily, current.time)+ ResolvedConditions(+ weatherCode = current.weatherCode,+ isDay = current.isDay == 1,+ temperature = current.temperature,+ apparentTemperature = current.apparentTemperature,+ high = daily?.temperatureMax?.getOrNull(t),+ low = daily?.temperatureMin?.getOrNull(t),+ uvIndexMax = daily?.uvIndexMax?.getOrNull(t),+ sunriseIso = daily?.sunrise?.getOrNull(t),+ sunsetIso = daily?.sunset?.getOrNull(t),+ precipitationSum = daily?.precipitationSum?.getOrNull(t),+ daylightDurationSec = daily?.daylightDuration?.getOrNull(t),+ moonPhase = daily?.moonPhase?.getOrNull(t),+ moonriseIso = daily?.moonrise?.getOrNull(t),+ moonsetIso = daily?.moonset?.getOrNull(t),+ blockCurrent = current,+ )+ } is SelectedDateOrTime.Time -> { val h = hourly?.time?.indexOf(selected.isoTime)?.takeIf { it >= 0 }
weather/src/main/java/com/vayunmathur/weather/intents/GetWeatherIntent.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/intents/GetWeatherIntent.kt b/weather/src/main/java/com/vayunmathur/weather/intents/GetWeatherIntent.ktindex e2a96203f4..dde138a3a3 100644--- a/weather/src/main/java/com/vayunmathur/weather/intents/GetWeatherIntent.kt+++ b/weather/src/main/java/com/vayunmathur/weather/intents/GetWeatherIntent.kt@@ -7,6 +7,7 @@ import com.vayunmathur.weather.network.ForecastResponse import com.vayunmathur.weather.network.WeatherApi import com.vayunmathur.weather.domain.compassDirection import com.vayunmathur.weather.domain.parseLocalIsoToEpochSec+import com.vayunmathur.weather.domain.todayIndex import com.vayunmathur.weather.domain.weatherConditionForCode import kotlinx.serialization.InternalSerializationApi import kotlinx.serialization.Serializable@@ -56,12 +57,13 @@ internal fun errorWeatherData(locationName: String?, error: String) = WeatherDat internal fun ForecastResponse.toWeatherData(context: Context, locationName: String?): WeatherData { val current = current ?: return errorWeatherData(locationName, "No current observations available") val condition = context.getString(weatherConditionForCode(current.weatherCode).label)- val hi = daily?.temperatureMax?.firstOrNull() ?: current.temperature- val lo = daily?.temperatureMin?.firstOrNull() ?: current.temperature- val precip = daily?.precipitationProbabilityMax?.firstOrNull() ?: 0- val uv = daily?.uvIndexMax?.firstOrNull() ?: 0.0- val sunrise = daily?.sunrise?.firstOrNull()?.let { parseLocalIsoToEpochSec(it, utcOffsetSeconds) }- val sunset = daily?.sunset?.firstOrNull()?.let { parseLocalIsoToEpochSec(it, utcOffsetSeconds) }+ val t = todayIndex(daily, current.time)+ val hi = daily?.temperatureMax?.getOrNull(t) ?: current.temperature+ val lo = daily?.temperatureMin?.getOrNull(t) ?: current.temperature+ val precip = daily?.precipitationProbabilityMax?.getOrNull(t) ?: 0+ val uv = daily?.uvIndexMax?.getOrNull(t) ?: 0.0+ val sunrise = daily?.sunrise?.getOrNull(t)?.let { parseLocalIsoToEpochSec(it, utcOffsetSeconds) }+ val sunset = daily?.sunset?.getOrNull(t)?.let { parseLocalIsoToEpochSec(it, utcOffsetSeconds) } return WeatherData( locationName = locationName,
weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt b/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.ktindex f898fb6626..226888db2e 100644--- a/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt+++ b/weather/src/main/java/com/vayunmathur/weather/network/WeatherApi.kt@@ -15,6 +15,8 @@ object WeatherApi { private const val FORECAST_BASE = "https://api.open-meteo.com/v1/forecast" private const val GEOCODE_BASE = "https://geocoding-api.open-meteo.com/v1/search" private const val AIR_QUALITY_BASE = "https://air-quality-api.open-meteo.com/v1/air-quality"+ /** Days of historical data prepended to the forecast response. */+ private const val PAST_DAYS = 7 /** * Fetch the current conditions + 24h hourly + 7-day daily forecast for a@@ -82,6 +84,7 @@ object WeatherApi { ) append("&minutely_15=precipitation") append("&timezone=auto")+ append("&past_days=").append(PAST_DAYS) append("&forecast_days=7") } return NetworkClient.getJson(url)
weather/src/main/java/com/vayunmathur/weather/platform/Summary.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/platform/Summary.kt b/weather/src/main/java/com/vayunmathur/weather/platform/Summary.ktindex f6d90c56e1..4f5dce595f 100644--- a/weather/src/main/java/com/vayunmathur/weather/platform/Summary.kt+++ b/weather/src/main/java/com/vayunmathur/weather/platform/Summary.kt@@ -4,6 +4,7 @@ import android.content.Context import com.vayunmathur.weather.R import com.vayunmathur.weather.domain.TemperatureUnit import com.vayunmathur.weather.domain.formatTemperatureCompact+import com.vayunmathur.weather.domain.todayIndex import com.vayunmathur.weather.domain.weatherConditionForCode import com.vayunmathur.weather.network.ForecastResponse import java.util.Locale@@ -20,9 +21,10 @@ fun computeDaySummary(context: Context, forecast: ForecastResponse, tempUnit: Te val conditionLabel = current?.weatherCode?.let { context.getString(weatherConditionForCode(it).label).lowercase(Locale.getDefault()) } ?: context.getString(R.string.summary_mixed_conditions)- val hi = daily?.temperatureMax?.firstOrNull()- val lo = daily?.temperatureMin?.firstOrNull()- val precip = daily?.precipitationProbabilityMax?.firstOrNull() ?: 0+ val t = todayIndex(daily, current?.time)+ val hi = daily?.temperatureMax?.getOrNull(t)+ val lo = daily?.temperatureMin?.getOrNull(t)+ val precip = daily?.precipitationProbabilityMax?.getOrNull(t) ?: 0 val wind = current?.windSpeed val parts = mutableListOf<String>()
weather/src/main/java/com/vayunmathur/weather/service/car/WeatherCarDetailScreen.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/service/car/WeatherCarDetailScreen.kt b/weather/src/main/java/com/vayunmathur/weather/service/car/WeatherCarDetailScreen.ktindex 1c1737aae7..2e78180318 100644--- a/weather/src/main/java/com/vayunmathur/weather/service/car/WeatherCarDetailScreen.kt+++ b/weather/src/main/java/com/vayunmathur/weather/service/car/WeatherCarDetailScreen.kt@@ -97,7 +97,7 @@ class WeatherCarDetailScreen( val lo = day.temperatureMin.getOrNull(i) val code = day.weatherCode.getOrNull(i) val precip = day.precipitationProbabilityMax.getOrNull(i)- val dayLabel = dayLabelFor(isoDate, i)+ val dayLabel = dayLabelFor(isoDate, current?.time) val parts = mutableListOf<String>() if (hi != null && lo != null) { parts += "${formatTemperatureCompact(hi, tempUnit)} / " +@@ -124,8 +124,8 @@ class WeatherCarDetailScreen( .build() } - private fun dayLabelFor(isoDate: String, index: Int): String {- if (index == 0) return "Today"+ private fun dayLabelFor(isoDate: String, currentTime: String?): String {+ if (currentTime?.substringBefore('T') == isoDate) return "Today" return runCatching { LocalDate.parse(isoDate).dayOfWeek .getDisplayName(TextStyle.FULL, Locale.getDefault())
weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.ktindex 423fb10c66..6893d6219b 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/ForecastColumn.kt@@ -137,6 +137,7 @@ internal fun ForecastColumn( selectedIsoTime = (selected as? SelectedDateOrTime.Time)?.isoTime, onHourSelected = { actions.toggleTime(it) }, scrollToIsoDate = (selected as? SelectedDateOrTime.Day)?.isoDate,+ nowIsoHour = forecast.current?.time?.take(13)?.let { "$it:00" }, nowEpochSec = nowEpochSec, ) }@@ -146,6 +147,7 @@ internal fun ForecastColumn( tempUnit = units.temperature, selectedIsoDate = (selected as? SelectedDateOrTime.Day)?.isoDate, onDaySelected = { actions.toggleDay(it) },+ todayIsoDate = forecast.current?.time?.substringBefore('T'), ) } if (current != null && resolved != null) {
weather/src/main/java/com/vayunmathur/weather/ui/components/DailyCard.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/DailyCard.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/DailyCard.ktindex 222619e7e6..6cdd565d21 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/DailyCard.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/DailyCard.kt@@ -2,6 +2,7 @@ package com.vayunmathur.weather.ui.components import com.vayunmathur.library.util.DateNameStyle import kotlinx.datetime.isoDayNumber+import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues@@ -12,12 +13,14 @@ import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.widthIn import androidx.compose.foundation.lazy.LazyRow+import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.shape.CircleShape import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.IconCalendar import com.vayunmathur.library.ui.Surface import com.vayunmathur.library.ui.Text import androidx.compose.runtime.Composable+import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color@@ -45,9 +48,18 @@ fun DailyCard( tempUnit: TemperatureUnit, selectedIsoDate: String? = null, onDaySelected: (String) -> Unit = {},+ /** ISO `yyyy-MM-dd` of today in the location's zone; that card gets an outline. */+ todayIsoDate: String? = null, ) { if (daily.time.isEmpty()) return + // Open scrolled so today is visible with history to its left.+ val todayIdx = daily.time.indexOf(todayIsoDate).takeIf { it >= 0 } ?: 0+ val listState = rememberLazyListState()+ LaunchedEffect(todayIdx, daily.time.size) {+ if (todayIdx > 0) listState.scrollToItem((todayIdx - 1).coerceAtLeast(0))+ }+ Surface( color = MaterialTheme.colorScheme.surface, shape = MaterialTheme.shapes.extraLarge,@@ -57,6 +69,7 @@ fun DailyCard( CardsHeader(text = stringResource(R.string.daily_forecast), icon = { m, c -> IconCalendar(m, c) }) Spacer(Modifier.height(14.dp)) LazyRow(+ state = listState, horizontalArrangement = Arrangement.spacedBy(6.dp), contentPadding = PaddingValues(horizontal = 16.dp), ) {@@ -66,15 +79,17 @@ fun DailyCard( val lo = daily.temperatureMin.getOrNull(index) ?: 0.0 val code = daily.weatherCode.getOrNull(index) ?: 0 val precip = daily.precipitationProbabilityMax.getOrNull(index) ?: 0+ val isToday = date != null && date == todayIsoDate DailyItem(- weekday = if (index == 0) stringResource(R.string.today) else dayLabel(date),+ weekday = if (isToday) stringResource(R.string.today) else dayLabel(date), maxTemp = hi, minTemp = lo, icon = weatherConditionForCode(code).iconContent(true), precipitationProbability = precip, tempUnit = tempUnit, isSelected = date != null && date == selectedIsoDate,+ isToday = isToday, onClick = { if (date != null) onDaySelected(date) }, ) }@@ -92,11 +107,13 @@ private fun DailyItem( precipitationProbability: Int, tempUnit: TemperatureUnit, isSelected: Boolean,+ isToday: Boolean, onClick: () -> Unit, ) { Surface( color = if (isSelected) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.surfaceContainer, shape = CircleShape,+ border = if (isToday) BorderStroke(1.dp, MaterialTheme.colorScheme.primary) else null, onClick = onClick, ) { val onColor = if (isSelected) MaterialTheme.colorScheme.onPrimaryContainer else MaterialTheme.colorScheme.onSurface
weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.ktindex 39c697fbfe..02908a5cf7 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/components/HourlyCard.kt@@ -3,6 +3,7 @@ package com.vayunmathur.weather.ui.components import com.vayunmathur.library.util.DateNameStyle import com.vayunmathur.library.util.localizedDayOfWeekNames import kotlinx.datetime.isoDayNumber+import androidx.compose.foundation.border import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box@@ -64,19 +65,19 @@ fun HourlyCard( selectedIsoTime: String? = null, onHourSelected: (String) -> Unit = {}, scrollToIsoDate: String? = null,+ /** ISO `yyyy-MM-ddTHH:00` of the current hour in the location's zone; that cell gets an outline. */+ nowIsoHour: String? = null, /**- * "Now" for the strip: hours before it are dropped and the first surviving cell is- * labelled Now. A parameter rather than a direct clock read so a preview can pin it —- * otherwise fixed sample data ages out and the whole card disappears.+ * "Now" for the strip's day labels and the sun arc. A parameter rather than a+ * direct clock read so a preview can pin it — otherwise fixed sample data ages+ * out and the whole card disappears. */ nowEpochSec: Long = System.currentTimeMillis() / 1000, ) {- val nowSec = nowEpochSec val cells = hourly.time.indices .mapNotNull { i -> val iso = hourly.time.getOrNull(i) ?: return@mapNotNull null val ts = parseLocalIsoToEpochSec(iso, utcOffsetSeconds) ?: return@mapNotNull null- if (ts < nowSec - 3600) return@mapNotNull null HourCell( iso = iso, epochSec = ts,@@ -88,12 +89,17 @@ fun HourlyCard( } if (cells.isEmpty()) return + // Open scrolled so now is visible with history to its left, unless the+ // user picked a day (which takes precedence and jumps to that date).+ val nowIdx = cells.indexOfFirst { it.iso == nowIsoHour }.takeIf { it >= 0 } val listState = rememberLazyListState()- LaunchedEffect(scrollToIsoDate, cells) {- if (scrollToIsoDate != null) {- val target = cells.indexOfFirst { it.iso.substringBefore('T') == scrollToIsoDate }- if (target >= 0) listState.animateScrollToItem(target)+ LaunchedEffect(scrollToIsoDate, nowIdx, cells) {+ val target = if (scrollToIsoDate != null) {+ cells.indexOfFirst { it.iso.substringBefore('T') == scrollToIsoDate }.takeIf { it >= 0 }+ } else {+ nowIdx?.minus(1)?.coerceAtLeast(0) }+ if (target != null && target >= 0) listState.scrollToItem(target) } Surface(@@ -106,12 +112,13 @@ fun HourlyCard( LazyRow(state = listState, contentPadding = PaddingValues(horizontal = 12.dp)) { items(cells.size, key = { "${cells[it].epochSec}_$it" }) { index -> val cell = cells[index]+ val isNow = cell.iso == nowIsoHour HourlyItem(- time = if (index == 0) stringResource(R.string.now) else formatStripHour(cell.epochSec, use24Hour),+ time = if (isNow) stringResource(R.string.now) else formatStripHour(cell.epochSec, use24Hour), dayLabel = formatDayLabel(cell.epochSec, nowEpochSec), precipitationProbability = cell.precip, temperature = cell.temperature,- isNow = index == 0,+ isNow = isNow, isSelected = selectedIsoTime == cell.iso, icon = weatherConditionForCode(cell.weatherCode).iconContent(cell.isDay), tempUnit = tempUnit,@@ -136,12 +143,33 @@ private fun HourlyItem( onClick: () -> Unit, ) { Column(- modifier = Modifier.heightIn(min = 135.dp).widthIn(min = 45.dp).clickable(onClick = onClick),+ modifier = Modifier+ .heightIn(min = 135.dp)+ .widthIn(min = 45.dp)+ .then(+ if (isNow) {+ Modifier+ .padding(1.dp)+ .border(+ width = 1.dp,+ color = MaterialTheme.colorScheme.primary,+ shape = MaterialTheme.shapes.medium,+ )+ .padding(1.dp)+ } else {+ Modifier.padding(2.dp)+ }+ )+ .clickable(onClick = onClick), verticalArrangement = Arrangement.Bottom, horizontalAlignment = Alignment.CenterHorizontally, ) { Spacer(Modifier.height(5.dp))- TempWithShape(temperature = temperature, tempUnit = tempUnit, highlighted = isNow || isSelected)+ TempWithShape(+ temperature = temperature,+ tempUnit = tempUnit,+ highlighted = isNow || isSelected,+ ) Spacer(Modifier.height(2.dp)) Text( "${precipitationProbability}%",@@ -168,7 +196,11 @@ private fun HourlyItem( @OptIn(ExperimentalMaterial3ExpressiveApi::class) @Composable-private fun TempWithShape(temperature: Double, tempUnit: TemperatureUnit, highlighted: Boolean) {+private fun TempWithShape(+ temperature: Double,+ tempUnit: TemperatureUnit,+ highlighted: Boolean,+) { Surface( shape = MaterialShapes.Cookie4Sided.toShape(), modifier = Modifier.size(36.dp),
weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt
diff --git a/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt b/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.ktindex c9eab1721f..024f527a0f 100644--- a/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt+++ b/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt@@ -12,11 +12,11 @@ import com.vayunmathur.weather.network.Current import com.vayunmathur.weather.network.Daily import com.vayunmathur.weather.network.ForecastResponse import com.vayunmathur.weather.network.Hourly-import com.vayunmathur.weather.domain.DisplayUnits import com.vayunmathur.weather.domain.LocationRow import com.vayunmathur.weather.domain.LocationUiState import com.vayunmathur.weather.domain.LocationsUiState import com.vayunmathur.weather.domain.SelectedDateOrTime+import com.vayunmathur.weather.platform.DisplayUnits import com.vayunmathur.weather.platform.WeatherActions /** Phone-shaped, roughly 1080x2340 at xxhdpi — comfortably above the F-Droid minimum. */@@ -30,24 +30,56 @@ private const val UTC_OFFSET_SEC = 3600 /** * Wednesday 15 July 2026, 14:30 local — the epoch second that [NOW_ISO_HOUR] and every- * timestamp below hang off. The hourly strip drops anything more than an hour old and the- * sun block positions its marker from it, so pinning "now" is what stops the sample data- * ageing out and keeps the two images identical from one run to the next.+ * timestamp below hang off. The sun block positions its marker from it, so pinning "now"+ * is what stops the sample data ageing out and keeps the two images identical from one+ * run to the next. */ private const val NOW_EPOCH_SEC = 1_784_122_200L -/** The hour the strip labels "Now"; the first entry of [SAMPLE_HOURLY]. */+/** The hour the strip labels "Now"; [HISTORY_HOURS] entries into [SAMPLE_HOURLY]. */ private const val NOW_ISO_HOUR = "2026-07-15T14:00" -/** The hour preview 2 inspects: 6 PM the same day, five cells into the strip. */+/** The hour preview 2 inspects: 6 PM the same day, four cells after "Now". */ private const val SELECTED_ISO_HOUR = "2026-07-15T18:00" /**- * 24 hours from [NOW_ISO_HOUR] onwards. Open-Meteo returns local-time ISO strings with no- * offset — [UTC_OFFSET_SEC] is what turns them back into instants.+ * Hours of synthetic history prepended to the sample hourly strip, mirroring+ * `past_days` on the real endpoint. Values cycle the 24 h of sample data (a whole+ * cycle, so day/night alignment is preserved); the strip opens scrolled to "Now",+ * so these cells sit off-screen to the left.+ */+private const val HISTORY_HOURS = 48++/** `2026-07-13T14:00` through `2026-07-15T13:00` — the 48 hours before [NOW_ISO_HOUR]. */+private fun historyHourLabels(): List<String> {+ val out = ArrayList<String>(HISTORY_HOURS)+ var day = 13+ var hour = 14+ repeat(HISTORY_HOURS) {+ out += "2026-07-%02dT%02d:00".format(day, hour)+ if (++hour == 24) {+ hour = 0+ day+++ }+ }+ return out+}++/** Cycle [sample] to synthesize [HISTORY_HOURS] of plausible history before it. */+private fun <T> withHistory(sample: List<T>): List<T> {+ val hist = ArrayList<T>(HISTORY_HOURS)+ var i = 0+ repeat(HISTORY_HOURS) { hist += sample[i++ % sample.size] }+ return hist + sample+}++/**+ * [HISTORY_HOURS] of history followed by 24 hours from [NOW_ISO_HOUR] onwards.+ * Open-Meteo returns local-time ISO strings with no offset — [UTC_OFFSET_SEC]+ * is what turns them back into instants. */ private val SAMPLE_HOURLY = Hourly(- time = listOf(+ time = historyHourLabels() + listOf( "2026-07-15T14:00", "2026-07-15T15:00", "2026-07-15T16:00", "2026-07-15T17:00", "2026-07-15T18:00", "2026-07-15T19:00", "2026-07-15T20:00", "2026-07-15T21:00", "2026-07-15T22:00", "2026-07-15T23:00", "2026-07-16T00:00", "2026-07-16T01:00",@@ -55,117 +87,170 @@ private val SAMPLE_HOURLY = Hourly( "2026-07-16T06:00", "2026-07-16T07:00", "2026-07-16T08:00", "2026-07-16T09:00", "2026-07-16T10:00", "2026-07-16T11:00", "2026-07-16T12:00", "2026-07-16T13:00", ),- temperature = listOf(- 21.8, 22.1, 22.4, 22.0, 21.2, 20.0, 18.6, 17.4,- 16.5, 15.9, 15.4, 15.0, 14.6, 14.3, 14.0, 13.8,- 14.4, 15.7, 17.2, 18.8, 20.1, 21.2, 22.0, 22.5,+ temperature = withHistory(+ listOf(+ 21.8, 22.1, 22.4, 22.0, 21.2, 20.0, 18.6, 17.4,+ 16.5, 15.9, 15.4, 15.0, 14.6, 14.3, 14.0, 13.8,+ 14.4, 15.7, 17.2, 18.8, 20.1, 21.2, 22.0, 22.5,+ ), ),- apparentTemperature = listOf(- 21.1, 21.4, 21.7, 21.3, 20.5, 19.3, 17.9, 16.7,- 15.8, 15.2, 14.7, 14.3, 13.9, 13.6, 13.3, 13.1,- 13.7, 15.0, 16.5, 18.1, 19.4, 20.5, 21.3, 21.8,+ apparentTemperature = withHistory(+ listOf(+ 21.1, 21.4, 21.7, 21.3, 20.5, 19.3, 17.9, 16.7,+ 15.8, 15.2, 14.7, 14.3, 13.9, 13.6, 13.3, 13.1,+ 13.7, 15.0, 16.5, 18.1, 19.4, 20.5, 21.3, 21.8,+ ), ),- relativeHumidity = listOf(- 58, 56, 55, 57, 60, 65, 70, 74,- 78, 81, 83, 85, 86, 87, 88, 89,- 87, 82, 76, 70, 64, 60, 57, 55,+ relativeHumidity = withHistory(+ listOf(+ 58, 56, 55, 57, 60, 65, 70, 74,+ 78, 81, 83, 85, 86, 87, 88, 89,+ 87, 82, 76, 70, 64, 60, 57, 55,+ ), ),- dewPoint = listOf(- 13.4, 13.2, 13.3, 13.4, 13.5, 13.6, 13.4, 13.2,- 13.0, 12.9, 12.8, 12.7, 12.6, 12.5, 12.4, 12.3,- 12.5, 12.8, 13.1, 13.3, 13.5, 13.6, 13.5, 13.4,+ dewPoint = withHistory(+ listOf(+ 13.4, 13.2, 13.3, 13.4, 13.5, 13.6, 13.4, 13.2,+ 13.0, 12.9, 12.8, 12.7, 12.6, 12.5, 12.4, 12.3,+ 12.5, 12.8, 13.1, 13.3, 13.5, 13.6, 13.5, 13.4,+ ), ),- weatherCode = listOf(- 2, 2, 1, 1, 2, 3, 3, 2,- 2, 1, 1, 2, 3, 3, 45, 45,- 3, 2, 1, 1, 0, 0, 1, 2,+ weatherCode = withHistory(+ listOf(+ 2, 2, 1, 1, 2, 3, 3, 2,+ 2, 1, 1, 2, 3, 3, 45, 45,+ 3, 2, 1, 1, 0, 0, 1, 2,+ ), ),- precipitationProbability = listOf(- 5, 5, 0, 0, 5, 10, 15, 10,- 5, 5, 10, 15, 20, 25, 30, 25,- 15, 10, 5, 0, 0, 0, 5, 10,+ precipitationProbability = withHistory(+ listOf(+ 5, 5, 0, 0, 5, 10, 15, 10,+ 5, 5, 10, 15, 20, 25, 30, 25,+ 15, 10, 5, 0, 0, 0, 5, 10,+ ), ),- precipitation = listOf(- 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,- 0.0, 0.0, 0.0, 0.0, 0.1, 0.2, 0.3, 0.2,- 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,+ precipitation = withHistory(+ listOf(+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,+ 0.0, 0.0, 0.0, 0.0, 0.1, 0.2, 0.3, 0.2,+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,+ ), ),- windSpeed = listOf(- 16.4, 17.2, 17.8, 17.1, 15.9, 14.3, 12.8, 11.6,- 10.7, 10.1, 9.6, 9.2, 8.9, 8.6, 8.4, 8.7,- 9.5, 11.0, 12.9, 14.6, 16.1, 17.3, 18.0, 18.4,+ windSpeed = withHistory(+ listOf(+ 16.4, 17.2, 17.8, 17.1, 15.9, 14.3, 12.8, 11.6,+ 10.7, 10.1, 9.6, 9.2, 8.9, 8.6, 8.4, 8.7,+ 9.5, 11.0, 12.9, 14.6, 16.1, 17.3, 18.0, 18.4,+ ), ),- windDirection = listOf(- 236, 240, 244, 246, 244, 240, 234, 228,- 222, 218, 214, 210, 208, 206, 208, 214,- 222, 230, 236, 242, 246, 250, 252, 254,+ windDirection = withHistory(+ listOf(+ 236, 240, 244, 246, 244, 240, 234, 228,+ 222, 218, 214, 210, 208, 206, 208, 214,+ 222, 230, 236, 242, 246, 250, 252, 254,+ ), ),- pressureMsl = listOf(- 1015.2, 1015.0, 1014.8, 1014.7, 1014.9, 1015.2, 1015.6, 1016.0,- 1016.3, 1016.5, 1016.7, 1016.8, 1016.8, 1016.7, 1016.5, 1016.3,- 1016.0, 1015.7, 1015.4, 1015.0, 1014.7, 1014.4, 1014.2, 1014.1,+ pressureMsl = withHistory(+ listOf(+ 1015.2, 1015.0, 1014.8, 1014.7, 1014.9, 1015.2, 1015.6, 1016.0,+ 1016.3, 1016.5, 1016.7, 1016.8, 1016.8, 1016.7, 1016.5, 1016.3,+ 1016.0, 1015.7, 1015.4, 1015.0, 1014.7, 1014.4, 1014.2, 1014.1,+ ), ),- visibility = listOf(- 24140.0, 24140.0, 24140.0, 24140.0, 22000.0, 20000.0, 18000.0, 16000.0,- 14000.0, 12000.0, 11000.0, 10000.0, 8000.0, 6000.0, 2500.0, 1800.0,- 5000.0, 12000.0, 18000.0, 22000.0, 24140.0, 24140.0, 24140.0, 24140.0,+ visibility = withHistory(+ listOf(+ 24140.0, 24140.0, 24140.0, 24140.0, 22000.0, 20000.0, 18000.0, 16000.0,+ 14000.0, 12000.0, 11000.0, 10000.0, 8000.0, 6000.0, 2500.0, 1800.0,+ 5000.0, 12000.0, 18000.0, 22000.0, 24140.0, 24140.0, 24140.0, 24140.0,+ ), ),- cloudCover = listOf(- 34, 30, 22, 18, 28, 44, 58, 46,- 38, 26, 30, 42, 60, 72, 90, 94,- 78, 55, 32, 20, 10, 8, 16, 30,+ cloudCover = withHistory(+ listOf(+ 34, 30, 22, 18, 28, 44, 58, 46,+ 38, 26, 30, 42, 60, 72, 90, 94,+ 78, 55, 32, 20, 10, 8, 16, 30,+ ), ),- windGusts = listOf(- 27.8, 29.1, 30.2, 29.0, 27.0, 24.3, 21.7, 19.6,- 18.1, 17.1, 16.3, 15.6, 15.1, 14.6, 14.3, 14.8,- 16.1, 18.7, 21.9, 24.8, 27.3, 29.4, 30.6, 31.2,+ windGusts = withHistory(+ listOf(+ 27.8, 29.1, 30.2, 29.0, 27.0, 24.3, 21.7, 19.6,+ 18.1, 17.1, 16.3, 15.6, 15.1, 14.6, 14.3, 14.8,+ 16.1, 18.7, 21.9, 24.8, 27.3, 29.4, 30.6, 31.2,+ ), ),- uvIndex = listOf(- 5.2, 4.3, 3.2, 2.1, 1.2, 0.5, 0.1, 0.0,- 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,- 0.3, 0.9, 1.8, 2.9, 4.1, 5.2, 6.0, 6.3,+ uvIndex = withHistory(+ listOf(+ 5.2, 4.3, 3.2, 2.1, 1.2, 0.5, 0.1, 0.0,+ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,+ 0.3, 0.9, 1.8, 2.9, 4.1, 5.2, 6.0, 6.3,+ ), ), // Sunset is 21:07 and sunrise 05:05, so 22:00 through 05:00 are the night hours.- isDay = listOf(- 1, 1, 1, 1, 1, 1, 1, 1,- 0, 0, 0, 0, 0, 0, 0, 0,- 1, 1, 1, 1, 1, 1, 1, 1,+ isDay = withHistory(+ listOf(+ 1, 1, 1, 1, 1, 1, 1, 1,+ 0, 0, 0, 0, 0, 0, 0, 0,+ 1, 1, 1, 1, 1, 1, 1, 1,+ ), ), ) -/** A week from the sample date, Wednesday through the following Tuesday. */+/**+ * Two weeks ending the Tuesday after the sample date: seven days of history+ * followed by the forward week, so the sample "today" (2026-07-15) sits at+ * index 7, mirroring a real `past_days=7` response.+ */ private val SAMPLE_DAILY = Daily( time = listOf(+ "2026-07-08", "2026-07-09", "2026-07-10", "2026-07-11",+ "2026-07-12", "2026-07-13", "2026-07-14", "2026-07-15", "2026-07-16", "2026-07-17", "2026-07-18", "2026-07-19", "2026-07-20", "2026-07-21", ),- weatherCode = listOf(2, 45, 1, 0, 0, 61, 3),- temperatureMax = listOf(22.6, 23.4, 24.8, 26.1, 24.9, 20.7, 19.8),- temperatureMin = listOf(13.8, 14.5, 15.2, 16.0, 15.4, 14.1, 13.2),- apparentTemperatureMax = listOf(21.9, 22.7, 24.1, 25.5, 24.2, 20.0, 19.0),- apparentTemperatureMin = listOf(13.1, 13.8, 14.5, 15.3, 14.7, 13.4, 12.5),+ weatherCode = listOf(3, 2, 61, 45, 0, 1, 2, 2, 45, 1, 0, 0, 61, 3),+ temperatureMax = listOf(19.5, 20.8, 18.9, 21.7, 23.0, 24.1, 22.2, 22.6, 23.4, 24.8, 26.1, 24.9, 20.7, 19.8),+ temperatureMin = listOf(12.9, 13.5, 13.0, 14.0, 14.8, 15.0, 13.6, 13.8, 14.5, 15.2, 16.0, 15.4, 14.1, 13.2),+ apparentTemperatureMax = listOf(18.8, 20.1, 18.2, 21.0, 22.3, 23.4, 21.5, 21.9, 22.7, 24.1, 25.5, 24.2, 20.0, 19.0),+ apparentTemperatureMin = listOf(12.2, 12.8, 12.3, 13.3, 14.1, 14.3, 12.9, 13.1, 13.8, 14.5, 15.3, 14.7, 13.4, 12.5), sunrise = listOf(+ "2026-07-08T04:59", "2026-07-09T05:00", "2026-07-10T05:01", "2026-07-11T05:02",+ "2026-07-12T05:03", "2026-07-13T05:04", "2026-07-14T05:04", "2026-07-15T05:05", "2026-07-16T05:06", "2026-07-17T05:08", "2026-07-18T05:09", "2026-07-19T05:10", "2026-07-20T05:12", "2026-07-21T05:13", ), sunset = listOf(+ "2026-07-08T21:12", "2026-07-09T21:11", "2026-07-10T21:10", "2026-07-11T21:09",+ "2026-07-12T21:08", "2026-07-13T21:08", "2026-07-14T21:07", "2026-07-15T21:07", "2026-07-16T21:06", "2026-07-17T21:05", "2026-07-18T21:03", "2026-07-19T21:02", "2026-07-20T21:01", "2026-07-21T20:59", ),- daylightDuration = listOf(57720.0, 57600.0, 57420.0, 57240.0, 57120.0, 56940.0, 56760.0),- sunshineDuration = listOf(43200.0, 25200.0, 47000.0, 50400.0, 48600.0, 19800.0, 34200.0),- uvIndexMax = listOf(6.4, 5.7, 6.8, 7.2, 7.0, 4.4, 5.6),- precipitationProbabilityMax = listOf(15, 30, 5, 0, 0, 70, 25),- precipitationSum = listOf(0.0, 0.8, 0.0, 0.0, 0.0, 5.4, 0.4),+ daylightDuration = listOf(+ 58380.0, 58260.0, 58140.0, 58020.0, 57900.0, 57840.0, 57780.0,+ 57720.0, 57600.0, 57420.0, 57240.0, 57120.0, 56940.0, 56760.0,+ ),+ sunshineDuration = listOf(+ 36000.0, 41400.0, 12600.0, 45000.0, 46800.0, 44100.0, 42300.0,+ 43200.0, 25200.0, 47000.0, 50400.0, 48600.0, 19800.0, 34200.0,+ ),+ uvIndexMax = listOf(5.9, 6.1, 3.8, 6.6, 6.9, 7.1, 6.2, 6.4, 5.7, 6.8, 7.2, 7.0, 4.4, 5.6),+ precipitationProbabilityMax = listOf(20, 10, 65, 25, 5, 0, 10, 15, 30, 5, 0, 0, 70, 25),+ precipitationSum = listOf(0.2, 0.0, 4.1, 0.5, 0.0, 0.0, 0.1, 0.0, 0.8, 0.0, 0.0, 0.0, 5.4, 0.4), // A real Open-Meteo lunation (waxing gibbous through full), relabelled onto- // the sample week. The moon is a 2%-lit sliver on these actual dates, which+ // the sample fortnight. The moon is a 2%-lit sliver on these actual dates, which // would render as an all-but-invisible disc in the store screenshot.- moonPhase = listOf(0.318, 0.35, 0.381, 0.412, 0.443, 0.474, 0.504),+ moonPhase = listOf(+ 0.10, 0.135, 0.169, 0.203, 0.235, 0.264, 0.292,+ 0.318, 0.35, 0.381, 0.412, 0.443, 0.474, 0.504,+ ), moonrise = listOf(+ "2026-07-08T06:58", "2026-07-09T08:06", "2026-07-10T09:19", "2026-07-11T10:33",+ "2026-07-12T11:47", "2026-07-13T12:52", "2026-07-14T13:06", "2026-07-15T13:52", "2026-07-16T15:06", "2026-07-17T16:19", "2026-07-18T17:33", "2026-07-19T18:47", "2026-07-20T19:59", "2026-07-21T21:04", ), moonset = listOf(+ "2026-07-08T00:58", "2026-07-09T01:06", "2026-07-10T01:14", "2026-07-11T01:19",+ "2026-07-12T01:22", "2026-07-13T01:24", "2026-07-14T01:26", "2026-07-15T01:27", "2026-07-16T01:38", "2026-07-17T01:49", "2026-07-18T02:02", "2026-07-19T02:17", "2026-07-20T02:38", "2026-07-21T03:07", ),
weather/src/test/java/com/vayunmathur/weather/domain/ForecastIdentityTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/domain/ForecastIdentityTest.kt b/weather/src/test/java/com/vayunmathur/weather/domain/ForecastIdentityTest.ktnew file mode 100644index 0000000000..269c54d240--- /dev/null+++ b/weather/src/test/java/com/vayunmathur/weather/domain/ForecastIdentityTest.kt@@ -0,0 +1,65 @@+package com.vayunmathur.weather.domain++import com.vayunmathur.weather.network.Daily+import com.vayunmathur.weather.network.ForecastResponse+import kotlin.test.Test+import kotlin.test.assertEquals++class ForecastIdentityTest {++ private val daily = Daily(+ time = listOf(+ "2026-07-08", "2026-07-09", "2026-07-10", "2026-07-11",+ "2026-07-12", "2026-07-13", "2026-07-14",+ "2026-07-15", "2026-07-16", "2026-07-17",+ ),+ )++ @Test+ fun todayIndex_matchesCurrentTimeDate() {+ assertEquals(7, todayIndex(daily, "2026-07-15T14:30"))+ }++ @Test+ fun todayIndex_fallsBackToZeroForUnknownDate() {+ assertEquals(0, todayIndex(daily, "2026-07-30T14:30"))+ }++ @Test+ fun todayIndex_emptyDaily_returnsZero() {+ assertEquals(0, todayIndex(Daily(), "2026-07-15T14:30"))+ assertEquals(0, todayIndex(null, "2026-07-15T14:30"))+ }++ @Test+ fun nowIndex_matchesCurrentHour() {+ val hourly = com.vayunmathur.weather.network.Hourly(+ time = listOf("2026-07-15T13:00", "2026-07-15T14:00", "2026-07-15T15:00"),+ )+ assertEquals(1, nowIndex(hourly, "2026-07-15T14:30"))+ }++ @Test+ fun nowIndex_emptyHourly_returnsZero() {+ assertEquals(0, nowIndex(com.vayunmathur.weather.network.Hourly(), "2026-07-15T14:30"))+ assertEquals(0, nowIndex(null, "2026-07-15T14:30"))+ }++ @Test+ fun todayIsoDate_prefersCurrentTime() {+ val forecast = ForecastResponse(+ latitude = 0.0,+ longitude = 0.0,+ current = com.vayunmathur.weather.network.Current(+ time = "2026-07-15T14:30",+ temperature = 0.0,+ apparentTemperature = 0.0,+ relativeHumidity = 0,+ weatherCode = 0,+ windSpeed = 0.0,+ windDirection = 0,+ ),+ )+ assertEquals("2026-07-15", todayIsoDate(forecast))+ }+}