krz/domain-dig

an ios app for DNS & SSL analysis

clone: git clone https://gitbay.org/krz/domain-dig.git

v4.6.0: DomainDig/DomainDigApp.swift · raw

 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    @State private var viewModel = DomainViewModel()
16    @State private var purchaseService = PurchaseService.shared
17    @State private var cloudSyncService = CloudSyncService.shared
18    @State private var localAPIService = LocalAPIService.shared
19
20    init() {
21        LocalNotificationService.shared.configureForegroundPresentation()
22        DomainMonitoringScheduler.shared.registerBackgroundTask()
23    }
24
25    var body: some Scene {
26        WindowGroup {
27            RootTabView(viewModel: viewModel)
28                .environment(\.appDensity, AppDensity(rawValue: density) ?? .compact)
29                .task {
30                    let _ = purchaseService.currentTier
31                    let _ = cloudSyncService.status
32                    let _ = localAPIService.isRunning
33                    let _ = IntegrationService.shared.targets.count
34                    await purchaseService.refreshEntitlements()
35                    viewModel.refreshMonitoringState()
36                    await viewModel.refreshMonitoringAuthorizationStatus()
37                    await cloudSyncService.refreshAvailability()
38                    localAPIService.refresh()
39                    cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
40                    viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
41                    IntegrationService.shared.processQueueNow()
42                    viewModel.refreshWidgetData()
43                    consumeShareInbox()
44                }
45                .onReceive(NotificationCenter.default.publisher(for: .cloudSyncDidApplyChanges)) { _ in
46                    viewModel.refreshPersistedData()
47                    viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
48                    IntegrationService.shared.refresh()
49                }
50        }
51        .onChange(of: scenePhase) { _, newValue in
52            guard newValue == .active else { return }
53            viewModel.refreshMonitoringState()
54            Task {
55                await viewModel.refreshMonitoringAuthorizationStatus()
56                await cloudSyncService.refreshAvailability()
57            }
58            localAPIService.refresh()
59            cloudSyncService.scheduleSyncIfNeeded(trigger: .launch)
60            viewModel.monitoringStatusMessage = DomainMonitoringScheduler.shared.syncSchedule()
61            IntegrationService.shared.processQueueNow()
62            viewModel.refreshWidgetData()
63            consumeShareInbox()
64        }
65    }
66
67    /// Picks up a domain shared via the share extension and routes it into an
68    /// inspection through the intent router (consumed by `RootTabView`).
69    private func consumeShareInbox() {
70        guard let domain = DomainDigShareInbox.consume() else { return }
71        DomainDigIntentRouter.shared.pendingAction = .inspect(domain)
72    }
73}