commit 12db7c5de690a7faccffccb17063b3495643c5e0
parent 1c88f294f1f3236240a794fa33a1488562ea4313
Author: Michael Camilleri <[email protected]>
Date: Tue, 20 Oct 2026 08:23:22 +0900
Show sleep or steps in the timeline's context line
This commit adds a Health reading to the end of each report's context
line in the timeline, after the place and the weather. A wake report
shows the night's time asleep and other reports show the steps taken so far
that day.
Health can be read again after a report is saved, which changes the
report's HealthEntity but not the report itself, so a row with an entry
now observes the entry as well and redraws when it changes. The
timeline's fetch prefetches the entries along with the responses.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat
| M | Notes/Design.md | | | 6 | ++++-- |
| M | Reportage/Views/Home/HomeView.swift | | | 25 | +++++++++++++++++++++++-- |
| M | Reportage/Views/Home/ReportSummary.swift | | | 31 | +++++++++++++++++++++++++++---- |
| M | Tests/Unit/ReportSummaryTests.swift | | | 16 | ++++++++++++++++ |
4 files changed, 70 insertions(+), 8 deletions(-)
diff --git a/Notes/Design.md b/Notes/Design.md
@@ -95,8 +95,10 @@ The look is called Almanac: ink on paper, like a printed journal.
shows its own local time (the period beneath on a 12-hour clock, so times
fit one column), its kind, its entry, any answers the entry doesn't cover,
and a line of context: the city with its region ('Suginami, Tokyo', put
- together from the locality and region for imported placemarks) and the
- weather. Days are grouped in the device's time zone: grouping by each
+ together from the locality and region for imported placemarks), the
+ weather, and Health: time asleep on a wake report, and steps so far that
+ day on the rest, with Reporter's step count standing in for a report
+ Health hasn't filled in. Days are grouped in the device's time zone: grouping by each
report's own zone would put reports out of order around midnight after a
change of zone, which a sectioned fetch can't show. Grouping by a computed
day loads every report the fetch covers, and regroups them all whenever one
diff --git a/Reportage/Views/Home/HomeView.swift b/Reportage/Views/Home/HomeView.swift
@@ -88,7 +88,10 @@ final class ReportDescriber {
.sorted { $0.order < $1.order }
.compactMap { try? $0.response() }
let context = report.context.flatMap { try? ModelCoding.decode(Context.self, from: $0) }
- return ReportSummary(responses: responses, context: context ?? Context(), lookup: lookup)
+ return ReportSummary(
+ responses: responses, context: context ?? Context(),
+ health: try? report.health?.record().health,
+ kind: report.kind.flatMap(ReportKind.init), lookup: lookup)
}
}
@@ -140,7 +143,7 @@ struct ReportList: View {
request.predicate = NSPredicate(value: false)
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]
request.fetchBatchSize = 40
- request.relationshipKeyPathsForPrefetching = ["responses"]
+ request.relationshipKeyPathsForPrefetching = ["responses", "health"]
return request
}
@@ -336,6 +339,16 @@ struct ReportRow: View {
@ScaledMetric(relativeTo: .title3) private var timeWidth: CGFloat = 60
var body: some View {
+ if let health = report.health {
+ // Reading Health again changes the entry but not the report, so
+ // the row watches the entry as well.
+ HealthObserver(health: health) { content }
+ } else {
+ content
+ }
+ }
+
+ @ViewBuilder private var content: some View {
let summary = describer.summary(of: report)
let kind = report.kind.flatMap(ReportKind.init)
// The tops of the time's digits and of the kind label line up,
@@ -360,6 +373,14 @@ struct ReportRow: View {
private var isLogged: Bool { report.state == ReportState.passive.rawValue }
}
+/// Redraws its content when a report's Health entry changes.
+private struct HealthObserver<Content: View>: View {
+ @ObservedObject var health: HealthEntity
+ @ViewBuilder let content: () -> Content
+
+ var body: some View { content() }
+}
+
/// A report's local time. On a 12-hour clock the period goes beneath the
/// digits, so every time fits the same narrow column.
struct ReportTime: View {
diff --git a/Reportage/Views/Home/ReportSummary.swift b/Reportage/Views/Home/ReportSummary.swift
@@ -27,13 +27,17 @@ struct ReportSummary: Equatable {
var details: [AnswerDetail]
var context: String?
- init(responses: [Response], context: Context, lookup: SummaryLookup) {
+ init(
+ responses: [Response], context: Context, health: HealthContext? = nil, kind: ReportKind? = nil,
+ lookup: SummaryLookup
+ ) {
(entry, details) = EntryBuilder.build(responses: responses, lookup: lookup)
- self.context = Self.contextLine(context)
+ self.context = Self.contextLine(context, health: health, kind: kind)
}
- /// Where and the weather, for example 'Suginami, Tokyo · 18° Cloudy'.
- static func contextLine(_ context: Context) -> String? {
+ /// Where, the weather and Health, for example 'Suginami, Tokyo · 18°
+ /// Cloudy · 4,321 steps'.
+ static func contextLine(_ context: Context, health: HealthContext? = nil, kind: ReportKind? = nil) -> String? {
var parts: [String] = []
if let locality = context.location?.placemark?.displayLocality {
parts.append(locality)
@@ -54,8 +58,27 @@ struct ReportSummary: Equatable {
}
if !reading.isEmpty { parts.append(reading.joined(separator: " ")) }
}
+ if let health = healthReading(context, health: health, kind: kind) {
+ parts.append(health)
+ }
return parts.isEmpty ? nil : parts.joined(separator: " · ")
}
+
+ /// The night's sleep on a wake report, since steps just after waking
+ /// say little, and steps so far that day on the rest. Reporter's own
+ /// step count stands in for a report Health hasn't filled in, as it
+ /// does for the imported archive.
+ private static func healthReading(_ context: Context, health: HealthContext?, kind: ReportKind?) -> String? {
+ if kind == .wake, let sleep = health?.sleep {
+ let duration = Duration.seconds((sleep.asleep / 60).rounded() * 60)
+ .formatted(.units(allowed: [.hours, .minutes], width: .narrow))
+ return String(localized: "\(duration) asleep", comment: "Time asleep, in a timeline row")
+ }
+ guard let steps = health?.activity?.steps ?? context.motion?.steps, steps > 0 else { return nil }
+ return steps == 1
+ ? String(localized: "1 step", comment: "A step count, in a timeline row")
+ : String(localized: "\(steps.formatted()) steps", comment: "A step count, in a timeline row")
+ }
}
extension ReportStore {
diff --git a/Tests/Unit/ReportSummaryTests.swift b/Tests/Unit/ReportSummaryTests.swift
@@ -215,4 +215,20 @@ struct ReportSummaryTests {
#expect(line?.hasPrefix("Musashino, Tokyo · ") == true)
#expect(line?.hasSuffix(" Cloudy") == true)
}
+
+ @Test("Health adds steps, or sleep on a wake report, with Reporter's steps standing in")
+ func healthReading() {
+ var context = Context()
+ context.motion = MotionContext(steps: 1200)
+ #expect(ReportSummary.contextLine(context) == "1,200 steps")
+
+ let health = HealthContext(
+ activity: ActivityHealth(steps: 4321),
+ sleep: SleepHealth(start: .now, end: .now, asleep: 7 * 3600 + 12 * 60 + 20))
+ #expect(ReportSummary.contextLine(context, health: health, kind: .random) == "4,321 steps")
+ #expect(ReportSummary.contextLine(context, health: health, kind: .wake) == "7h 12m asleep")
+
+ context.motion = MotionContext(steps: 0)
+ #expect(ReportSummary.contextLine(context) == nil)
+ }
}