|
| 1 | +import os.log |
| 2 | +import SwiftUI |
| 3 | +import UserNotifications |
| 4 | + |
| 5 | +/// Notification permission page for onboarding flow. |
| 6 | +/// |
| 7 | +/// Allows users to enable native macOS notifications for VibeTunnel events |
| 8 | +/// during the welcome flow. Users can grant permissions or skip and enable later. |
| 9 | +struct NotificationPermissionPageView: View { |
| 10 | + private let notificationService = NotificationService.shared |
| 11 | + @State private var isRequestingPermission = false |
| 12 | + @State private var permissionStatus: UNAuthorizationStatus = .notDetermined |
| 13 | + |
| 14 | + private let logger = Logger( |
| 15 | + subsystem: "sh.vibetunnel.vibetunnel", |
| 16 | + category: "NotificationPermissionPageView" |
| 17 | + ) |
| 18 | + |
| 19 | + #if DEBUG |
| 20 | + init(permissionStatus: UNAuthorizationStatus = .notDetermined) { |
| 21 | + self.permissionStatus = permissionStatus |
| 22 | + } |
| 23 | + #endif |
| 24 | + |
| 25 | + var body: some View { |
| 26 | + VStack(spacing: 30) { |
| 27 | + VStack(spacing: 16) { |
| 28 | + Text("Enable Notifications") |
| 29 | + .font(.largeTitle) |
| 30 | + .fontWeight(.semibold) |
| 31 | + |
| 32 | + Text( |
| 33 | + "Get notified about session events, command completions, and errors. You can customize which notifications to receive in Settings." |
| 34 | + ) |
| 35 | + .font(.body) |
| 36 | + .foregroundColor(.secondary) |
| 37 | + .multilineTextAlignment(.center) |
| 38 | + .frame(maxWidth: 480) |
| 39 | + .fixedSize(horizontal: false, vertical: true) |
| 40 | + |
| 41 | + if permissionStatus != .denied { |
| 42 | + // Notification examples |
| 43 | + VStack(alignment: .leading, spacing: 12) { |
| 44 | + Label("Session starts and exits", systemImage: "terminal") |
| 45 | + Label("Command completions and errors", systemImage: "exclamationmark.triangle") |
| 46 | + Label("Terminal bell events", systemImage: "bell") |
| 47 | + } |
| 48 | + .font(.callout) |
| 49 | + .foregroundColor(.secondary) |
| 50 | + .padding() |
| 51 | + .background(Color(NSColor.controlBackgroundColor)) |
| 52 | + .cornerRadius(8) |
| 53 | + .frame(maxWidth: 400) |
| 54 | + } |
| 55 | + |
| 56 | + // Permission button/status |
| 57 | + if permissionStatus == .authorized { |
| 58 | + HStack { |
| 59 | + Image(systemName: "checkmark.circle.fill") |
| 60 | + .foregroundColor(.green) |
| 61 | + Text("Notifications enabled") |
| 62 | + .foregroundColor(.secondary) |
| 63 | + } |
| 64 | + .font(.body) |
| 65 | + .frame(height: 32) |
| 66 | + } else if permissionStatus == .denied { |
| 67 | + VStack(spacing: 8) { |
| 68 | + HStack { |
| 69 | + Image(systemName: "exclamationmark.triangle.fill") |
| 70 | + .foregroundColor(.orange) |
| 71 | + Text("Notifications are disabled") |
| 72 | + .foregroundColor(.secondary) |
| 73 | + } |
| 74 | + .font(.body) |
| 75 | + |
| 76 | + Button("Open System Settings") { |
| 77 | + notificationService.openNotificationSettings() |
| 78 | + } |
| 79 | + .buttonStyle(.borderedProminent) |
| 80 | + .frame(height: 32) |
| 81 | + } |
| 82 | + } else { |
| 83 | + Button(action: requestNotificationPermission) { |
| 84 | + if isRequestingPermission { |
| 85 | + ProgressView() |
| 86 | + .scaleEffect(0.5) |
| 87 | + .frame(width: 8, height: 8) |
| 88 | + } else { |
| 89 | + Text("Enable Notifications") |
| 90 | + } |
| 91 | + } |
| 92 | + .buttonStyle(.borderedProminent) |
| 93 | + .disabled(isRequestingPermission) |
| 94 | + .frame(height: 32) |
| 95 | + } |
| 96 | + } |
| 97 | + Spacer() |
| 98 | + } |
| 99 | + .padding() |
| 100 | + .task { |
| 101 | + if !isRunningPreviews() { |
| 102 | + await checkNotificationPermission() |
| 103 | + } |
| 104 | + } |
| 105 | + .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in |
| 106 | + // Check permissions when returning from System Settings |
| 107 | + Task { |
| 108 | + await checkNotificationPermission() |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private func checkNotificationPermission() async { |
| 114 | + permissionStatus = await notificationService.authorizationStatus() |
| 115 | + } |
| 116 | + |
| 117 | + private func requestNotificationPermission() { |
| 118 | + Task { |
| 119 | + isRequestingPermission = true |
| 120 | + defer { isRequestingPermission = false } |
| 121 | + _ = try? await notificationService.requestAuthorization() |
| 122 | + // Update permission status after request |
| 123 | + await checkNotificationPermission() |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +#Preview("Not determined") { |
| 129 | + NotificationPermissionPageView(permissionStatus: .notDetermined) |
| 130 | + .frame(width: 640, height: 480) |
| 131 | + .background(Color(NSColor.windowBackgroundColor)) |
| 132 | +} |
| 133 | + |
| 134 | +#Preview("Authorized") { |
| 135 | + NotificationPermissionPageView(permissionStatus: .authorized) |
| 136 | + .frame(width: 640, height: 480) |
| 137 | + .background(Color(NSColor.windowBackgroundColor)) |
| 138 | +} |
| 139 | + |
| 140 | +#Preview("Permissions denied") { |
| 141 | + NotificationPermissionPageView(permissionStatus: .denied) |
| 142 | + .frame(width: 640, height: 480) |
| 143 | + .background(Color(NSColor.windowBackgroundColor)) |
| 144 | +} |
0 commit comments