// // AppRouter.swift // Bible-Week // // Navegação global do app e tratamento do toque em notificações: // tocar no lembrete abre diretamente o Plano de Leitura → Leitura de Hoje. // import Foundation import Observation import UserNotifications @MainActor @Observable final class AppRouter { enum Tab: Hashable { case bible case plan } var selectedTab: Tab = .bible /// A tela principal do plano já é a "Leitura de Hoje". func openTodayReading() { selectedTab = .plan } } /// Delegate do UNUserNotificationCenter. Precisa ser registrado no launch /// (init do App) para capturar o toque na notificação mesmo com o app fechado. final class NotificationCoordinator: NSObject, UNUserNotificationCenterDelegate { private let router: AppRouter init(router: AppRouter) { self.router = router } /// Usuário tocou na notificação (app fechado, em background ou aberto). func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse ) async { let userInfo = response.notification.request.content.userInfo let destination = userInfo[NotificationUserInfo.destinationKey] as? String guard destination == NotificationUserInfo.destinationReadingPlan else { return } await MainActor.run { router.openTodayReading() } } /// Notificação chegou com o app em foreground: apresentação normal, /// sem navegar automaticamente nem interromper uma leitura em andamento. func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification ) async -> UNNotificationPresentationOptions { [.banner, .sound] } }