DomainDig/BatchSweepSummaryView.swift

84 lines · 2918 bytes

 1import SwiftUI
 2
 3struct BatchSweepSummaryView: View {
 4    @Bindable var viewModel: DomainViewModel
 5    let summary: BatchSweepSummary
 6
 7    @State private var showUnchangedDomains = false
 8
 9    private var visibleResults: [BatchLookupResult] {
10        if showUnchangedDomains {
11            return summary.results
12        }
13
14        return summary.results.filter(\.hasMeaningfulChange)
15    }
16
17    var body: some View {
18        NavigationStack {
19            List {
20                Section("Overview") {
21                    statRow(label: "Checked", value: "\(summary.totalDomains)")
22                    statRow(label: "Changed", value: "\(summary.changedDomains)")
23                    statRow(label: "Warnings", value: "\(summary.warningDomains)")
24                    statRow(label: "Unchanged", value: "\(summary.unchangedDomains)")
25                }
26
27                Section {
28                    Toggle("Show unchanged domains", isOn: $showUnchangedDomains)
29                }
30
31                BatchResultsSection(
32                    viewModel: viewModel,
33                    results: visibleResults,
34                    emptyTitle: "Changed Domains",
35                    emptyText: "No domains with changes or warnings"
36                )
37            }
38            .navigationTitle(summary.source == .watchlistRefresh ? "Sweep Summary" : "Batch Summary")
39        }
40    }
41
42    private func statRow(label: String, value: String) -> some View {
43        HStack {
44            Text(label)
45                .font(.system(.caption, design: .monospaced))
46                .foregroundStyle(Color(.appTextSecondary))
47            Spacer()
48            Text(value)
49                .font(.system(.callout, design: .monospaced))
50                .foregroundStyle(.primary)
51        }
52    }
53}
54
55/// A batch's results, each opening its history entry where one was saved.
56/// Titled `emptyTitle`, with `emptyText` under it, when nothing is shown.
57struct BatchResultsSection: View {
58    @Bindable var viewModel: DomainViewModel
59    let results: [BatchLookupResult]
60    let emptyTitle: String
61    let emptyText: String
62
63    var body: some View {
64        Section(results.isEmpty ? emptyTitle : "Results") {
65            if results.isEmpty {
66                Text(emptyText)
67                    .font(.system(.caption, design: .monospaced))
68                    .foregroundStyle(Color(.appTextSecondary))
69            } else {
70                ForEach(results) { result in
71                    if let entry = viewModel.historyEntry(for: result) {
72                        NavigationLink {
73                            HistoryDetailView(viewModel: viewModel, entry: entry)
74                        } label: {
75                            BatchResultRowView(result: result)
76                        }
77                    } else {
78                        BatchResultRowView(result: result)
79                    }
80                }
81            }
82        }
83    }
84}