DomainDig/WatchlistView.swift
403 lines · 16551 bytes
1import SwiftUI
2
3struct WatchlistView: View {
4 @Bindable var viewModel: DomainViewModel
5 @Environment(\.dismiss) private var dismiss
6
7 var body: some View {
8 List {
9 if viewModel.batchLookupSource == .watchlistRefresh, (!viewModel.batchResults.isEmpty || viewModel.batchLookupRunning) {
10 Section("Refresh Progress") {
11 VStack(alignment: .leading, spacing: 8) {
12 if viewModel.batchLookupRunning {
13 ProgressView(value: Double(viewModel.batchCompletedCount), total: Double(max(viewModel.batchTotalCount, 1)))
14 .tint(.cyan)
15 Text(viewModel.batchProgressLabel)
16 .font(.system(.caption, design: .monospaced))
17 .foregroundStyle(.secondary)
18 }
19
20 ForEach(viewModel.batchResults.prefix(5)) { result in
21 BatchResultRowView(result: result)
22 }
23 }
24 .padding(.vertical, 4)
25 }
26 .listRowBackground(Color(.systemGray6).opacity(0.5))
27 }
28
29 if viewModel.filteredTrackedDomains.isEmpty {
30 Section {
31 VStack(alignment: .leading, spacing: 8) {
32 Text("No tracked domains yet")
33 .font(.system(.callout, design: .monospaced))
34 .foregroundStyle(.primary)
35 Text("Tracked domains appear here. Tracking is local and manual for now.")
36 .font(.system(.caption, design: .monospaced))
37 .foregroundStyle(.secondary)
38 }
39 .padding(.vertical, 8)
40 }
41 .listRowBackground(Color(.systemGray6).opacity(0.5))
42 } else {
43 if let limitMessage = PremiumAccessService.trackedDomainLimitMessage(currentCount: viewModel.trackedDomains.count) {
44 Section {
45 Text(limitMessage)
46 .font(.system(.caption, design: .monospaced))
47 .foregroundStyle(.secondary)
48 }
49 .listRowBackground(Color(.systemGray6).opacity(0.5))
50 }
51
52 Section {
53 ForEach(viewModel.filteredTrackedDomains) { trackedDomain in
54 NavigationLink {
55 TrackedDomainDetailView(viewModel: viewModel, trackedDomain: trackedDomain)
56 } label: {
57 WatchlistRowView(
58 trackedDomain: trackedDomain,
59 isRefreshing: viewModel.refreshingTrackedDomainID == trackedDomain.id
60 )
61 }
62 .buttonStyle(.plain)
63 .swipeActions(edge: .leading, allowsFullSwipe: false) {
64 Button {
65 viewModel.togglePinned(for: trackedDomain)
66 } label: {
67 Label(trackedDomain.isPinned ? "Unpin" : "Pin", systemImage: trackedDomain.isPinned ? "pin.slash" : "pin")
68 }
69 .tint(.yellow)
70 }
71 .swipeActions(edge: .trailing, allowsFullSwipe: false) {
72 Button(role: .destructive) {
73 viewModel.deleteTrackedDomain(trackedDomain)
74 } label: {
75 Label("Delete", systemImage: "trash")
76 }
77 }
78 .contextMenu {
79 Button {
80 viewModel.refreshTrackedDomain(trackedDomain)
81 } label: {
82 Label("Refresh", systemImage: "arrow.clockwise")
83 }
84
85 Button {
86 dismiss()
87 viewModel.rerunInspection(for: trackedDomain)
88 } label: {
89 Label("Open Inspection", systemImage: "magnifyingglass")
90 }
91
92 Button {
93 viewModel.togglePinned(for: trackedDomain)
94 } label: {
95 Label(trackedDomain.isPinned ? "Unpin" : "Pin", systemImage: trackedDomain.isPinned ? "pin.slash" : "pin")
96 }
97
98 Button(role: .destructive) {
99 viewModel.deleteTrackedDomain(trackedDomain)
100 } label: {
101 Label("Delete", systemImage: "trash")
102 }
103 }
104 .listRowBackground(Color(.systemGray6).opacity(0.5))
105 }
106 .onDelete(perform: deleteFilteredTrackedDomains)
107 } header: {
108 Text("Tracked Domains")
109 }
110 }
111 }
112 .scrollContentBackground(.hidden)
113 .background(Color.black)
114 .navigationTitle("Watchlist")
115 .searchable(text: $viewModel.watchlistSearchText, prompt: "Search tracked domains")
116 .toolbar {
117 if !viewModel.filteredTrackedDomains.isEmpty {
118 ToolbarItemGroup(placement: .topBarTrailing) {
119 Menu {
120 Picker("Filter", selection: $viewModel.watchlistFilter) {
121 ForEach(WatchlistFilterOption.allCases) { option in
122 Text(option.title).tag(option)
123 }
124 }
125
126 Picker("Sort", selection: $viewModel.watchlistSortOption) {
127 ForEach(WatchlistSortOption.allCases) { option in
128 Text(option.title).tag(option)
129 }
130 }
131
132 Button("Refresh All") {
133 viewModel.refreshAllTrackedDomains()
134 }
135
136 Button("Export TXT") {
137 shareTrackedDomains(asCSV: false)
138 }
139
140 Button("Export CSV") {
141 shareTrackedDomains(asCSV: true)
142 }
143 } label: {
144 Image(systemName: "line.3.horizontal.decrease.circle")
145 }
146
147 EditButton()
148 }
149 }
150 }
151 .onChange(of: viewModel.rerunNavigationToken) { _, _ in
152 dismiss()
153 }
154 .preferredColorScheme(.dark)
155 }
156
157 private func deleteFilteredTrackedDomains(at offsets: IndexSet) {
158 let domains = offsets.map { viewModel.filteredTrackedDomains[$0] }
159 domains.forEach(viewModel.deleteTrackedDomain)
160 }
161
162 private func shareTrackedDomains(asCSV: Bool) {
163 let formatter = DateFormatter()
164 formatter.dateFormat = "yyyyMMdd_HHmmss"
165 let timestamp = formatter.string(from: Date())
166 let fileExtension = asCSV ? "csv" : "txt"
167 let filename = "\(timestamp)_domaindig_watchlist.\(fileExtension)"
168 let contents = asCSV
169 ? viewModel.exportTrackedDomainsCSV(domains: viewModel.filteredTrackedDomains)
170 : viewModel.exportTrackedDomainsText(domains: viewModel.filteredTrackedDomains)
171 ExportPresenter.share(filename: filename, contents: contents)
172 }
173}
174
175struct WatchlistRowView: View {
176 let trackedDomain: TrackedDomain
177 let isRefreshing: Bool
178
179 var body: some View {
180 VStack(alignment: .leading, spacing: 6) {
181 HStack(alignment: .firstTextBaseline, spacing: 8) {
182 if trackedDomain.isPinned {
183 Image(systemName: "pin.fill")
184 .font(.caption2)
185 .foregroundStyle(.yellow)
186 }
187 Text(trackedDomain.domain)
188 .font(.system(.callout, design: .monospaced))
189 .foregroundStyle(.primary)
190 .lineLimit(2)
191 .multilineTextAlignment(.leading)
192 Spacer(minLength: 8)
193 statusBadge
194 }
195
196 Text("Updated \(trackedDomain.updatedAt.formatted(date: .abbreviated, time: .shortened))")
197 .font(.system(.caption2, design: .monospaced))
198 .foregroundStyle(.secondary)
199
200 if let note = trackedDomain.note?.trimmingCharacters(in: .whitespacesAndNewlines), !note.isEmpty {
201 Text(note)
202 .font(.system(.caption, design: .monospaced))
203 .foregroundStyle(.secondary)
204 .lineLimit(2)
205 } else if let summary = trackedDomain.lastChangeSummary {
206 Text(summary.changedSections.isEmpty ? "No meaningful changes detected." : summary.changedSections.joined(separator: " • "))
207 .font(.system(.caption, design: .monospaced))
208 .foregroundStyle(.secondary)
209 .lineLimit(2)
210 }
211 }
212 .frame(maxWidth: .infinity, alignment: .leading)
213 .padding(.vertical, 4)
214 }
215
216 private func availabilityLabel(_ status: DomainAvailabilityStatus?) -> String {
217 switch status {
218 case .available:
219 return "Available"
220 case .registered:
221 return "Registered"
222 case .unknown, .none:
223 return "Unknown"
224 }
225 }
226
227 @ViewBuilder
228 private var statusBadge: some View {
229 if isRefreshing {
230 HStack(spacing: 6) {
231 ProgressView()
232 .controlSize(.small)
233 Text("Refreshing")
234 .font(.system(.caption2, design: .monospaced))
235 .foregroundStyle(.secondary)
236 }
237 .padding(.horizontal, 8)
238 .padding(.vertical, 4)
239 .background(Color(.systemGray5).opacity(0.6))
240 .clipShape(Capsule())
241 } else {
242 Text(availabilityLabel(trackedDomain.lastKnownAvailability))
243 .font(.system(.caption2, design: .monospaced))
244 .foregroundStyle(badgeColor)
245 .padding(.horizontal, 8)
246 .padding(.vertical, 4)
247 .background(badgeBackground)
248 .clipShape(Capsule())
249 }
250 }
251
252 private var badgeColor: Color {
253 switch trackedDomain.lastKnownAvailability {
254 case .available:
255 return .green
256 case .registered:
257 return .yellow
258 case .unknown, .none:
259 return .secondary
260 }
261 }
262
263 private var badgeBackground: Color {
264 switch trackedDomain.lastKnownAvailability {
265 case .available:
266 return .green.opacity(0.16)
267 case .registered:
268 return .yellow.opacity(0.16)
269 case .unknown, .none:
270 return Color(.systemGray5).opacity(0.6)
271 }
272 }
273}
274
275struct TrackedDomainDetailView: View {
276 @Bindable var viewModel: DomainViewModel
277 let trackedDomain: TrackedDomain
278 @Environment(\.dismiss) private var dismiss
279
280 @State private var noteDraft = ""
281 @State private var isEditingNote = false
282
283 private var liveTrackedDomain: TrackedDomain {
284 viewModel.trackedDomains.first(where: { $0.id == trackedDomain.id }) ?? trackedDomain
285 }
286
287 private var latestSnapshots: [HistoryEntry] {
288 viewModel.recentSnapshots(for: liveTrackedDomain)
289 }
290
291 private var latestDiffSections: [DomainDiffSection] {
292 viewModel.diffSectionsForLatestSnapshots(of: liveTrackedDomain)
293 }
294
295 var body: some View {
296 List {
297 Section {
298 WatchlistRowView(
299 trackedDomain: liveTrackedDomain,
300 isRefreshing: viewModel.refreshingTrackedDomainID == liveTrackedDomain.id
301 )
302 }
303 .listRowBackground(Color(.systemGray6).opacity(0.5))
304
305 Section {
306 Button {
307 viewModel.refreshTrackedDomain(liveTrackedDomain)
308 } label: {
309 Label("Manual Refresh", systemImage: "arrow.clockwise")
310 }
311
312 Button {
313 viewModel.rerunInspection(for: liveTrackedDomain)
314 } label: {
315 Label("Re-run Inspection", systemImage: "magnifyingglass")
316 }
317
318 Button {
319 viewModel.togglePinned(for: liveTrackedDomain)
320 } label: {
321 Label(liveTrackedDomain.isPinned ? "Unpin Domain" : "Pin Domain", systemImage: liveTrackedDomain.isPinned ? "pin.slash" : "pin")
322 }
323
324 Button {
325 noteDraft = liveTrackedDomain.note ?? ""
326 isEditingNote = true
327 } label: {
328 Label(liveTrackedDomain.note == nil ? "Add Note" : "Edit Note", systemImage: "note.text")
329 }
330 }
331 .listRowBackground(Color(.systemGray6).opacity(0.5))
332
333 if let summary = viewModel.latestChangeSummary(for: liveTrackedDomain) {
334 Section("Latest Change Summary") {
335 DomainChangeSummaryView(summary: summary)
336 }
337 .listRowBackground(Color.clear)
338 }
339
340 if !latestDiffSections.isEmpty {
341 Section("Latest Diff") {
342 DomainDiffView(title: "Latest Snapshot vs Previous", sections: latestDiffSections, showsUnchanged: false)
343 }
344 .listRowBackground(Color.clear)
345 }
346
347 Section("Recent Snapshots") {
348 if latestSnapshots.isEmpty {
349 Text("No snapshots yet")
350 .font(.system(.caption, design: .monospaced))
351 .foregroundStyle(.secondary)
352 } else {
353 ForEach(latestSnapshots) { entry in
354 NavigationLink {
355 HistoryDetailView(viewModel: viewModel, entry: entry)
356 } label: {
357 VStack(alignment: .leading, spacing: 4) {
358 Text(entry.timestamp.formatted(date: .abbreviated, time: .shortened))
359 .font(.system(.caption, design: .monospaced))
360 .foregroundStyle(.primary)
361 Text(entry.changeSummary?.hasChanges == true ? "Changed" : "Snapshot")
362 .font(.system(.caption2, design: .monospaced))
363 .foregroundStyle(.secondary)
364 }
365 }
366 }
367 }
368 }
369 .listRowBackground(Color(.systemGray6).opacity(0.5))
370 }
371 .scrollContentBackground(.hidden)
372 .background(Color.black)
373 .navigationTitle(liveTrackedDomain.domain)
374 .preferredColorScheme(.dark)
375 .onChange(of: viewModel.rerunNavigationToken) { _, _ in
376 dismiss()
377 }
378 .sheet(isPresented: $isEditingNote) {
379 NavigationStack {
380 Form {
381 Section("Tracking Note") {
382 TextField("Optional note", text: $noteDraft, axis: .vertical)
383 .lineLimit(3...6)
384 }
385 }
386 .navigationTitle("Edit Note")
387 .toolbar {
388 ToolbarItem(placement: .cancellationAction) {
389 Button("Cancel") {
390 isEditingNote = false
391 }
392 }
393 ToolbarItem(placement: .confirmationAction) {
394 Button("Save") {
395 viewModel.updateNote(noteDraft, for: liveTrackedDomain)
396 isEditingNote = false
397 }
398 }
399 }
400 }
401 }
402 }
403}