1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
import SwiftUI
import WidgetKit
struct DomainDigEntry: TimelineEntry {
let date: Date
let data: DomainDigWidgetData
}
struct DomainDigProvider: TimelineProvider {
func placeholder(in context: Context) -> DomainDigEntry {
DomainDigEntry(date: Date(), data: .placeholder)
}
func getSnapshot(in context: Context, completion: @escaping (DomainDigEntry) -> Void) {
let data = context.isPreview ? .placeholder : (DomainDigWidgetStore.read() ?? .placeholder)
completion(DomainDigEntry(date: Date(), data: data))
}
func getTimeline(in context: Context, completion: @escaping (Timeline<DomainDigEntry>) -> Void) {
let data = DomainDigWidgetStore.read() ?? .empty
let entry = DomainDigEntry(date: Date(), data: data)
// The app reloads timelines on foreground and on watchlist changes; this
// periodic refresh is a backstop so cert countdowns stay roughly current.
let next = Calendar.current.date(byAdding: .hour, value: 6, to: Date())
?? Date().addingTimeInterval(6 * 3600)
completion(Timeline(entries: [entry], policy: .after(next)))
}
}
struct DomainDigPortfolioWidget: Widget {
let kind = "DomainDigPortfolioWidget"
var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: DomainDigProvider()) { entry in
DomainDigWidgetView(data: entry.data)
.containerBackground(.fill.tertiary, for: .widget)
}
.configurationDisplayName("Domain Portfolio")
.description("Health and certificate status for your tracked domains.")
.supportedFamilies([.systemSmall, .systemMedium, .systemLarge])
}
}
struct DomainDigWidgetView: View {
@Environment(\.widgetFamily) private var family
let data: DomainDigWidgetData
var body: some View {
if data.totalDomains == 0 {
emptyState
} else {
switch family {
case .systemSmall:
smallView
default:
mediumOrLargeView
}
}
}
private var emptyState: some View {
VStack(spacing: 6) {
Image(systemName: "magnifyingglass")
.font(.title2)
.foregroundStyle(.secondary)
Text("No tracked domains")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
}
// MARK: Small
private var smallView: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 4) {
Image(systemName: "shield.lefthalf.filled")
Text("DomainDig")
.fontWeight(.semibold)
Spacer()
}
.font(.caption2)
.foregroundStyle(.secondary)
Text("\(data.totalDomains)")
.font(.system(size: 34, weight: .bold, design: .rounded))
Text("tracked")
.font(.caption2)
.foregroundStyle(.secondary)
Spacer(minLength: 0)
HStack(spacing: 10) {
countPill(data.healthyCount, .green)
countPill(data.warningCount, .orange)
countPill(data.criticalCount, .red)
}
}
}
private func countPill(_ value: Int, _ color: Color) -> some View {
HStack(spacing: 3) {
Circle().fill(color).frame(width: 7, height: 7)
Text("\(value)").font(.caption).fontWeight(.medium)
}
}
// MARK: Medium / Large
private var mediumOrLargeView: some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Label("Domain Portfolio", systemImage: "shield.lefthalf.filled")
.font(.caption)
.fontWeight(.semibold)
.foregroundStyle(.secondary)
Spacer()
Text("\(data.totalDomains) tracked")
.font(.caption2)
.foregroundStyle(.secondary)
}
HStack(spacing: 12) {
summaryStat(data.healthyCount, "Healthy", .green)
summaryStat(data.warningCount, "Warning", .orange)
summaryStat(data.criticalCount, "Critical", .red)
summaryStat(data.expiringSoonCount, "Expiring", .yellow)
}
Divider()
VStack(spacing: 6) {
ForEach(data.domains.prefix(family == .systemLarge ? 6 : 3)) { domain in
Link(destination: DomainDigDeepLink.url(for: .detail(domain.domain))) {
domainRow(domain)
}
}
}
Spacer(minLength: 0)
}
}
private func summaryStat(_ value: Int, _ label: String, _ color: Color) -> some View {
VStack(alignment: .leading, spacing: 1) {
Text("\(value)")
.font(.headline)
.foregroundStyle(color)
Text(label)
.font(.system(size: 9))
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, alignment: .leading)
}
private func domainRow(_ domain: DomainDigWidgetDomain) -> some View {
HStack(spacing: 6) {
Circle()
.fill(color(for: domain.status))
.frame(width: 8, height: 8)
if domain.isPinned {
Image(systemName: "pin.fill")
.font(.system(size: 8))
.foregroundStyle(.secondary)
}
Text(domain.domain)
.font(.caption)
.lineLimit(1)
Spacer(minLength: 4)
Text(certLabel(for: domain))
.font(.caption2)
.foregroundStyle(.secondary)
}
}
private func certLabel(for domain: DomainDigWidgetDomain) -> String {
guard let days = domain.certDaysRemaining else { return "—" }
if days < 0 { return "expired" }
return "\(days)d"
}
private func color(for status: DomainDigWidgetStatus) -> Color {
switch status {
case .healthy: return .green
case .warning: return .orange
case .critical: return .red
}
}
}
|