aboutsummaryrefslogtreecommitdiff
path: root/DomainDig/DomainDigApp.swift
blob: d392ddef8526d00da4274c04ae5a238a7959fa9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
//  DomainDigApp.swift
//  DomainDig
//
//  Created by cmc on 2026-03-10.
//

import SwiftUI

@main
struct DomainDigApp: App {
    @UIApplicationDelegateAdaptor(DomainDigAppDelegate.self) private var appDelegate
    @Environment(\.scenePhase) private var scenePhase
    @AppStorage(AppDensity.userDefaultsKey) private var density = AppDensity.compact.rawValue
    @AppStorage(AppAppearance.userDefaultsKey) private var appearance = AppAppearance.system.rawValue
    @State private var viewModel = DomainViewModel()
    @State private var purchaseService = PurchaseService.shared
    @State private var cloudSyncService = CloudSyncService.shared
    @State private var localAPIService = LocalAPIService.shared

    init() {
        LocalNotificationService.shared.configureForegroundPresentation()
        DomainMonitoringScheduler.shared.registerBackgroundTask()
        ScheduledReportScheduler.shared.registerBackgroundTask()
    }

    var body: some Scene {
        WindowGroup {
            RootTabView(viewModel: viewModel)
                .environment(\.appDensity, AppDensity(rawValue: density) ?? .compact)
                // The single place appearance is applied. Keep it that way.
                .preferredColorScheme((AppAppearance(rawValue: appearance) ?? .system).colorScheme)
                .task {
                    let _ = purchaseService.currentTier
                    let _ = cloudSyncService.status
                    let _ = localAPIService.isRunning
                    let _ = IntegrationService.shared.targets.count
                    await purchaseService.refreshEntitlements()
                    viewModel.refreshMonitoringState()
                    await viewModel.refreshMonitoringAuthorizationStatus()
                    await cloudSyncService.refreshAvailability()
                    localAPIService.refresh()
                    cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
                    viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
                    ScheduledReportScheduler.shared.syncSchedule()
                    IntegrationService.shared.processQueueNow()
                    viewModel.refreshWidgetData()
                    consumeShareInbox()
                }
                .onReceive(NotificationCenter.default.publisher(for: .cloudSyncDidApplyChanges)) { _ in
                    viewModel.refreshPersistedData()
                    viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
                    IntegrationService.shared.refresh()
                }
        }
        .onChange(of: scenePhase) { _, newValue in
            guard newValue == .active else { return }
            viewModel.refreshMonitoringState()
            Task {
                await viewModel.refreshMonitoringAuthorizationStatus()
                await cloudSyncService.refreshAvailability()
            }
            localAPIService.refresh()
            cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
            viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
            ScheduledReportScheduler.shared.syncSchedule()
            IntegrationService.shared.processQueueNow()
            viewModel.refreshWidgetData()
            consumeShareInbox()
        }
    }

    /// Picks up a domain shared via the share extension and routes it into an
    /// inspection through the intent router (consumed by `RootTabView`).
    private func consumeShareInbox() {
        guard let domain = DomainDigShareInbox.consume() else { return }
        DomainDigIntentRouter.shared.pendingAction = .inspect(domain)
    }
}