DomainDig/TimelineView.swift

1715e59287a7b48b29ed8cbf2cd45fcc92ec3126
domain-dig/DomainDig/TimelineView.swift history · blame · raw

212 lines · 8573 bytes

  1import SwiftUI
  2
  3struct TimelineView: View {
  4    @Environment(\.appDensity) private var appDensity
  5    @Bindable var viewModel: DomainViewModel
  6    let domain: String
  7
  8    @State private var presentedDiff: DomainDiff?
  9    @State private var focusedSectionID: String?
 10
 11    private var timelineSections: [TimelineSection] {
 12        viewModel.timelineSections(for: domain)
 13    }
 14
 15    private var compareButtonDisabled: Bool {
 16        viewModel.selectedSnapshots.count != 2 && viewModel.historyEntries(for: domain).count < 2
 17    }
 18
 19    var body: some View {
 20        List {
 21            ForEach(timelineSections) { section in
 22                Section(section.title) {
 23                    ForEach(section.entries) { summary in
 24                        if let entry = viewModel.historyEntry(withID: summary.historyEntryID) {
 25                            NavigationLink {
 26                                HistoryDetailView(viewModel: viewModel, entry: entry)
 27                            } label: {
 28                                TimelineRow(summary: summary)
 29                            }
 30                            .swipeActions(edge: .trailing, allowsFullSwipe: false) {
 31                                Button {
 32                                    viewModel.toggleSnapshotSelection(entry)
 33                                } label: {
 34                                    Label(
 35                                        viewModel.selectedSnapshotIDs.contains(entry.id) ? "Selected" : "Compare",
 36                                        systemImage: viewModel.selectedSnapshotIDs.contains(entry.id) ? "checkmark.circle.fill" : "arrow.left.arrow.right"
 37                                    )
 38                                }
 39
 40                                Button(role: .destructive) {
 41                                    viewModel.removeHistoryEntries(withIDs: [entry.id])
 42                                } label: {
 43                                    Label("Delete", systemImage: "trash")
 44                                }
 45                            }
 46                        }
 47                    }
 48                    .listRowBackground(Color(.systemGray6).opacity(0.5))
 49                }
 50            }
 51        }
 52        .scrollContentBackground(.hidden)
 53        .background(Color.black)
 54        .navigationTitle(domain)
 55        .toolbar {
 56            ToolbarItemGroup(placement: .topBarTrailing) {
 57                Menu {
 58                    Picker("Grouping", selection: $viewModel.timelineGrouping) {
 59                        ForEach(TimelineGroupingOption.allCases) { option in
 60                            Text(option.title).tag(option)
 61                        }
 62                    }
 63                } label: {
 64                    Image(systemName: "line.3.horizontal.decrease.circle")
 65                }
 66
 67                Button("Compare") {
 68                    if viewModel.selectedSnapshots.count == 2 {
 69                        presentedDiff = viewModel.generateDiffForSelectedSnapshots()
 70                    } else {
 71                        let entries = viewModel.historyEntries(for: domain)
 72                        guard entries.count >= 2 else { return }
 73                        presentedDiff = viewModel.generateDiff(from: entries[1], to: entries[0])
 74                    }
 75                    focusedSectionID = viewModel.currentDiffTargetSectionID
 76                }
 77                .disabled(compareButtonDisabled)
 78
 79                Menu("Export") {
 80                    Button("Export TXT") {
 81                        ExportPresenter.share(
 82                            filename: "\(domain)-timeline.txt",
 83                            contents: viewModel.exportTimelineText(domain: domain, includeDiffSummary: true)
 84                        )
 85                    }
 86
 87                    Button("Export JSON") {
 88                        guard let data = viewModel.exportTimelineJSONData(domain: domain, includeDiffSummary: true) else { return }
 89                        ExportPresenter.share(filename: "\(domain)-timeline.json", data: data)
 90                    }
 91                }
 92            }
 93        }
 94        .sheet(item: $presentedDiff) { diff in
 95            NavigationStack {
 96                TimelineDiffView(viewModel: viewModel, diff: diff, focusedSectionID: $focusedSectionID)
 97            }
 98        }
 99    }
100}
101
102private struct TimelineRow: View {
103    @Environment(\.appDensity) private var appDensity
104    let summary: SnapshotSummary
105
106    var body: some View {
107        VStack(alignment: .leading, spacing: appDensity.metrics.rowSpacing + 1) {
108            HStack(alignment: .center, spacing: 8) {
109                Text(summary.timestamp.formatted(date: .abbreviated, time: .shortened))
110                    .font(appDensity.font(.callout))
111                    .foregroundStyle(.primary)
112                Spacer()
113                if let severity = summary.severitySummary {
114                    AppStatusBadgeView(
115                        model: .init(
116                            title: severity.title,
117                            systemImage: "arrow.triangle.2.circlepath",
118                            foregroundColor: severity == .high ? .red : .yellow,
119                            backgroundColor: (severity == .high ? Color.red : .yellow).opacity(0.16)
120                        )
121                    )
122                }
123            }
124
125            Text(summary.changeSummaryMessage ?? "No change summary")
126                .font(appDensity.font(.caption))
127                .foregroundStyle(.secondary)
128                .lineLimit(2)
129
130            HStack(spacing: 8) {
131                AppStatusBadgeView(model: AppStatusFactory.availability(summary.availability))
132                if let riskScore = summary.riskScore {
133                    Text("Risk \(riskScore)")
134                        .lineLimit(1)
135                        .minimumScaleFactor(0.85)
136                }
137            }
138            .font(appDensity.font(.caption2))
139            .foregroundStyle(.secondary)
140
141            HStack(spacing: 8) {
142                if let primaryIP = summary.primaryIP {
143                    Text(primaryIP)
144                        .lineLimit(1)
145                        .truncationMode(.middle)
146                }
147                Spacer(minLength: 8)
148                Text(summary.timestamp.formatted(date: .abbreviated, time: .shortened))
149                    .lineLimit(1)
150            }
151            .font(appDensity.font(.caption2))
152            .foregroundStyle(.secondary)
153        }
154    }
155}
156
157struct TimelineDiffView: View {
158    @Bindable var viewModel: DomainViewModel
159    let diff: DomainDiff
160    @Binding var focusedSectionID: String?
161
162    var body: some View {
163        ScrollViewReader { proxy in
164            ScrollView {
165                VStack(alignment: .leading, spacing: 12) {
166                    HStack {
167                        Button("Previous Change") {
168                            viewModel.moveToPreviousDiffChange()
169                            focusedSectionID = viewModel.currentDiffTargetSectionID
170                            scroll(proxy: proxy)
171                        }
172                        .disabled(viewModel.activeDiffChangeIndex == 0)
173
174                        Button("Next Change") {
175                            viewModel.moveToNextDiffChange()
176                            focusedSectionID = viewModel.currentDiffTargetSectionID
177                            scroll(proxy: proxy)
178                        }
179                        .disabled(viewModel.activeDomainDiff?.changedSectionIDs.isEmpty != false || viewModel.currentDiffTargetSectionID == viewModel.activeDomainDiff?.changedSectionIDs.last)
180
181                        Spacer()
182                    }
183
184                    DomainDiffView(
185                        title: "Snapshot Diff",
186                        sections: diff.sections,
187                        contextNote: diff.contextNote,
188                        showsUnchanged: false,
189                        highlightedSectionID: focusedSectionID
190                    )
191                }
192                .padding()
193            }
194            .background(Color.black)
195            .navigationTitle("Compare Snapshots")
196            .navigationBarTitleDisplayMode(.inline)
197            .onAppear {
198                scroll(proxy: proxy)
199            }
200            .onChange(of: focusedSectionID) { _, _ in
201                scroll(proxy: proxy)
202            }
203        }
204    }
205
206    private func scroll(proxy: ScrollViewProxy) {
207        guard let focusedSectionID else { return }
208        withAnimation {
209            proxy.scrollTo(focusedSectionID, anchor: .top)
210        }
211    }
212}