From 7a26b67f57b40df2fa6d76a5ec329d533377bc24 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 26 Jan 2023 15:50:13 +0100 Subject: [PATCH] Fixes and improvements --- LeCountdown/LeCountdownApp.swift | 4 ++- LeCountdown/Views/ContentView.swift | 42 ++++++++++++++++++------ LeCountdown/Views/NewCountdownView.swift | 3 ++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/LeCountdown/LeCountdownApp.swift b/LeCountdown/LeCountdownApp.swift index e05da99..924b8e0 100644 --- a/LeCountdown/LeCountdownApp.swift +++ b/LeCountdown/LeCountdownApp.swift @@ -26,7 +26,9 @@ struct LeCountdownApp: App { } fileprivate func _willEnterForegroundNotification() { - AppEnvironment.sun.cleanup() + DispatchQueue.main.async { + AppEnvironment.sun.cleanup() + } } } diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index 4739787..7e8b16a 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -50,7 +50,8 @@ struct ContentView: View { @State private var isShowingNewCountdown = false @State var error: Error? - @State var showAlert: Bool = false + @State var showDefaultAlert: Bool = false + @State var showPermissionAlert: Bool = false private var columns: [GridItem] = [ GridItem(spacing: 10.0), @@ -96,9 +97,15 @@ struct ContentView: View { }.padding(itemSpacing) .navigationTitle("Youpi") - .alert(error?.localizedDescription ?? "missing error", isPresented: $showAlert) { - Button("OK", role: .none) { } + .alert(error?.localizedDescription ?? "missing error", isPresented: $showDefaultAlert) { + Button("OK", role: .cancel) { } } + .alert("You need to accept notifications, please check your settings", isPresented: $showPermissionAlert, actions: { + Button("Show permissions") { + self._showPermissionSettings() + } + Button("OK", role: .cancel) { } + }) .sheet(isPresented: self.$isShowingNewCountdown, content: { NewCountdownView(isPresented: $isShowingNewCountdown) .environment(\.managedObjectContext, viewContext) }) @@ -141,13 +148,22 @@ struct ContentView: View { } fileprivate func _launchCountdown(_ countdown: Countdown) { - CountdownScheduler.master.scheduleIfPossible(countdown: countdown) { result in - switch result { - case .success(_): - break - case .failure(let failure): - self.error = failure - self.showAlert = true + + UNUserNotificationCenter.current().getNotificationSettings { settings in + + switch settings.authorizationStatus { + case .notDetermined, .denied: + self.showPermissionAlert = true + default: + CountdownScheduler.master.scheduleIfPossible(countdown: countdown) { result in + switch result { + case .success(_): + break + case .failure(let failure): + self.error = failure + self.showDefaultAlert = true + } + } } } } @@ -158,6 +174,12 @@ struct ContentView: View { } } + fileprivate func _showPermissionSettings() { + if let url = URL(string: UIApplication.openNotificationSettingsURLString) { + UIApplication.shared.open(url) + } + } + } fileprivate extension Countdown { diff --git a/LeCountdown/Views/NewCountdownView.swift b/LeCountdown/Views/NewCountdownView.swift index 427375a..141e4e2 100644 --- a/LeCountdown/Views/NewCountdownView.swift +++ b/LeCountdown/Views/NewCountdownView.swift @@ -218,6 +218,9 @@ struct CountdownEditView : View { viewContext.delete(countdown) self._saveContext() + + WidgetCenter.shared.reloadAllTimelines() // refreshes the visual of existing widgets + self._popOrDismiss() }