diff --git a/damus/Views/Settings/NotificationSettingsView.swift b/damus/Views/Settings/NotificationSettingsView.swift index 7af78d9a..f2711156 100644 --- a/damus/Views/Settings/NotificationSettingsView.swift +++ b/damus/Views/Settings/NotificationSettingsView.swift @@ -10,6 +10,7 @@ import SwiftUI struct NotificationSettingsView: View { let damus_state: DamusState @ObservedObject var settings: UserSettingsStore + @State var notification_mode_setting_error: String? = nil @Environment(\.dismiss) var dismiss @@ -25,27 +26,56 @@ struct NotificationSettingsView: View { }) } + func try_to_set_notifications_mode(new_value: UserSettingsStore.NotificationsMode) { + notification_mode_setting_error = nil + if new_value == .push { + Task { + do { + try await damus_state.push_notification_client.send_token() + settings.notifications_mode = new_value + } + catch { + notification_mode_setting_error = String(format: NSLocalizedString("Error configuring push notifications with the server: %@", comment: "Error label shown when user tries to enable push notifications but something fails"), error.localizedDescription) + } + } + } + else { + Task { + do { + try await damus_state.push_notification_client.revoke_token() + settings.notifications_mode = new_value + } + catch { + notification_mode_setting_error = String(format: NSLocalizedString("Error disabling push notifications with the server: %@", comment: "Error label shown when user tries to disable push notifications but something fails"), error.localizedDescription) + } + } + } + } + var body: some View { Form { if settings.enable_experimental_push_notifications { - Picker(NSLocalizedString("Notifications mode", comment: "Prompt selection of the notification mode (Feature to switch between local notifications (generated from user's own phone) or push notifications (generated by Damus server)."), - selection: Binding( - get: { settings.notifications_mode }, - set: { newValue in - settings.notifications_mode = newValue - if newValue == .push { - Task { try await damus_state.push_notification_client.send_token() } - } - else { - Task { try await damus_state.push_notification_client.revoke_token() } - } + Section( + header: Text("General", comment: "Section header for general damus notifications user configuration"), + footer: VStack { + if let notification_mode_setting_error { + Text(notification_mode_setting_error) + .foregroundStyle(.damusDangerPrimary) } - ) + } ) { - ForEach(UserSettingsStore.NotificationsMode.allCases, id: \.self) { notification_mode in - Text(notification_mode.text_description()) - .tag(notification_mode.rawValue) - + Picker(NSLocalizedString("Notifications mode", comment: "Prompt selection of the notification mode (Feature to switch between local notifications (generated from user's own phone) or push notifications (generated by Damus server)."), + selection: Binding( + get: { settings.notifications_mode }, + set: { newValue in + self.try_to_set_notifications_mode(new_value: newValue) + } + ) + ) { + ForEach(UserSettingsStore.NotificationsMode.allCases, id: \.self) { notification_mode in + Text(notification_mode.text_description()) + .tag(notification_mode.rawValue) + } } } }