DomainDig/DomainDiffViews.swift
274 lines · 12080 bytes
7 symbols in this file
1import SwiftUI
2
3// Snapshot comparison: the change summary and the diff list. Split out of
4// ContentView.swift (A13).
5
6struct DomainChangeSummaryView: View {
7 @Environment(\.appDensity) private var appDensity
8 let summary: DomainChangeSummary
9 @State private var showsDetails = false
10
11 var body: some View {
12 CardView(allowsHorizontalScroll: false) {
13 HStack {
14 Label(summary.hasChanges ? "Changed" : "Stable", systemImage: summary.hasChanges ? "arrow.triangle.2.circlepath" : "checkmark.circle")
15 .font(appDensity.font(.caption))
16 .foregroundStyle(summary.hasChanges ? severityTone(summary.severity).foreground : ThemeToken.positive)
17 Spacer()
18 Text(summary.severity.title.uppercased())
19 .font(appDensity.font(.caption2))
20 .foregroundStyle(summary.hasChanges ? severityTone(summary.severity).foreground : ThemeToken.textSecondary)
21 .padding(.horizontal, 8)
22 .padding(.vertical, 4)
23 .background((summary.hasChanges ? severityTone(summary.severity) : AppStatusTone.neutral).surface)
24 .clipShape(Capsule())
25 Text(summary.impactClassification.title.uppercased())
26 .font(appDensity.font(.caption2))
27 .foregroundStyle(summary.impactClassification.tone.foreground)
28 .padding(.horizontal, 8)
29 .padding(.vertical, 4)
30 .background(summary.impactClassification.tone.surface)
31 .clipShape(Capsule())
32 Text(summary.generatedAt, style: .time)
33 .font(appDensity.font(.caption2))
34 .foregroundStyle(ThemeToken.textSecondary)
35 }
36
37 VStack(alignment: .leading, spacing: 4) {
38 Text("Inference")
39 .font(appDensity.font(.caption2))
40 .foregroundStyle(ThemeToken.textSecondary)
41 Text(summary.message)
42 .font(appDensity.font(.caption))
43 .foregroundStyle(.primary)
44 .lineLimit(2)
45 }
46
47 if !summary.observedFacts.isEmpty || summary.contextNote != nil {
48 DisclosureGroup(showsDetails ? "Hide Details" : "Show Details", isExpanded: $showsDetails) {
49 VStack(alignment: .leading, spacing: 8) {
50 if !summary.observedFacts.isEmpty {
51 VStack(alignment: .leading, spacing: 4) {
52 Text("Observed")
53 .font(appDensity.font(.caption2))
54 .foregroundStyle(ThemeToken.textSecondary)
55 ForEach(Array(summary.observedFacts.enumerated()), id: \.offset) { _, fact in
56 Text(fact)
57 .font(appDensity.font(.caption))
58 .foregroundStyle(.primary)
59 }
60 }
61 }
62
63 if let riskScoreDelta = summary.riskScoreDelta {
64 Text("Risk delta: \(riskScoreDelta >= 0 ? "+" : "")\(riskScoreDelta)")
65 .font(appDensity.font(.caption2))
66 .foregroundStyle(riskScoreDelta > 0 ? AnyShapeStyle(ThemeToken.warning) : AnyShapeStyle(.secondary))
67 }
68
69 if let contextNote = summary.contextNote {
70 Text(contextNote)
71 .font(appDensity.font(.caption2))
72 .foregroundStyle(ThemeToken.warning)
73 }
74 }
75 .padding(.top, 4)
76 }
77 .font(appDensity.font(.caption))
78 .tint(.secondary)
79 }
80 }
81 }
82
83 private func severityTone(_ severity: ChangeSeverity) -> AppStatusTone {
84 switch severity {
85 case .low:
86 return .neutral
87 case .medium:
88 return .warning
89 case .high:
90 return .critical
91 }
92 }
93
94}
95
96struct DomainDiffView: View {
97 let title: String
98 let sections: [DomainDiffSection]
99 let contextNote: String?
100 let showsUnchanged: Bool
101 let highlightedSectionID: String?
102
103 @State private var collapsedSections = Set<String>()
104 @State private var showsLowSeverity = false
105
106 private var filteredSections: [DomainDiffSection] {
107 sections
108 .map { section in
109 let items = section.items.filter { item in
110 if !showsUnchanged, !item.hasChanges {
111 return false
112 }
113 if showsLowSeverity {
114 return true
115 }
116 return item.severity >= .medium || (showsUnchanged && item.changeType == .unchanged)
117 }
118 return DomainDiffSection(id: section.id, title: section.title, items: items)
119 }
120 .filter { !$0.items.isEmpty }
121 }
122
123 private var hasLowSeverityChanges: Bool {
124 sections.flatMap(\.items).contains { $0.hasChanges && $0.severity == .low }
125 }
126
127 var body: some View {
128 VStack(alignment: .leading, spacing: 12) {
129 HStack {
130 SectionTitleView(title: title)
131 Spacer()
132 if hasLowSeverityChanges {
133 Button(showsLowSeverity ? "Hide Low" : "Show Low") {
134 showsLowSeverity.toggle()
135 }
136 .buttonStyle(.bordered)
137 .font(.system(.caption, design: .monospaced))
138 }
139 }
140 if let contextNote {
141 MessageCardView(text: contextNote, isError: false)
142 }
143 if filteredSections.isEmpty {
144 MessageCardView(text: "No comparison data available", isError: false)
145 } else {
146 ForEach(filteredSections) { section in
147 CardView(allowsHorizontalScroll: false) {
148 DisclosureGroup(isExpanded: binding(for: section)) {
149 let visibleItems = showsUnchanged ? section.items : section.items.filter(\.hasChanges)
150
151 ForEach(visibleItems) { item in
152 VStack(alignment: .leading, spacing: 6) {
153 HStack {
154 Text(item.label)
155 .font(.system(.caption, design: .monospaced))
156 .foregroundStyle(ThemeToken.textSecondary)
157 Spacer()
158 Text("\(item.changeType.marker) \(item.severity.title) • \(changeLabel(for: item.changeType))")
159 .font(.system(.caption2, design: .monospaced))
160 .foregroundStyle(changeTone(for: item).foreground)
161 .padding(.horizontal, 8)
162 .padding(.vertical, 4)
163 .background(changeTone(for: item).surface)
164 .clipShape(Capsule())
165 }
166
167 if let oldValue = item.oldValue {
168 VStack(alignment: .leading, spacing: 2) {
169 Text("Old")
170 .font(.system(.caption2, design: .monospaced))
171 .foregroundStyle(ThemeToken.textSecondary)
172 Text(oldValue)
173 .font(.system(.caption2, design: .monospaced))
174 .foregroundStyle(ThemeToken.textSecondary)
175 .textSelection(.enabled)
176 }
177 }
178
179 if let newValue = item.newValue {
180 VStack(alignment: .leading, spacing: 2) {
181 Text("New")
182 .font(.system(.caption2, design: .monospaced))
183 .foregroundStyle(ThemeToken.textSecondary)
184 Text(newValue)
185 .font(.system(.caption, design: .monospaced))
186 .foregroundStyle(item.hasChanges ? .primary : .secondary)
187 .textSelection(.enabled)
188 }
189 }
190 }
191 .padding(10)
192 .background(item.hasChanges ? changeTone(for: item).surface : ThemeToken.surface)
193 .cornerRadius(8)
194 }
195 } label: {
196 HStack {
197 Text(section.title)
198 .font(.system(.subheadline, design: .monospaced))
199 .fontWeight(.semibold)
200 .foregroundStyle(sectionColor(section))
201 Spacer()
202 Text(section.severity.title)
203 .font(.system(.caption2, design: .monospaced))
204 .foregroundStyle(sectionColor(section))
205 }
206 }
207 }
208 .id(section.id)
209 .overlay {
210 if highlightedSectionID == section.id {
211 RoundedRectangle(cornerRadius: 12)
212 .stroke(ThemeToken.info.opacity(0.55), lineWidth: 1)
213 }
214 }
215 }
216 }
217 }
218 .onAppear {
219 collapsedSections = Set(sections.filter { !showsUnchanged && !$0.hasChanges }.map(\.id))
220 }
221 }
222
223 private func changeLabel(for changeType: DiffChangeType) -> String {
224 switch changeType {
225 case .added:
226 return "Added"
227 case .removed:
228 return "Removed"
229 case .changed:
230 return "Changed"
231 case .unchanged:
232 return "Unchanged"
233 }
234 }
235
236 private func changeTone(for item: DomainDiffItem) -> AppStatusTone {
237 if item.changeType == .unchanged {
238 return .neutral
239 }
240
241 switch item.severity {
242 case .low:
243 return .info
244 case .medium:
245 return .warning
246 case .high:
247 return .critical
248 }
249 }
250
251 private func sectionColor(_ section: DomainDiffSection) -> AnyShapeStyle {
252 switch section.severity {
253 case .low:
254 return AnyShapeStyle(.blue)
255 case .medium:
256 return AnyShapeStyle(ThemeToken.warning)
257 case .high:
258 return AnyShapeStyle(ThemeToken.critical)
259 }
260 }
261
262 private func binding(for section: DomainDiffSection) -> Binding<Bool> {
263 Binding(
264 get: { !collapsedSections.contains(section.id) },
265 set: { isExpanded in
266 if isExpanded {
267 collapsedSections.remove(section.id)
268 } else {
269 collapsedSections.insert(section.id)
270 }
271 }
272 )
273 }
274}