Reportage/Views/Charts/ContextCharts.swift (8475B)
1import SwiftUI
2
3
4enum ContextChart: Hashable {
5 case steps
6 case temperature
7}
8
9
10
11
12struct ContextCards: View {
13 let model: ChartsModel
14
15 var body: some View {
16 if let calendar = model.data.context.calendar {
17 NavigationLink {
18 ReportCalendarView(model: model)
19 } label: {
20 ContextCard(title: "Calendar", footnote: Text("^[\(calendar.reports) report](inflect: true)")) {
21 VStack(alignment: .leading, spacing: Theme.itemGap) {
22 MonthGrid(
23 month: Calendar.current.start(of: .month, for: calendar.last), summary: calendar,
24 compact: true
25 )
26 .accessibilityHidden(true)
27 Text("Reports on \(calendar.daysWithReports) of ^[\(calendar.days) day](inflect: true)")
28 }
29 }
30 }
31 .settingsRow()
32 }
33 if let places = model.data.context.places, let top = places.places.first {
34 NavigationLink {
35 PlacesMapView(model: model)
36 } label: {
37 ContextCard(title: "Places", footnote: Text("^[\(places.places.count) place](inflect: true)")) {
38 VStack(alignment: .leading, spacing: Theme.itemGap) {
39 PlacesMap(places: places.places, interactive: false)
40 .frame(height: 160)
41 .clipShape(.rect(cornerRadius: 6))
42 .overlay(RoundedRectangle(cornerRadius: 6).strokeBorder(Theme.rule))
43 .allowsHitTesting(false)
44 .accessibilityHidden(true)
45 Text("Most often \(top.name)")
46 }
47 }
48 }
49 .settingsRow()
50 }
51 if let steps = model.data.context.steps {
52 NavigationLink {
53 ContextChartView(chart: .steps, model: model)
54 } label: {
55 ContextCard(title: "Steps", footnote: Text("^[\(steps.days) day](inflect: true)")) {
56 Text("\(steps.mean.formatted(.number.precision(.fractionLength(0)))) a day on average")
57 }
58 }
59 .settingsRow()
60 }
61 if let temperature = model.data.context.temperature {
62 NavigationLink {
63 ContextChartView(chart: .temperature, model: model)
64 } label: {
65 ContextCard(
66 title: "Temperature", footnote: Text("^[\(temperature.reports) report](inflect: true)")
67 ) {
68 Text(
69 "Average \(Degrees.format(temperature.mean)) · \(Degrees.format(temperature.lowest)) to \(Degrees.format(temperature.highest))"
70 )
71 }
72 }
73 .settingsRow()
74 }
75 }
76}
77
78
79private struct ContextCard<Gist: View>: View {
80 let title: LocalizedStringKey
81 let footnote: Text
82 @ViewBuilder let gist: Gist
83
84 var body: some View {
85 VStack(alignment: .leading, spacing: Theme.itemGap) {
86 Text(title)
87 .font(Theme.serif(.body, weight: .medium))
88 gist
89 .font(Theme.serif(.body))
90 footnote
91 .font(.footnote)
92 .foregroundStyle(Theme.muted)
93 }
94 .padding(.vertical, 4)
95 }
96}
97
98
99struct ContextChartView: View {
100 let chart: ContextChart
101 let model: ChartsModel
102
103 var body: some View {
104 List {
105 Section {
106 VStack(alignment: .leading, spacing: Theme.lineGap) {
107 Text(chart == .steps ? "Steps" : "Temperature")
108 .font(Theme.serif(.title3, weight: .medium))
109 subtitle
110 .font(.footnote)
111 .foregroundStyle(Theme.muted)
112 }
113 .settingsRow()
114 .listRowSeparator(.hidden)
115 }
116 switch chart {
117 case .steps:
118 if let steps = model.data.context.steps {
119 Section {
120 TrendChart(
121 points: steps.trend, bucket: model.data.period.bucket, domain: model.data.domain,
122 format: { $0.formatted(.number.notation(.compactName).precision(.significantDigits(1...3))) }
123 )
124 .settingsRow()
125 .listRowSeparator(.hidden)
126 SettingsNote {
127 Text(
128 "Each day's steps are the most counted by its reports, so a day without a report late in it may show fewer than were taken."
129 )
130 }
131 } header: {
132 SettingsHeading(model.data.period.bucket == .day ? "Each Day" : "Average a Day")
133 }
134 }
135 case .temperature:
136 if let temperature = model.data.context.temperature {
137 Section {
138 TrendChart(
139 points: temperature.trend.map(Degrees.converted), bucket: model.data.period.bucket,
140 domain: model.data.domain, showsRange: true,
141 format: { Degrees.formatConverted($0) }
142 )
143 .settingsRow()
144 .listRowSeparator(.hidden)
145 SettingsNote { Text("The range of temperatures reports captured, with the average marked.") }
146 } header: {
147 SettingsHeading("Range and Average")
148 }
149 Section {
150 Grid(alignment: .leading, horizontalSpacing: Theme.blockGap, verticalSpacing: Theme.itemGap) {
151 GridRow {
152 Stat(title: "Average", value: Degrees.format(temperature.mean))
153 Stat(title: "Lowest", value: Degrees.format(temperature.lowest))
154 Stat(title: "Highest", value: Degrees.format(temperature.highest))
155 }
156 }
157 .settingsRow()
158 }
159 }
160 }
161 }
162 .listStyle(.plain)
163 .scrollContentBackground(.hidden)
164 .tabBarClearance()
165 .background(Theme.paper)
166 .foregroundStyle(Theme.ink)
167 .navigationBarTitleDisplayMode(.inline)
168 }
169
170 private var subtitle: Text {
171 let period = model.data.period.title
172 switch chart {
173 case .steps:
174 let days = model.data.context.steps?.days ?? 0
175 return Text("\(period) · ^[\(days) day](inflect: true)")
176 case .temperature:
177 let reports = model.data.context.temperature?.reports ?? 0
178 return Text("\(period) · ^[\(reports) report](inflect: true)")
179 }
180 }
181}
182
183
184
185enum Degrees {
186 private static var unit: UnitTemperature { UnitTemperature(forLocale: .current, usage: .weather) }
187
188
189 static func convert(_ celsius: Double) -> Double {
190 Measurement(value: celsius, unit: UnitTemperature.celsius).converted(to: unit).value
191 }
192
193 static func converted(_ point: TrendPoint) -> TrendPoint {
194 TrendPoint(
195 date: point.date, value: convert(point.value), low: point.low.map(convert), high: point.high.map(convert))
196 }
197
198
199 static func format(_ celsius: Double) -> String {
200 formatConverted(convert(celsius))
201 }
202
203
204 static func formatConverted(_ value: Double) -> String {
205 "\(value.formatted(.number.precision(.fractionLength(0))))°"
206 }
207}