DomainDig/DomainDigApp.swift

77 lines · 3334 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    @State private var viewModel = DomainViewModel()
17    @State private var purchaseService = PurchaseService.shared
18    @State private var cloudSyncService = CloudSyncService.shared
19    @State private var localAPIService = LocalAPIService.shared
20
21    init() {
22        LocalNotificationService.shared.configureForegroundPresentation()
23        DomainMonitoringScheduler.shared.registerBackgroundTask()
24        ScheduledReportScheduler.shared.registerBackgroundTask()
25    }
26
27    var body: some Scene {
28        WindowGroup {
29            RootTabView(viewModel: viewModel)
30                .environment(\.appDensity, AppDensity(rawValue: density) ?? .compact)
31                // The single place appearance is applied. Keep it that way.
32                .preferredColorScheme((AppAppearance(rawValue: appearance) ?? .system).colorScheme)
33                .task {
34                    #if DEBUG
35                    viewModel.seedAuditFixturesIfRequested()
36                    #endif
37                    await refreshAfterBecomingActive()
38                }
39                .onReceive(NotificationCenter.default.publisher(for: .cloudSyncDidApplyChanges)) { _ in
40                    viewModel.refreshPersistedData()
41                    viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
42                    IntegrationService.shared.refresh()
43                }
44        }
45        .onChange(of: scenePhase) { _, newValue in
46            guard newValue == .active else { return }
47            Task { await refreshAfterBecomingActive() }
48        }
49    }
50
51    /// Everything that must re-run whenever the app becomes active.
52    ///
53    /// Launch and foreground carried near-duplicates of this, and the
54    /// foreground copy left out `refreshEntitlements()` — so a purchase or
55    /// cancellation made outside the app was not picked up until the next cold
56    /// launch.
57    private func refreshAfterBecomingActive() async {
58        await purchaseService.refreshEntitlements()
59        viewModel.refreshMonitoringState()
60        await viewModel.refreshMonitoringAuthorizationStatus()
61        await cloudSyncService.refreshAvailability()
62        localAPIService.refresh()
63        cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
64        viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
65        ScheduledReportScheduler.shared.syncSchedule()
66        IntegrationService.shared.processQueueNow()
67        viewModel.refreshWidgetData()
68        consumeShareInbox()
69    }
70
71    /// Picks up a domain shared via the share extension and routes it into an
72    /// inspection through the intent router (consumed by `RootTabView`).
73    private func consumeShareInbox() {
74        guard let domain = DomainDigShareInbox.consume() else { return }
75        DomainDigIntentRouter.shared.pendingAction = .inspect(domain)
76    }
77}