Modern Apps git

← Modern-Apps.git

health: label month/year history by bucket date, not position

Commit
c83cac7444fbf22afa13dc0a336fec81982e0116
Author
Vayun Mathur <[email protected]>
When
2026-11-07 19:57 UTC (28 days ago)
Parents
0b8c03d
Symptom: on Month/Year tabs, history rows for sparse data were labelled
as if periods were consecutive from the range start (first row always
the 1st / January), and Month-tab weekly buckets were labelled as
consecutive days. Values sat next to wrong dates.

Root cause: buildHistoryItems labelled rawPairsHistory by mapIndexed
position via historyLabel(startTime + index), while the DAO only returns
periods that have records. The week tab already fixed this class (#727);
month/year history did not.

Fix: label each row from its own bucket key (triple.first epoch days:
week start on Month tab, month start on Year tab) via historyLabelForKey.

Verification: new HistoryBucketLabelTest pins bucket-key -> date;
full test/compile run after all fixes.

Diff

health/src/main/java/com/vayunmathur/health/util/HealthChartData.kt
diff --git a/health/src/main/java/com/vayunmathur/health/util/HealthChartData.kt b/health/src/main/java/com/vayunmathur/health/util/HealthChartData.ktindex c3cd31d42d..64034715f3 100644--- a/health/src/main/java/com/vayunmathur/health/util/HealthChartData.kt+++ b/health/src/main/java/com/vayunmathur/health/util/HealthChartData.kt@@ -192,9 +192,12 @@ internal object HealthChartData {                 )             }.reversed()         } else if (selectedTab != 0) {-            rawPairsHistory.mapIndexed { index, triple ->+            // Label from the row's own bucket key, not its position: DAO rows+            // only exist for periods with data, so position mislabels sparse+            // months/years (same class as the fixed week bug #727).+            rawPairsHistory.map { triple ->                 HistoryItem(-                    label = historyLabel(selectedTab, startTime, tz, resources, index),+                    label = historyLabelForKey(selectedTab, triple.first, resources),                     value = triple.second,                     secondaryValue = if (config.isDualSeries) triple.third else null,                     unit = config.unit,@@ -207,31 +210,23 @@ internal object HealthChartData {         }     } -    private fun historyLabel(+    /** The calendar date a bucket key names (pure; label rendering needs Resources). */+    internal fun bucketDate(epochDay: Long): LocalDate = LocalDate.fromEpochDays(epochDay.toInt())++    /** History label from a bucket's epoch-day key (week/month start for tabs 2/3). */+    internal fun historyLabelForKey(         selectedTab: Int,-        startTime: kotlin.time.Instant,-        tz: TimeZone,+        epochDay: Long,         resources: Resources,-        index: Int,-    ): String = when (selectedTab) {-        0 -> ""-        1 -> localizedDayOfWeekNames(DateNameStyle.FULL)[-            startTime.plus(index.toLong(), DateTimeUnit.DAY, tz)-                .toLocalDateTime(tz).dayOfWeek.isoDayNumber - 1-        ]-        2 -> {-            val date = startTime.plus(index.toLong(), DateTimeUnit.DAY, tz)-                .toLocalDateTime(tz).date-            resources.getString(+    ): String {+        val date = bucketDate(epochDay)+        return when (selectedTab) {+            2 -> resources.getString(                 R.string.month_year_format,                 localizedMonthNames(DateNameStyle.SHORT)[date.month.number - 1],                 date.day,             )-        }-        else -> {-            val date = startTime.plus(index.toLong(), DateTimeUnit.MONTH, tz)-                .toLocalDateTime(tz).date-            localizedMonthNames(DateNameStyle.FULL)[date.month.number - 1]+            else -> localizedMonthNames(DateNameStyle.FULL)[date.month.number - 1]         }     } 
health/src/test/java/com/vayunmathur/health/util/HistoryBucketLabelTest.kt
diff --git a/health/src/test/java/com/vayunmathur/health/util/HistoryBucketLabelTest.kt b/health/src/test/java/com/vayunmathur/health/util/HistoryBucketLabelTest.ktnew file mode 100644index 0000000000..7bd3ac5ed7--- /dev/null+++ b/health/src/test/java/com/vayunmathur/health/util/HistoryBucketLabelTest.kt@@ -0,0 +1,22 @@+package com.vayunmathur.health.util++import kotlinx.datetime.LocalDate+import kotlin.test.Test+import kotlin.test.assertEquals++/** Sparse history rows must be labelled by their bucket date, not position. */+class HistoryBucketLabelTest {+    @Test+    fun bucketKeyResolvesToItsOwnDate() {+        // Only Mar 15 and Jun 1 have data: position-based labels would call+        // them Mar 1 / Mar 2; bucket keys keep their real dates.+        assertEquals(+            LocalDate(2026, 3, 15),+            HealthChartData.bucketDate(LocalDate(2026, 3, 15).toEpochDays().toLong()),+        )+        assertEquals(+            LocalDate(2026, 6, 1),+            HealthChartData.bucketDate(LocalDate(2026, 6, 1).toEpochDays().toLong()),+        )+    }+}