From 00cc231e9b419d27b412036d93457c2cbabe0b16 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Sat, 11 Apr 2026 23:36:35 -0500 Subject: Add SourceHut system status screen and home disruption banner --- Hutch/Networking/SystemStatusRepository.swift | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Hutch/Networking/SystemStatusRepository.swift (limited to 'Hutch/Networking/SystemStatusRepository.swift') diff --git a/Hutch/Networking/SystemStatusRepository.swift b/Hutch/Networking/SystemStatusRepository.swift new file mode 100644 index 0000000..48c5b16 --- /dev/null +++ b/Hutch/Networking/SystemStatusRepository.swift @@ -0,0 +1,57 @@ +import Foundation + +actor SystemStatusRepository { + private let service: SystemStatusService + private let ttl: TimeInterval + + private var snapshotCache: CacheEntry? + private var incidentsCache: CacheEntry<[StatusIncident]>? + + init(service: SystemStatusService = SystemStatusService(), ttl: TimeInterval = 10 * 60) { + self.service = service + self.ttl = ttl + } + + func snapshot(forceRefresh: Bool = false) async throws -> SystemStatusSnapshot { + if let cached = snapshotCache, !forceRefresh, !cached.isExpired(ttl: ttl) { + return cached.value + } + + do { + let snapshot = try await service.fetchSnapshot() + snapshotCache = CacheEntry(value: snapshot, timestamp: Date()) + return snapshot + } catch { + if let cached = snapshotCache { + return cached.value + } + throw error + } + } + + func recentIncidents(forceRefresh: Bool = false) async throws -> [StatusIncident] { + if let cached = incidentsCache, !forceRefresh, !cached.isExpired(ttl: ttl) { + return cached.value + } + + do { + let incidents = try await service.fetchIncidentFeed() + incidentsCache = CacheEntry(value: incidents, timestamp: Date()) + return incidents + } catch { + if let cached = incidentsCache { + return cached.value + } + throw error + } + } +} + +private struct CacheEntry: Sendable { + let value: Value + let timestamp: Date + + nonisolated func isExpired(ttl: TimeInterval) -> Bool { + Date().timeIntervalSince(timestamp) > ttl + } +} -- cgit v1.2.3