DomainDig/DomainDigApp.swift
99 lines · 4593 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 // The store is cleared as the view model's argument rather than in
18 // `init()`, because a property's default value is evaluated before the
19 // initialiser body — a reset there would run after the first read.
20 #if DEBUG
21 @State private var viewModel = DomainViewModel(defaults: AuditFixtures.resetStoreIfRequested())
22 #else
23 @State private var viewModel = DomainViewModel()
24 #endif
25 @State private var purchaseService = PurchaseService.shared
26 @State private var cloudSyncService = CloudSyncService.shared
27 @State private var localAPIService = LocalAPIService.shared
28
29 /// Falls back to the default when the tier does not include themes, so a
30 /// lapsed subscriber sees the stock palette rather than a theme they can no
31 /// longer change.
32 private var selectedAccent: AccentTheme {
33 guard FeatureAccessService.hasAccess(to: .accentTheme) else { return .default }
34 return AccentTheme(rawValue: accent) ?? .default
35 }
36
37 init() {
38 LocalNotificationService.shared.configureForegroundPresentation()
39 DomainMonitoringScheduler.shared.registerBackgroundTask()
40 ScheduledReportScheduler.shared.registerBackgroundTask()
41 }
42
43 var body: some Scene {
44 WindowGroup {
45 RootTabView(viewModel: viewModel)
46 .environment(\.appDensity, AppDensity(rawValue: density) ?? .compact)
47 .environment(\.theme, selectedAccent.theme)
48 .task { AppIconController.apply(selectedAccent) }
49 .onChange(of: accent) { _, _ in AppIconController.apply(selectedAccent) }
50 // So standard controls pick the accent up too, not only the
51 // views that name a token.
52 .tint(selectedAccent == .default ? nil : selectedAccent.theme.accentFill)
53 // The single place appearance is applied. Keep it that way.
54 .preferredColorScheme((AppAppearance(rawValue: appearance) ?? .system).colorScheme)
55 .task {
56 #if DEBUG
57 viewModel.seedAuditFixturesIfRequested()
58 #endif
59 await refreshAfterBecomingActive()
60 }
61 .onReceive(NotificationCenter.default.publisher(for: .cloudSyncDidApplyChanges)) { _ in
62 viewModel.refreshPersistedData()
63 viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
64 IntegrationService.shared.refresh()
65 }
66 }
67 .onChange(of: scenePhase) { _, newValue in
68 guard newValue == .active else { return }
69 Task { await refreshAfterBecomingActive() }
70 }
71 }
72
73 /// Everything that must re-run whenever the app becomes active.
74 ///
75 /// Launch and foreground carried near-duplicates of this, and the
76 /// foreground copy left out `refreshEntitlements()` — so a purchase or
77 /// cancellation made outside the app was not picked up until the next cold
78 /// launch.
79 private func refreshAfterBecomingActive() async {
80 await purchaseService.refreshEntitlements()
81 viewModel.refreshMonitoringState()
82 await viewModel.refreshMonitoringAuthorizationStatus()
83 await cloudSyncService.refreshAvailability()
84 localAPIService.refresh()
85 cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
86 viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
87 ScheduledReportScheduler.shared.syncSchedule()
88 IntegrationService.shared.processQueueNow()
89 viewModel.refreshWidgetData()
90 consumeShareInbox()
91 }
92
93 /// Picks up a domain shared via the share extension and routes it into an
94 /// inspection through the intent router (consumed by `RootTabView`).
95 private func consumeShareInbox() {
96 guard let domain = DomainDigShareInbox.consume() else { return }
97 DomainDigIntentRouter.shared.pendingAction = .inspect(domain)
98 }
99}