From 4902513bd9fdd5fccdbfb0c92cb2f902b391af01 Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 11 Oct 2023 10:28:31 +0200 Subject: [PATCH] Multiple bug fixes + new settings for volume --- LeCountdown/AppDelegate.swift | 5 +++- LeCountdown/Conductor.swift | 23 ++++++++++++++++-- LeCountdown/Utils/Preferences.swift | 2 ++ LeCountdown/Views/Reusable/VolumeView.swift | 5 ++++ LeCountdown/Views/SettingsView.swift | 27 +++++++++++++++------ 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/LeCountdown/AppDelegate.swift b/LeCountdown/AppDelegate.swift index b31349f..d545ba9 100644 --- a/LeCountdown/AppDelegate.swift +++ b/LeCountdown/AppDelegate.swift @@ -46,6 +46,10 @@ class AppDelegate : NSObject, UIApplicationDelegate { func applicationWillTerminate(_ application: UIApplication) { Logger.log("applicationWillTerminate") FileLogger.log("applicationWillTerminate") + + Conductor.removeLiveActivities() + +// Conductor.maestro.removeLiveActivities() } func applicationDidReceiveMemoryWarning(_ application: UIApplication) { @@ -66,7 +70,6 @@ class AppDelegate : NSObject, UIApplicationDelegate { } func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool { - if userActivity.interaction == nil { Logger.log("restorationHandler called! interaction is nil") return false diff --git a/LeCountdown/Conductor.swift b/LeCountdown/Conductor.swift index 48e9e06..d091d54 100644 --- a/LeCountdown/Conductor.swift +++ b/LeCountdown/Conductor.swift @@ -206,8 +206,11 @@ class Conductor: ObservableObject { FileLogger.log("Cancel \(self._timerName(id))") CountdownScheduler.master.cancelCurrentNotifications(countdownId: id) + self.currentCountdowns.removeValue(forKey: id) + + self.removeLiveTimer(id: id) self.cancelSoundPlayer(id: id) - self.cancelledCountdowns.append(id) + self._recordAndRemoveCountdown(countdownId: id, cancel: true) self.pausedCountdowns.removeValue(forKey: id) @@ -496,7 +499,23 @@ class Conductor: ObservableObject { } } - + + class func removeLiveActivities() { + print("Ending Live Activities") + let semaphore = DispatchSemaphore(value: 0) + Task.detached(priority: .high) { + print("Task") + for activity in ActivityKit.Activity.activities { + print("Ending Live Activity: \(activity.id)") + if #available(iOS 16.2, *) { + await activity.end(nil, dismissalPolicy: .immediate) + } + } + semaphore.signal() + } + semaphore.wait() + } + fileprivate func _liveActivity(timerId: String) -> [ActivityKit.Activity] { return ActivityKit.Activity.activities.filter { $0.attributes.id == timerId } } diff --git a/LeCountdown/Utils/Preferences.swift b/LeCountdown/Utils/Preferences.swift index ec415cc..1edd313 100644 --- a/LeCountdown/Utils/Preferences.swift +++ b/LeCountdown/Utils/Preferences.swift @@ -23,6 +23,7 @@ enum PreferenceKey: String { case timerSiriTips case cloudKitSchemaInitialized case defaultVolume + case raiseSoundOnLaunch } class Preferences { @@ -40,6 +41,7 @@ class Preferences { @UserDefault(PreferenceKey.defaultVolume.rawValue, defaultValue: 0.5) static var defaultVolume: Float @UserDefault(PreferenceKey.installDate.rawValue, defaultValue: nil) static var installDate: Date? @UserDefault(PreferenceKey.hasShownStartView.rawValue, defaultValue: false) static var hasShownStartView: Bool + @UserDefault(PreferenceKey.raiseSoundOnLaunch.rawValue, defaultValue: false) static var raiseSoundOnLaunch: Bool static var hideSilentModeAlerts: Bool { return UserDefaults.standard.bool(forKey: PreferenceKey.showSilentModeAlert.rawValue) diff --git a/LeCountdown/Views/Reusable/VolumeView.swift b/LeCountdown/Views/Reusable/VolumeView.swift index 986ec7a..ea8fdfc 100644 --- a/LeCountdown/Views/Reusable/VolumeView.swift +++ b/LeCountdown/Views/Reusable/VolumeView.swift @@ -36,6 +36,11 @@ struct VolumeView: UIViewRepresentable { } fileprivate func _setVolume(volumeView: MPVolumeView) { + + guard Preferences.raiseSoundOnLaunch else { + return + } + if let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { diff --git a/LeCountdown/Views/SettingsView.swift b/LeCountdown/Views/SettingsView.swift index 3bec49f..97df472 100644 --- a/LeCountdown/Views/SettingsView.swift +++ b/LeCountdown/Views/SettingsView.swift @@ -11,6 +11,7 @@ import MessageUI struct SettingsView: View { @State var confirmationSound = Preferences.playConfirmationSound + @State var raiseSoundOnLaunch = Preferences.raiseSoundOnLaunch @State var cancellationSound = Preferences.playCancellationSound @State var defaultVolume: Float = Preferences.defaultVolume @@ -29,14 +30,24 @@ struct SettingsView: View { // .onChange(of: self.cancellationSound) { newValue in // Preferences.playCancellationSound = newValue // } - HStack { - Text("Default Volume") - Spacer() - Slider(value: self.$defaultVolume) - .onChange(of: self.defaultVolume, perform: { newValue in - Preferences.defaultVolume = self.defaultVolume - }) - .frame(width: 120.0) + + + Toggle("Raise sound on launch", isOn: self.$raiseSoundOnLaunch) + .onChange(of: self.raiseSoundOnLaunch) { newValue in + Preferences.raiseSoundOnLaunch = newValue + } + + + if self.raiseSoundOnLaunch { + HStack { + Text("Default Volume") + Spacer() + Slider(value: self.$defaultVolume) + .onChange(of: self.defaultVolume, perform: { newValue in + Preferences.defaultVolume = self.defaultVolume + }) + .frame(width: 120.0) + } } if MFMailComposeViewController.canSendMail() {