DomainDig/WatchlistView.swift
69 lines · 2407 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.watchedDomains.isEmpty {
10 Text("No watched domains")
11 .font(.system(.callout, design: .monospaced))
12 .foregroundStyle(.secondary)
13 .listRowBackground(Color(.systemGray6).opacity(0.5))
14 } else {
15 ForEach(viewModel.watchedDomains) { watchedDomain in
16 Button {
17 viewModel.domain = watchedDomain.domain
18 dismiss()
19 viewModel.run()
20 } label: {
21 VStack(alignment: .leading, spacing: 4) {
22 Text(watchedDomain.domain)
23 .font(.system(.callout, design: .monospaced))
24 .foregroundStyle(.primary)
25 Text(statusLabel(watchedDomain.lastKnownAvailability))
26 .font(.system(.caption2, design: .monospaced))
27 .foregroundStyle(statusColor(watchedDomain.lastKnownAvailability))
28 }
29 }
30 .listRowBackground(Color(.systemGray6).opacity(0.5))
31 }
32 .onDelete { offsets in
33 viewModel.removeWatchedDomains(at: offsets)
34 }
35 }
36 }
37 .scrollContentBackground(.hidden)
38 .background(Color.black)
39 .navigationTitle("Watchlist")
40 .toolbar {
41 if !viewModel.watchedDomains.isEmpty {
42 EditButton()
43 }
44 }
45 .preferredColorScheme(.dark)
46 }
47
48 private func statusLabel(_ status: DomainAvailabilityStatus?) -> String {
49 switch status {
50 case .available:
51 return "Available"
52 case .registered:
53 return "Registered"
54 case .unknown, .none:
55 return "Unknown"
56 }
57 }
58
59 private func statusColor(_ status: DomainAvailabilityStatus?) -> Color {
60 switch status {
61 case .available:
62 return .green
63 case .registered:
64 return .yellow
65 case .unknown, .none:
66 return .secondary
67 }
68 }
69}