health: fix week view mislabeling sparse days (#727)
- Commit
2aedfc8c176a63a29db81d4a83565c8f26924aae- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-16 22:00 UTC (2 months ago)
- Parents
2dd8ad2
Diff
health/src/main/java/com/vayunmathur/health/util/HealthViewModel.kt
diff --git a/health/src/main/java/com/vayunmathur/health/util/HealthViewModel.kt b/health/src/main/java/com/vayunmathur/health/util/HealthViewModel.ktindex 6abca68004..d3561f76e6 100644--- a/health/src/main/java/com/vayunmathur/health/util/HealthViewModel.kt+++ b/health/src/main/java/com/vayunmathur/health/util/HealthViewModel.kt@@ -271,14 +271,54 @@ class HealthViewModel( HealthAPI.getListOfSums(config.recordType, startTime, endTimeNow, periodType2) } - val mappedChart = rawPairs.map { p ->+ // Week tab must always show 7 Sunday..Saturday slots. The DAO only+ // returns days that have records, so mapping it directly collapses+ // the series left: Tuesday's bar lands in Sunday's slot (#727).+ // Missing days become null (bar chart renders zero, line chart a+ // gap) so each value keeps its own weekday slot.+ val weekSeries = if (selectedTab == 1) {+ fillWeekSeries(+ startDate,+ rawPairs.associate { it.first to (it.second to it.third) },+ )+ } else null+ val mappedChart: List<Pair<String, Double?>> = weekSeries?.map { (date, value, _) ->+ labelFor(selectedTab, date.toEpochDays().toLong()) to value+ } ?: rawPairs.map { p -> labelFor(selectedTab, p.first) to p.second }- val mappedSecondaryChart = if (config.isDualSeries) {- rawPairs.map { p -> labelFor(selectedTab, p.first) to p.third }+ val mappedSecondaryChart: List<Pair<String, Double?>>? = if (config.isDualSeries) {+ weekSeries?.map { (date, _, secondary) ->+ labelFor(selectedTab, date.toEpochDays().toLong()) to secondary+ } ?: rawPairs.map { p -> labelFor(selectedTab, p.first) to p.third } } else null - val history = if (selectedTab != 0) rawPairsHistory.mapIndexed { index, triple ->+ // Same collapse bug as the chart: label each row by its own date,+ // not by position. Bar metrics show all 7 days (missing = 0);+ // line metrics list only days with readings so gaps don't+ // drag the average to zero.+ val weekHistorySeries = if (selectedTab == 1) {+ fillWeekSeries(+ startDate,+ rawPairsHistory.associate { it.first to (it.second to it.third) },+ )+ } else null+ val history = if (selectedTab == 1 && weekHistorySeries != null) {+ weekHistorySeries.mapNotNull { (date, value, secondary) ->+ if (value == null && config.isLineChart) return@mapNotNull null+ val label = localizedDayOfWeekNames(DateNameStyle.FULL)[+ date.dayOfWeek.isoDayNumber - 1+ ]+ HistoryItem(+ label = label,+ value = value ?: 0.0,+ secondaryValue = if (config.isDualSeries) secondary else null,+ unit = config.unit,+ isGoalMet = (value ?: 0.0) >= config.dailyGoal,+ useDecimals = config.useDecimals,+ )+ }.reversed()+ } else if (selectedTab != 0) rawPairsHistory.mapIndexed { index, triple -> val label = when (selectedTab) { 0 -> "" 1 -> localizedDayOfWeekNames(DateNameStyle.FULL)[@@ -327,7 +367,9 @@ class HealthViewModel( chartData = mappedChart, secondaryChartData = mappedSecondaryChart, historyItems = history,- totalBarCount = rawPairs.size,+ // Fixed 7 for the week tab: spacing math must divide by all 7+ // slots even when only some days have data.+ totalBarCount = if (selectedTab == 1) 7 else rawPairs.size, primaryRange = mappedChart.mapNotNull { it.second }.let { vals -> if (vals.isEmpty()) null else vals.minOrNull()!!.let { min ->@@ -593,6 +635,21 @@ class HealthViewModel( companion object { private const val TAG = "HealthViewModel"++ /**+ * Week-tab slot fill: returns all 7 Sunday..Saturday days of the week+ * containing [weekStart], each paired with its value (or null when that+ * day has no records). DAO rows are keyed by epoch day.+ */+ internal fun fillWeekSeries(+ weekStart: LocalDate,+ valuesByEpochDay: Map<Long, Pair<Double?, Double?>>,+ ): List<Triple<LocalDate, Double?, Double?>> =+ (0..6).map { offset ->+ val date = weekStart.plus(offset, DateTimeUnit.DAY)+ val pair = valuesByEpochDay[date.toEpochDays().toLong()]+ Triple(date, pair?.first, pair?.second)+ } } }
health/src/test/java/com/vayunmathur/health/util/WeekBucketsTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/WeekBucketsTest.kt b/health/src/test/java/com/vayunmathur/health/util/WeekBucketsTest.ktnew file mode 100644index 0000000000..5229aa96fc--- /dev/null+++ b/health/src/test/java/com/vayunmathur/health/util/WeekBucketsTest.kt@@ -0,0 +1,68 @@+package com.vayunmathur.health.util++import kotlinx.datetime.LocalDate+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * Regression test for #727: the week view collapsed sparse data left, so a+ * Tuesday-start week labeled Tuesday's bar as Sunday. [HealthViewModel] now+ * fills all 7 slots; this pins the fill behavior (null gaps, correct days).+ */+class WeekBucketsTest {++ // 2026-09-13 is a Sunday; the issue's tracking started Tuesday 2026-09-15.+ private val weekStart = LocalDate(2026, 9, 13)+ private val tuesday = LocalDate(2026, 9, 15)+ private val wednesday = LocalDate(2026, 9, 16)++ private fun byEpoch(vararg pairs: Pair<LocalDate, Double>): Map<Long, Pair<Double?, Double?>> =+ pairs.associate { (date, value) ->+ date.toEpochDays().toLong() to (value to value)+ }++ @Test+ fun sparseWeekKeepsEachDayInItsOwnSlot() {+ val series = HealthViewModel.fillWeekSeries(+ weekStart,+ byEpoch(tuesday to 5000.0, wednesday to 8000.0),+ )+ assertEquals(7, series.size)+ // Sun/Mon empty, Tue/Wed hold the values, Thu..Sat empty.+ assertNull(series[0].second)+ assertNull(series[1].second)+ assertEquals(tuesday, series[2].first)+ assertEquals(5000.0, series[2].second)+ assertEquals(wednesday, series[3].first)+ assertEquals(8000.0, series[3].second)+ assertNull(series[4].second)+ assertNull(series[5].second)+ assertNull(series[6].second)+ }++ @Test+ fun fullWeekPreservesOrderAndValues() {+ val values = (0..6).associate { offset ->+ val date = LocalDate(2026, 9, 13 + offset)+ date.toEpochDays().toLong() to ((1000.0 + offset) to (1000.0 + offset))+ }+ val series = HealthViewModel.fillWeekSeries(weekStart, values)+ assertEquals(7, series.size)+ series.forEachIndexed { index, (date, value, _) ->+ assertEquals(LocalDate(2026, 9, 13 + index), date)+ assertEquals(1000.0 + index, value)+ }+ }++ @Test+ fun emptyWeekYieldsSevenNullSlots() {+ val series = HealthViewModel.fillWeekSeries(weekStart, emptyMap())+ assertEquals(7, series.size)+ series.forEachIndexed { index, (date, value, secondary) ->+ assertEquals(LocalDate(2026, 9, 13 + index), date)+ assertNull(value)+ assertNull(secondary)+ }+ }+}