DomainDig/DomainDigApp.swift
92 lines · 4236 bytes
1//
2// DomainDigApp.swift
3// DomainDig
4//
5// Created by cmc on 2026-03-10.
6//
7
8import SwiftUI
9
10@main
11struct DomainDigApp: App {
12 @UIApplicationDelegateAdaptor(DomainDigAppDelegate.self) private var appDelegate
13 @Environment(\.scenePhase) private var scenePhase
14 @AppStorage(AppDensity.userDefaultsKey) private var density = AppDensity.compact.rawValue
15 @AppStorage(AppAppearance.userDefaultsKey) private var appearance = AppAppearance.system.rawValue
16 @AppStorage(AccentTheme.userDefaultsKey) private var accent = AccentTheme.default.rawValue
17 @State private var viewModel = DomainViewModel()
18 @State private var purchaseService = PurchaseService.shared
19 @State private var cloudSyncService = CloudSyncService.shared
20 @State private var localAPIService = LocalAPIService.shared
21
22 /// Falls back to the default when the tier does not include themes, so a
23 /// lapsed subscriber sees the stock palette rather than a theme they can no
24 /// longer change.
25 private var selectedAccent: AccentTheme {
26 guard FeatureAccessService.hasAccess(to: .accentTheme) else { return .default }
27 return AccentTheme(rawValue: accent) ?? .default
28 }
29
30 init() {
31 LocalNotificationService.shared.configureForegroundPresentation()
32 DomainMonitoringScheduler.shared.registerBackgroundTask()
33 ScheduledReportScheduler.shared.registerBackgroundTask()
34 }
35
36 var body: some Scene {
37 WindowGroup {
38 RootTabView(viewModel: viewModel)
39 .environment(\.appDensity, AppDensity(rawValue: density) ?? .compact)
40 .environment(\.theme, selectedAccent.theme)
41 .task { AppIconController.apply(selectedAccent) }
42 .onChange(of: accent) { _, _ in AppIconController.apply(selectedAccent) }
43 // So standard controls pick the accent up too, not only the
44 // views that name a token.
45 .tint(selectedAccent == .default ? nil : selectedAccent.theme.accentFill)
46 // The single place appearance is applied. Keep it that way.
47 .preferredColorScheme((AppAppearance(rawValue: appearance) ?? .system).colorScheme)
48 .task {
49 #if DEBUG
50 viewModel.seedAuditFixturesIfRequested()
51 #endif
52 await refreshAfterBecomingActive()
53 }
54 .onReceive(NotificationCenter.default.publisher(for: .cloudSyncDidApplyChanges)) { _ in
55 viewModel.refreshPersistedData()
56 viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
57 IntegrationService.shared.refresh()
58 }
59 }
60 .onChange(of: scenePhase) { _, newValue in
61 guard newValue == .active else { return }
62 Task { await refreshAfterBecomingActive() }
63 }
64 }
65
66 /// Everything that must re-run whenever the app becomes active.
67 ///
68 /// Launch and foreground carried near-duplicates of this, and the
69 /// foreground copy left out `refreshEntitlements()` — so a purchase or
70 /// cancellation made outside the app was not picked up until the next cold
71 /// launch.
72 private func refreshAfterBecomingActive() async {
73 await purchaseService.refreshEntitlements()
74 viewModel.refreshMonitoringState()
75 await viewModel.refreshMonitoringAuthorizationStatus()
76 await cloudSyncService.refreshAvailability()
77 localAPIService.refresh()
78 cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
79 viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
80 ScheduledReportScheduler.shared.syncSchedule()
81 IntegrationService.shared.processQueueNow()
82 viewModel.refreshWidgetData()
83 consumeShareInbox()
84 }
85
86 /// Picks up a domain shared via the share extension and routes it into an
87 /// inspection through the intent router (consumed by `RootTabView`).
88 private func consumeShareInbox() {
89 guard let domain = DomainDigShareInbox.consume() else { return }
90 DomainDigIntentRouter.shared.pendingAction = .inspect(domain)
91 }
92}