krz/domain-dig

an ios app for DNS & SSL analysis

clone: git clone https://gitbay.org/krz/domain-dig.git

v4.5.0: DomainDig/DomainViewModel+Widget.swift · raw

 1import Foundation
 2import WidgetKit
 3
 4extension DomainViewModel {
 5    /// Publishes the current portfolio state to the App Group container so the
 6    /// widget can render it, then asks WidgetKit to refresh its timelines.
 7    func refreshWidgetData() {
 8        let data = portfolioDashboardData
 9        let snapshot = data.snapshot
10
11        let ordered = data.domainStates.sorted { lhs, rhs in
12            if lhs.trackedDomain.isPinned != rhs.trackedDomain.isPinned {
13                return lhs.trackedDomain.isPinned
14            }
15            return lhs.health.widgetSeverityRank > rhs.health.widgetSeverityRank
16        }
17
18        let domains = ordered.prefix(6).map { state in
19            DomainDigWidgetDomain(
20                domain: state.trackedDomain.domain,
21                isPinned: state.trackedDomain.isPinned,
22                status: state.health.widgetStatus,
23                certDaysRemaining: state.certificateDaysRemaining,
24                lastChange: state.lastChangeDate
25            )
26        }
27
28        DomainDigWidgetStore.write(
29            DomainDigWidgetData(
30                generatedAt: Date(),
31                totalDomains: snapshot.totalDomains,
32                healthyCount: snapshot.healthyCount,
33                warningCount: snapshot.warningCount,
34                criticalCount: snapshot.criticalCount,
35                expiringSoonCount: snapshot.expiringSoonCount,
36                unreachableCount: snapshot.unreachableCount,
37                domains: Array(domains)
38            )
39        )
40        WidgetCenter.shared.reloadAllTimelines()
41    }
42}
43
44private extension DomainHealth {
45    var widgetStatus: DomainDigWidgetStatus {
46        switch self {
47        case .healthy: return .healthy
48        case .warning: return .warning
49        case .critical: return .critical
50        }
51    }
52
53    var widgetSeverityRank: Int {
54        switch self {
55        case .healthy: return 0
56        case .warning: return 1
57        case .critical: return 2
58        }
59    }
60}