diff --git a/LeCountdown/Conductor.swift b/LeCountdown/Conductor.swift index a3e9d2e..c178306 100644 --- a/LeCountdown/Conductor.swift +++ b/LeCountdown/Conductor.swift @@ -122,27 +122,27 @@ class Conductor: ObservableObject { // MARK: - Countdown - func startCountdown(_ date: Date, countdown: Countdown) { -// DispatchQueue.main.async { - - Logger.log("Starts countdown: \(countdown.displayName)") - - // cleanup existing countdowns - self.removeLiveTimer(id: countdown.stringId) - -// self._cleanupTimers.removeValue(forKey: countdown.stringId) - - let dateInterval = DateInterval(start: Date(), end: date) - self.currentCountdowns[countdown.stringId] = dateInterval - -// self._launchLiveActivity(countdown: countdown, endDate: date) - -// self._createTimerIntent(countdown) - -// Logger.log("countdowns count = \(self.currentCountdowns.count)") - -// } - } +// func startCountdown(_ date: Date, countdown: Countdown) { +//// DispatchQueue.main.async { +// +// Logger.log("Starts countdown: \(countdown.displayName)") +// +// // cleanup existing countdowns +// self.removeLiveTimer(id: countdown.stringId) +// +//// self._cleanupTimers.removeValue(forKey: countdown.stringId) +// +// let dateInterval = DateInterval(start: Date(), end: date) +// self.currentCountdowns[countdown.stringId] = dateInterval +// +//// self._launchLiveActivity(countdown: countdown, endDate: date) +// +//// self._createTimerIntent(countdown) +// +//// Logger.log("countdowns count = \(self.currentCountdowns.count)") +// +//// } +// } func cancelCountdown(id: TimerID) { CountdownScheduler.master.cancelCurrentNotifications(countdownId: id) @@ -167,14 +167,17 @@ class Conductor: ObservableObject { do { let date = Date(timeIntervalSinceNow: countdown.duration) - self.startCountdown(date, countdown: countdown) - + self.removeLiveTimer(id: countdown.stringId) + let soundFile = try SoundFile(fullName: countdown.soundName) let soundPlayer = try DelaySoundPlayer(timerID: countdown.stringId, soundFile: soundFile) self._delayedSoundPlayers[countdown.stringId] = soundPlayer try soundPlayer.start(in: countdown.duration, repeatCount: Int(countdown.repeatCount)) + let dateInterval = DateInterval(start: Date(), end: date) + self.currentCountdowns[countdown.stringId] = dateInterval + if Preferences.playConfirmationSound { self._playSound(Const.confirmationSound.rawValue) } @@ -315,11 +318,11 @@ class Conductor: ObservableObject { func deactivateAudioSessionIfPossible() { if self._delayedSoundPlayers.isEmpty { - do { - try AVAudioSession.sharedInstance().setActive(false) - } catch { - Logger.error(error) - } +// do { +// try AVAudioSession.sharedInstance().setActive(false) +// } catch { +// Logger.error(error) +// } } } diff --git a/LeCountdown/Intent/StartTimerIntent.swift b/LeCountdown/Intent/StartTimerIntent.swift index 9541955..9d3426a 100644 --- a/LeCountdown/Intent/StartTimerIntent.swift +++ b/LeCountdown/Intent/StartTimerIntent.swift @@ -10,7 +10,7 @@ import AppIntents import SwiftUI @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) -struct StartTimerIntent: AppIntent, CustomIntentMigratedAppIntent { +struct StartTimerIntent: AudioStartingIntent, CustomIntentMigratedAppIntent { static let intentClassName = "StartTimerIntent" static var title: LocalizedStringResource = "Launch Timer" @@ -22,7 +22,9 @@ struct StartTimerIntent: AppIntent, CustomIntentMigratedAppIntent { static var parameterSummary: some ParameterSummary { Summary("") } - + + static var openAppWhenRun: Bool = true + func perform() async throws -> some IntentResult & ProvidesDialog & ShowsSnippetView { let timerIdentifier: TimerIdentifierAppEntity diff --git a/LeCountdown/LeCountdownApp.swift b/LeCountdown/LeCountdownApp.swift index 64b0497..b283982 100644 --- a/LeCountdown/LeCountdownApp.swift +++ b/LeCountdown/LeCountdownApp.swift @@ -25,18 +25,30 @@ struct LeCountdownApp: App { UIPageControl.appearance().pageIndicatorTintColor = UIColor(white: 0.7, alpha: 1.0) self._registerBackgroundRefreshes() - self._initSchema() + self._initSchemaIfNeeded() + self._activateAudioSession() } - fileprivate func _initSchema() { - + fileprivate func _initSchemaIfNeeded() { + if !Preferences.cloudKitSchemaInitialized { + do { + try persistenceController.container.initializeCloudKitSchema() + Preferences.cloudKitSchemaInitialized = true + } catch { + print("ERROR \(error)") + } + } + } + + fileprivate func _activateAudioSession() { do { - try persistenceController.container.initializeCloudKitSchema() + let audioSession: AVAudioSession = AVAudioSession.sharedInstance() + try audioSession.setCategory(.playback) + try audioSession.setActive(true) } catch { - print("ERROR \(error)") + Logger.error(error) } - } @Environment(\.scenePhase) var scenePhase diff --git a/LeCountdown/Sound/DelaySoundPlayer.swift b/LeCountdown/Sound/DelaySoundPlayer.swift index 901df87..140d3cd 100644 --- a/LeCountdown/Sound/DelaySoundPlayer.swift +++ b/LeCountdown/Sound/DelaySoundPlayer.swift @@ -35,15 +35,16 @@ import AVFoundation fileprivate func _play(in duration: TimeInterval, repeatCount: Int) throws { - let audioSession: AVAudioSession = AVAudioSession.sharedInstance() - try audioSession.setCategory(.playback, options: .duckOthers) - try audioSession.setActive(true) +// let audioSession: AVAudioSession = AVAudioSession.sharedInstance() +// try audioSession.setCategory(.playback, options: .duckOthers) +// try audioSession.setActive(true) self._player.prepareToPlay() self._player.volume = 1.0 self._player.delegate = self self._player.numberOfLoops = repeatCount + Logger.log("self._player.deviceCurrentTime = \(self._player.deviceCurrentTime)") self._player.play(atTime: self._player.deviceCurrentTime + duration) } diff --git a/LeCountdown/Sound/SoundPlayer.swift b/LeCountdown/Sound/SoundPlayer.swift index ac8b868..c575f74 100644 --- a/LeCountdown/Sound/SoundPlayer.swift +++ b/LeCountdown/Sound/SoundPlayer.swift @@ -51,17 +51,20 @@ enum SoundPlayerError : Error { throw SoundPlayerError.missingResourceError(file: soundFile) } - let audioSession: AVAudioSession = AVAudioSession.sharedInstance() - try audioSession.setCategory(.playback) - try audioSession.setActive(true) +// let audioSession: AVAudioSession = AVAudioSession.sharedInstance() +// try audioSession.setCategory(.playback) +// try audioSession.setActive(true) - _player = try AVAudioPlayer(contentsOf: url) - _player?.prepareToPlay() - _player?.volume = 1.0 - _player?.delegate = self + let player = try AVAudioPlayer(contentsOf: url) + player.prepareToPlay() + player.volume = 1.0 + player.delegate = self + + self._player = player Logger.log("Plays \(url) on player: \(String(describing: self._player))") - _player?.play() + Logger.log("SoundPlayer > .deviceCurrentTime = \(player.deviceCurrentTime)") + player.play() } diff --git a/LeCountdown/Utils/Preferences.swift b/LeCountdown/Utils/Preferences.swift index c145d80..2387f48 100644 --- a/LeCountdown/Utils/Preferences.swift +++ b/LeCountdown/Utils/Preferences.swift @@ -16,6 +16,7 @@ enum PreferenceKey: String { case lastSoundPlayed case tips case timerSiriTips + case cloudKitSchemaInitialized } class Preferences { @@ -26,6 +27,7 @@ class Preferences { @UserDefault(PreferenceKey.tips.rawValue, defaultValue: nil) static var lastShownTip: Int? @UserDefault(PreferenceKey.timerSiriTips.rawValue, defaultValue: []) static var timerSiriTips: Set @UserDefault(PreferenceKey.playConfirmationSound.rawValue, defaultValue: true) static var playConfirmationSound: Bool + @UserDefault(PreferenceKey.cloudKitSchemaInitialized.rawValue, defaultValue: false) static var cloudKitSchemaInitialized: Bool static var hideSilentModeAlerts: Bool { return UserDefaults.standard.bool(forKey: PreferenceKey.showSilentModeAlert.rawValue)