summaryrefslogtreecommitdiff
path: root/DomainDigWidget/DomainDigPortfolioWidget.swift
blob: 15019c80f8d828ae9469320a4210f616d070ad31 (plain) (blame)
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
190
191
192
193
194
import SwiftUI
import WidgetKit

struct DomainDigEntry: TimelineEntry {
    let date: Date
    let data: DomainDigWidgetData
}

struct DomainDigProvider: TimelineProvider {
    func placeholder(in _: 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, 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)
                // Clamped here and ONLY here. A widget canvas is a fixed
                // system-defined size and WidgetKit truncates overflow with no
                // scroll affordance, so unclamped accessibility sizes produce
                // less readable output, not more. In-app there is always a
                // scroll view, so nothing there is clamped.
                .dynamicTypeSize(...DynamicTypeSize.accessibility1)
        }
        .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 {
            if family == .systemSmall {
                smallView
            } else {
                mediumOrLargeView
            }
        }
    }

    private var emptyState: some View {
        VStack(spacing: 6) {
            Image(systemName: "magnifyingglass")
                .font(.title2)
                .foregroundStyle(Color(.appTextSecondary))
            Text("No tracked domains")
                .font(.caption)
                .foregroundStyle(Color(.appTextSecondary))
                .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(Color(.appTextSecondary))

            Text("\(data.totalDomains)")
                .font(.system(.largeTitle, design: .rounded, weight: .bold))
            Text("tracked")
                .font(.caption2)
                .foregroundStyle(Color(.appTextSecondary))

            Spacer(minLength: 0)

            HStack(spacing: 10) {
                countPill(data.healthyCount, Color(.statusPositive))
                countPill(data.warningCount, Color(.statusWarning))
                countPill(data.criticalCount, Color(.statusCritical))
            }
        }
    }

    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(Color(.appTextSecondary))
                Spacer()
                Text("\(data.totalDomains) tracked")
                    .font(.caption2)
                    .foregroundStyle(Color(.appTextSecondary))
            }

            HStack(spacing: 12) {
                summaryStat(data.healthyCount, "Healthy", Color(.statusPositive))
                summaryStat(data.warningCount, "Warning", Color(.statusWarning))
                summaryStat(data.criticalCount, "Critical", Color(.statusCritical))
                summaryStat(data.expiringSoonCount, "Expiring", Color(.statusWarning))
            }

            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(.caption2)
                .foregroundStyle(Color(.appTextSecondary))
        }
        .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(.caption2)
                    .foregroundStyle(Color(.appTextSecondary))
            }
            Text(domain.domain)
                .font(.caption)
                .lineLimit(1)
            Spacer(minLength: 4)
            Text(certLabel(for: domain))
                .font(.caption2)
                .foregroundStyle(Color(.appTextSecondary))
        }
    }

    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 Color(.statusPositive)
        case .warning: return Color(.statusWarning)
        case .critical: return Color(.statusCritical)
        }
    }
}