import SwiftUI import DevPlaceSwiftSDK struct NotificationSettingsView: View { @Environment(\.api) var api var body: some View { NotificationSettingsViewContent(viewModel: .init(api: api)) } } private struct NotificationSettingsViewContent: View { @State var viewModel: NotificationSettingsView.ViewModel @State private var resetConfirmationPresented = false var body: some View { content() .screenStyle() .navigationTitle(Text("Notifications")) .alert($viewModel.alertMessage) .task { await viewModel.load() } } @ViewBuilder private func content() -> some View { if viewModel.isLoaded { Form { displaySection() ForEach(viewModel.preferences) { preference in preferenceSection(preference) } resetSection() } .scrollContentBackground(.hidden) } else { ProgressView() } } @ViewBuilder private func displaySection() -> some View { Section { Toggle( isOn: Binding( get: { viewModel.groupingEnabled }, set: { newValue in Task { await viewModel.setGrouping(newValue) } }, ) ) { Label { Text("Group similar notifications") } icon: { Image(systemName: "rectangle.stack") } } .tint(Color.accentColor) Toggle( isOn: Binding( get: { viewModel.throttlingEnabled }, set: { newValue in Task { await viewModel.setThrottling(newValue) } }, ) ) { Label { Text("Throttle repeat alerts") } icon: { Image(systemName: "timer") } } .tint(Color.accentColor) } footer: { Text("Grouped notifications share one entry and the badge counts groups. Throttling sends at most one push per similar event group every 5 minutes. In-app notifications are never throttled.") } .listRowBackground(Color.BG_1) } @ViewBuilder private func preferenceSection(_ preference: NotificationPreference) -> some View { Section { ForEach(NotificationChannel.allCases, id: \.self) { channel in Toggle( isOn: Binding( get: { viewModel.isEnabled(channel, in: preference) }, set: { newValue in Task { await viewModel.set(channel, enabled: newValue, forKey: preference.key) } }, ) ) { Text(title(for: channel)) } .tint(Color.accentColor) .disabled(channel == .telegram && !viewModel.telegramPaired) } } header: { Text(preference.label) } footer: { Text(preference.description) } .listRowBackground(Color.BG_1) } @ViewBuilder private func resetSection() -> some View { Section { Button(role: .destructive) { resetConfirmationPresented = true } label: { Label { Text("Reset notification types") } icon: { Image(systemName: "arrow.counterclockwise") } } .buttonStyle(.form) .disabled(!viewModel.hasCustomizedPreferences) .alert( "Reset all notification types to their defaults?", isPresented: $resetConfirmationPresented ) { Button(role: .cancel, action: {}) Button("Reset", role: .destructive) { Task { await viewModel.resetPreferences() } } } } footer: { if !viewModel.telegramPaired { Text("Telegram notifications need a paired Telegram chat. Connect Telegram from your profile on the web.") } } .listRowBackground(Color.BG_1) } private func title(for channel: NotificationChannel) -> String { switch channel { case .inApp: "In-app" case .push: "Push" case .telegram: "Telegram" } } } #Preview { NavigationStack { NotificationSettingsView() } .environment(\.api, .mock) }