Commit b0a3f9abee

b0a3f9abeefa9e7b286631a8de28f50ae0a9b7ed

parent: c7d0024989

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-23 05:38 UTC

Share lookup loading and background refresh; drop memberwise inits

- DomainViewModel.loadLookup for ownership history, DNS history and
  extended subdomains; DataCapability.feature maps onto the plan feature.
- BGAppRefreshTask.run for the monitoring and report refreshes.
- MonitoringPendingAlert and ScheduledReportLog use the synthesized init.
- Qlty: exclude the DEBUG audit fixtures.

Layout: unified · split

.qlty/qlty.toml +2
@@ -11,6 +11,8 @@ exclude_patterns = [
11 "**/generated/**", 11 "**/generated/**",
12 ".claude/**", 12 ".claude/**",
13 "**/Pods/**", 13 "**/Pods/**",
14 # DEBUG-only seed data for the accessibility audit tests.
15 "DomainDig/AuditFixtures.swift",
14] 16]
15 17
16test_patterns = [ 18test_patterns = [
DomainDig/DataAccessService.swift +13 −11
@@ -2,17 +2,19 @@ import Foundation
2 2
3enum DataAccessService { 3enum DataAccessService {
4 static func hasAccess(to capability: DataCapability) -> Bool { 4 static func hasAccess(to capability: DataCapability) -> Bool {
5 switch capability { 5 FeatureAccessService.hasAccess(to: capability.feature)
6 case .ownershipHistory: 6 }
7 return FeatureAccessService.hasAccess(to: .ownershipHistory) 7}
8 case .dnsHistory: 8
9 return FeatureAccessService.hasAccess(to: .dnsHistory) 9extension DataCapability {
10 case .extendedSubdomains: 10 /// The plan feature that unlocks this data.
11 return FeatureAccessService.hasAccess(to: .extendedSubdomains) 11 var feature: FeatureCapability {
12 case .domainPricing: 12 switch self {
13 return FeatureAccessService.hasAccess(to: .domainPricing) 13 case .ownershipHistory: .ownershipHistory
14 case .reputation: 14 case .dnsHistory: .dnsHistory
15 return FeatureAccessService.hasAccess(to: .reputation) 15 case .extendedSubdomains: .extendedSubdomains
16 case .domainPricing: .domainPricing
17 case .reputation: .reputation
16 } 18 }
17 } 19 }
18} 20}
DomainDig/DomainMonitoringService.swift +19 −8
@@ -187,16 +187,11 @@ final class DomainMonitoringScheduler {
187 private func handleAppRefresh(task: BGAppRefreshTask) { 187 private func handleAppRefresh(task: BGAppRefreshTask) {
188 _ = syncSchedule() 188 _ = syncSchedule()
189 189
190 let worker = Task { 190 task.run {
191 let outcome = await DomainMonitoringService.shared.performMonitoring( 191 await DomainMonitoringService.shared.performMonitoring(
192 trigger: .background, 192 trigger: .background,
193 requireEnabledSetting: true 193 requireEnabledSetting: true
194 ) 194 ).success
195 task.setTaskCompleted(success: outcome.success)
196 }
197
198 task.expirationHandler = {
199 worker.cancel()
200 } 195 }
201 } 196 }
202#endif 197#endif
@@ -903,3 +898,19 @@ final class DomainMonitoringService {
903 return snapshot.emailSecurityError 898 return snapshot.emailSecurityError
904 } 899 }
905} 900}
901
902#if canImport(BackgroundTasks)
903extension BGAppRefreshTask {
904 /// Runs `work` for this refresh, completing the task with whether it succeeded, and cancels it
905 /// when the system ends the task early.
906 @MainActor
907 func run(_ work: @escaping @MainActor () async -> Bool) {
908 let worker = Task {
909 setTaskCompleted(success: await work())
910 }
911 expirationHandler = {
912 worker.cancel()
913 }
914 }
915}
916#endif
DomainDig/DomainViewModel.swift +40 −50
@@ -1282,70 +1282,60 @@ final class DomainViewModel {
1282 } 1282 }
1283 1283
1284 func loadOwnershipHistory() async { 1284 func loadOwnershipHistory() async {
1285 guard !searchedDomain.isEmpty else { return } 1285 await loadLookup(.ownershipHistory, into: \.ownershipHistory, loading: \.ownershipHistoryLoading, error: \.ownershipHistoryError,
1286 guard DataAccessService.hasAccess(to: .ownershipHistory) else { 1286 fallback: "Ownership history unavailable") {
1287 upgradePrompt = FeatureAccessService.upgradePrompt(for: .ownershipHistory) 1287 await ExternalDataService.shared.ownershipHistory(
1288 return 1288 domain: searchedDomain,
1289 currentOwnership: ownershipResult,
1290 historyEntries: history
1291 ).value
1289 } 1292 }
1290 guard ownershipHistory.isEmpty else { return }
1291
1292 ownershipHistoryLoading = true
1293 ownershipHistoryError = nil
1294
1295 let outcome = await ExternalDataService.shared.ownershipHistory(
1296 domain: searchedDomain,
1297 currentOwnership: ownershipResult,
1298 historyEntries: history
1299 )
1300
1301 (ownershipHistory, ownershipHistoryError) = found(outcome.value, fallback: "Ownership history unavailable")
1302
1303 ownershipHistoryLoading = false
1304 _ = saveHistoryEntry(replaceLatest: true)
1305 } 1293 }
1306 1294
1307 func loadDNSHistory() async { 1295 func loadDNSHistory() async {
1308 guard !searchedDomain.isEmpty else { return } 1296 await loadLookup(.dnsHistory, into: \.dnsHistory, loading: \.dnsHistoryLoading, error: \.dnsHistoryError,
1309 guard DataAccessService.hasAccess(to: .dnsHistory) else { 1297 fallback: "DNS history unavailable") {
1310 upgradePrompt = FeatureAccessService.upgradePrompt(for: .dnsHistory) 1298 await ExternalDataService.shared.dnsHistory(
1311 return 1299 domain: searchedDomain,
1300 dnsSections: dnsSections,
1301 historyEntries: history
1302 ).value
1312 } 1303 }
1313 guard dnsHistory.isEmpty else { return }
1314
1315 dnsHistoryLoading = true
1316 dnsHistoryError = nil
1317
1318 let outcome = await ExternalDataService.shared.dnsHistory(
1319 domain: searchedDomain,
1320 dnsSections: dnsSections,
1321 historyEntries: history
1322 )
1323
1324 (dnsHistory, dnsHistoryError) = found(outcome.value, fallback: "DNS history unavailable")
1325
1326 dnsHistoryLoading = false
1327 _ = saveHistoryEntry(replaceLatest: true)
1328 } 1304 }
1329 1305
1330 func loadExtendedSubdomains() async { 1306 func loadExtendedSubdomains() async {
1307 await loadLookup(.extendedSubdomains, into: \.extendedSubdomains, loading: \.extendedSubdomainsLoading, error: \.extendedSubdomainsError,
1308 fallback: "Extended subdomains unavailable") {
1309 await ExternalDataService.shared.extendedSubdomains(
1310 domain: searchedDomain,
1311 existing: subdomains
1312 ).value
1313 }
1314 }
1315
1316 /// Loads `capability`'s lookup for the searched domain into `items`, unless it is loaded
1317 /// already, or shows the upgrade prompt when the plan doesn't include it.
1318 private func loadLookup<Item>(
1319 _ capability: DataCapability,
1320 into items: ReferenceWritableKeyPath<DomainViewModel, [Item]>,
1321 loading: ReferenceWritableKeyPath<DomainViewModel, Bool>,
1322 error: ReferenceWritableKeyPath<DomainViewModel, String?>,
1323 fallback: String,
1324 fetch: () async -> ServiceResult<[Item]>
1325 ) async {
1331 guard !searchedDomain.isEmpty else { return } 1326 guard !searchedDomain.isEmpty else { return }
1332 guard DataAccessService.hasAccess(to: .extendedSubdomains) else { 1327 guard DataAccessService.hasAccess(to: capability) else {
1333 upgradePrompt = FeatureAccessService.upgradePrompt(for: .extendedSubdomains) 1328 upgradePrompt = FeatureAccessService.upgradePrompt(for: capability.feature)
1334 return 1329 return
1335 } 1330 }
1336 guard extendedSubdomains.isEmpty else { return } 1331 guard self[keyPath: items].isEmpty else { return }
1337 1332
1338 extendedSubdomainsLoading = true 1333 self[keyPath: loading] = true
1339 extendedSubdomainsError = nil 1334 self[keyPath: error] = nil
1340 1335
1341 let outcome = await ExternalDataService.shared.extendedSubdomains( 1336 (self[keyPath: items], self[keyPath: error]) = found(await fetch(), fallback: fallback)
1342 domain: searchedDomain,
1343 existing: subdomains
1344 )
1345
1346 (extendedSubdomains, extendedSubdomainsError) = found(outcome.value, fallback: "Extended subdomains unavailable")
1347 1337
1348 extendedSubdomainsLoading = false 1338 self[keyPath: loading] = false
1349 _ = saveHistoryEntry(replaceLatest: true) 1339 _ = saveHistoryEntry(replaceLatest: true)
1350 } 1340 }
1351 1341
DomainDig/Models.swift +1 −15
@@ -1246,25 +1246,11 @@ struct MonitoringState: Codable, Equatable, Sendable {
1246} 1246}
1247 1247
1248struct MonitoringPendingAlert: Codable, Identifiable, Equatable, Sendable { 1248struct MonitoringPendingAlert: Codable, Identifiable, Equatable, Sendable {
1249 let id: UUID 1249 var id = UUID()
1250 var detectedAt: Date 1250 var detectedAt: Date
1251 var message: String 1251 var message: String
1252 var severity: MonitoringAlertSeverity 1252 var severity: MonitoringAlertSeverity
1253 var changeHash: String 1253 var changeHash: String
1254
1255 init(
1256 id: UUID = UUID(),
1257 detectedAt: Date,
1258 message: String,
1259 severity: MonitoringAlertSeverity,
1260 changeHash: String
1261 ) {
1262 self.id = id
1263 self.detectedAt = detectedAt
1264 self.message = message
1265 self.severity = severity
1266 self.changeHash = changeHash
1267 }
1268} 1254}
1269 1255
1270 1256
DomainDig/ScheduledReportService.swift +3 −22
@@ -31,25 +31,11 @@ struct ScheduledReportSettings: Codable, Equatable {
31} 31}
32 32
33struct ScheduledReportLog: Codable, Identifiable, Equatable { 33struct ScheduledReportLog: Codable, Identifiable, Equatable {
34 let id: UUID 34 var id = UUID()
35 let generatedAt: Date 35 let generatedAt: Date
36 let domainCount: Int 36 let domainCount: Int
37 let format: DomainExportFormat 37 let format: DomainExportFormat
38 let fileName: String 38 let fileName: String
39
40 init(
41 id: UUID = UUID(),
42 generatedAt: Date,
43 domainCount: Int,
44 format: DomainExportFormat,
45 fileName: String
46 ) {
47 self.id = id
48 self.generatedAt = generatedAt
49 self.domainCount = domainCount
50 self.format = format
51 self.fileName = fileName
52 }
53} 39}
54 40
55/// Local, UserDefaults-backed persistence for scheduled-report settings and 41/// Local, UserDefaults-backed persistence for scheduled-report settings and
@@ -245,13 +231,8 @@ final class ScheduledReportScheduler {
245 private func handleAppRefresh(task: BGAppRefreshTask) { 231 private func handleAppRefresh(task: BGAppRefreshTask) {
246 _ = syncSchedule() 232 _ = syncSchedule()
247 233
248 let worker = Task { 234 task.run {
249 let outcome = await ScheduledReportService.shared.generateReport(trigger: .background, requireEnabledSetting: true) 235 await ScheduledReportService.shared.generateReport(trigger: .background, requireEnabledSetting: true).success
250 task.setTaskCompleted(success: outcome.success)
251 }
252
253 task.expirationHandler = {
254 worker.cancel()
255 } 236 }
256 } 237 }
257 #endif 238 #endif