Reportage/Views/Charts/QuestionChartView.swift (5765B)
1import SwiftUI
2
3
4
5struct QuestionChartView: View {
6 let id: String
7 let model: ChartsModel
8
9
10 @State private var chosen: String?
11
12 var body: some View {
13 List {
14 if let summary = model.summary(id: id) {
15 Section {
16 VStack(alignment: .leading, spacing: Theme.lineGap) {
17 Text(summary.question.prompt)
18 .font(Theme.serif(.title3, weight: .medium))
19 Text("\(model.data.period.title) · ^[\(summary.answered) report](inflect: true)")
20 .font(.footnote)
21 .foregroundStyle(Theme.muted)
22 }
23 .settingsRow()
24 .listRowSeparator(.hidden)
25 }
26 if let trend = trend(for: summary) {
27 Section {
28 trend
29 .settingsRow()
30 .listRowSeparator(.hidden)
31 } header: {
32 SettingsHeading(trendTitle(for: summary))
33 }
34 }
35 Section {
36 content(for: summary)
37 } header: {
38 if summary.question.kind != .note {
39 SettingsHeading("Answers")
40 }
41 }
42 } else {
43 SettingsNote { Text("This question wasn't answered in this period.") }
44 }
45 }
46 .listStyle(.plain)
47 .scrollContentBackground(.hidden)
48 .tabBarClearance()
49 .background(Theme.paper)
50 .foregroundStyle(Theme.ink)
51 .navigationBarTitleDisplayMode(.inline)
52 }
53
54
55 private func chosenTally(in summary: QuestionSummary) -> Tally? {
56 summary.tallies.first { $0.id == chosen } ?? summary.tallies.first
57 }
58
59 private func trend(for summary: QuestionSummary) -> TrendChart? {
60 let bucket = model.data.period.bucket
61 let domain = model.data.domain
62 switch summary.question.kind {
63 case .yesNo:
64 return TrendChart(
65 points: summary.trend, bucket: bucket, domain: domain, valueDomain: 0...1,
66 format: { $0.formatted(.percent.precision(.fractionLength(0))) })
67 case .number:
68 return TrendChart(
69 points: summary.trend, bucket: bucket, domain: domain,
70 format: { $0.formatted(.number.precision(.fractionLength(0...1))) })
71 case .note:
72 return nil
73 default:
74 guard let tally = chosenTally(in: summary) else { return nil }
75 return TrendChart(
76 points: summary.tallyTrends[tally.id] ?? [], bucket: bucket, domain: domain,
77 format: { $0.formatted(.number.precision(.fractionLength(0))) })
78 }
79 }
80
81 private func trendTitle(for summary: QuestionSummary) -> LocalizedStringKey {
82 switch summary.question.kind {
83 case .yesNo: "Share Answering Yes"
84 case .number: "Average"
85 default: "'\(chosenTally(in: summary)?.label ?? "")' Over Time"
86 }
87 }
88
89 @ViewBuilder private func content(for summary: QuestionSummary) -> some View {
90 switch summary.question.kind {
91 case .yesNo:
92 ProportionBar(summary: summary)
93 .settingsRow()
94 TallyBars(tallies: summary.tallies, summary: summary)
95 .settingsRow()
96 case .number:
97 if let numbers = summary.numbers {
98 Grid(alignment: .leading, horizontalSpacing: Theme.blockGap, verticalSpacing: Theme.itemGap) {
99 GridRow {
100 Stat(title: "Average", value: numbers.mean)
101 Stat(title: "Lowest", value: numbers.lowest)
102 Stat(title: "Highest", value: numbers.highest)
103 }
104 }
105 .settingsRow()
106 }
107 case .note:
108 ForEach(Array(summary.notes.enumerated()), id: \.offset) { _, note in
109 VStack(alignment: .leading, spacing: Theme.lineGap) {
110 Text(note.date.formatted(date: .abbreviated, time: .shortened))
111 .eyebrow()
112 Text(note.text)
113 .font(Theme.serif(.body))
114 }
115 .settingsRow()
116 }
117 default:
118 TallyBars(
119 tallies: summary.tallies, summary: summary,
120 selection: Binding(get: { chosenTally(in: summary)?.id }, set: { chosen = $0 })
121 )
122 .settingsRow()
123 SettingsNote { Text("Choose an answer to see it over time.") }
124 }
125 }
126}
127
128
129struct Stat: View {
130 let title: LocalizedStringKey
131 let value: String
132
133 init(title: LocalizedStringKey, value: String) {
134 self.title = title
135 self.value = value
136 }
137
138 init(title: LocalizedStringKey, value: Double) {
139 self.init(title: title, value: value.formatted(.number.precision(.fractionLength(0...1))))
140 }
141
142 var body: some View {
143 VStack(alignment: .leading, spacing: Theme.lineGap) {
144 Text(title)
145 .eyebrow()
146 Text(value)
147 .font(Theme.serif(.title, weight: .medium))
148 .monospacedDigit()
149 }
150 .accessibilityElement(children: .combine)
151 }
152}