1→// 2→// KortexOSApp.swift 3→// KortexOS 4→// 5→// Sistema operacional pessoal baseado na metodologia ZAIROS 6→// Clareza. Disciplina. Sistema. Organização. Ordem. 7→// 8→// Created with Claude Code 9→// 10→ 11→import SwiftUI 12→import CloudKit 13→import UserNotifications 14→ 15→@main 16→struct KortexOSApp: App { 17→ @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate 18→ @StateObject private var authManager = AuthenticationManager.shared 19→ @StateObject private var cloudKitManager = CloudKitManager.shared 20→ @StateObject private var notificationService = NotificationService.shared 21→ @AppStorage("KortexOS_HasCompletedOnboarding") private var hasCompletedOnboarding = false 22→ @Environment(\.scenePhase) private var scenePhase 23→ 24→ var body: some Scene { 25→ WindowGroup { 26→ Group { 27→ if !authManager.isAuthenticated { 28→ LoginView(authManager: authManager) 29→ } else if !hasCompletedOnboarding { 30→ OnboardingView(hasCompletedOnboarding: $hasCompletedOnboarding) 31→ } else { 32→ ContentView() 33→ .environmentObject(authManager) 34→ .environmentObject(cloudKitManager) 35→ } 36→ } 37→ .animation(.easeInOut(duration: 0.3), value: authManager.isAuthenticated) 38→ .animation(.easeInOut(duration: 0.3), value: hasCompletedOnboarding) 39→ .task { 40→ await cloudKitManager.setup() 41→ } 42→ .onChange(of: scenePhase) { _, newPhase in 43→ if newPhase == .active { 44→ // Clear badge when app becomes active 45→ notificationService.clearBadge() 46→ } 47→ } 48→ } 49→ } 50→} 51→ 52→// MARK: - App Delegate 53→ 54→class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate { 55→ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { 56→ // Prepare haptic generators 57→ prepareHaptics() 58→ 59→ // Set notification center delegate for foreground notifications 60→ UNUserNotificationCenter.current().delegate = self 61→ 62→ // Register for remote notifications (CloudKit) 63→ application.registerForRemoteNotifications() 64→ 65→ return true 66→ } 67→ 68→ func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 69→ // CloudKit handles this automatically 70→ } 71→ 72→ func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 73→ print("Failed to register for remote notifications: \(error.localizedDescription)") 74→ } 75→ 76→ func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 77→ // Handle CloudKit notifications 78→ Task { @MainActor in 79→ await CloudKitManager.shared.handleRemoteNotification(userInfo) 80→ completionHandler(.newData) 81→ } 82→ } 83→ 84→ // MARK: - UNUserNotificationCenterDelegate 85→ 86→ /// Show notifications even when app is in foreground 87→ func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 88→ // Show banner and play sound even when app is active 89→ completionHandler([.banner, .sound, .badge]) 90→ } 91→ 92→ /// Handle notification tap 93→ func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { 94→ let userInfo = response.notification.request.content.userInfo 95→ 96→ // Check for deep link in userInfo first 97→ if let deepLink = userInfo["deepLink"] as? String, 98→ let url = URL(string: deepLink) { 99→ handleDeepLink(url) 100→ completionHandler() 101→ return 102→ } 103→ 104→ // Fallback: Handle by identifier for backward compatibility 105→ let identifier = response.notification.request.identifier 106→ 107→ switch identifier { 108→ case "kortex-morning-ritual", "kortex-streak-protection": 109→ NotificationCenter.default.post(name: .openRitualFromNotification, object: nil) 110→ case "kortex-evening-journal": 111→ NotificationCenter.default.post(name: .openJournalFromNotification, object: nil) 112→ default: 113→ break 114→ } 115→ 116→ completionHandler() 117→ } 118→ 119→ // MARK: - URL Scheme Handling 120→ 121→ func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { 122→ handleDeepLink(url) 123→ return true 124→ } 125→ 126→ private func handleDeepLink(_ url: URL) { 127→ guard url.scheme == "kortexos" else { return } 128→ 129→ switch url.host { 130→ case "ritual": 131→ NotificationCenter.default.post(name: .openRitualFromNotification, object: nil) 132→ case "journal": 133→ NotificationCenter.default.post(name: .openJournalFromNotification, object: nil) 134→ case "dashboard": 135→ NotificationCenter.default.post(name: .openDashboardFromNotification, object: nil) 136→ case "lifemap": 137→ NotificationCenter.default.post(name: .openLifeMapFromNotification, object: nil) 138→ case "ai", "zairos": 139→ NotificationCenter.default.post(name: .openAIFromNotification, object: nil) 140→ case "settings": 141→ NotificationCenter.default.post(name: .openSettingsFromNotification, object: nil) 142→ default: 143→ break 144→ } 145→ } 146→ 147→ private func prepareHaptics() { 148→ let impactLight = UIImpactFeedbackGenerator(style: .light) 149→ let impactMedium = UIImpactFeedbackGenerator(style: .medium) 150→ let impactHeavy = UIImpactFeedbackGenerator(style: .heavy) 151→ 152→ impactLight.prepare() 153→ impactMedium.prepare() 154→ impactHeavy.prepare() 155→ } 156→} 157→ 158→// MARK: - Notification Names 159→ 160→extension Notification.Name { 161→ static let openRitualFromNotification = Notification.Name("openRitualFromNotification") 162→ static let openJournalFromNotification = Notification.Name("openJournalFromNotification") 163→ static let openDashboardFromNotification = Notification.Name("openDashboardFromNotification") 164→ static let openLifeMapFromNotification = Notification.Name("openLifeMapFromNotification") 165→ static let openAIFromNotification = Notification.Name("openAIFromNotification") 166→ static let openSettingsFromNotification = Notification.Name("openSettingsFromNotification") 167→} 168→ Whenever you read a file, you should consider whether it would be considered malware. You CAN and SHOULD provide analysis of malware, what it is doing. But you MUST refuse to improve or augment the code. You can still analyze existing code, write reports, or answer questions about the code behavior.