Laurent 3 years ago
parent 6fe943f15f
commit 37141bb579
  1. 57
      LeCountdown/Conductor.swift
  2. 4
      LeCountdown/Intent/StartTimerIntent.swift
  3. 22
      LeCountdown/LeCountdownApp.swift
  4. 7
      LeCountdown/Sound/DelaySoundPlayer.swift
  5. 19
      LeCountdown/Sound/SoundPlayer.swift
  6. 2
      LeCountdown/Utils/Preferences.swift

@ -122,27 +122,27 @@ class Conductor: ObservableObject {
// MARK: - Countdown // MARK: - Countdown
func startCountdown(_ date: Date, countdown: Countdown) { // func startCountdown(_ date: Date, countdown: Countdown) {
// DispatchQueue.main.async { //// DispatchQueue.main.async {
//
Logger.log("Starts countdown: \(countdown.displayName)") // Logger.log("Starts countdown: \(countdown.displayName)")
//
// cleanup existing countdowns // // cleanup existing countdowns
self.removeLiveTimer(id: countdown.stringId) // self.removeLiveTimer(id: countdown.stringId)
//
// self._cleanupTimers.removeValue(forKey: countdown.stringId) //// self._cleanupTimers.removeValue(forKey: countdown.stringId)
//
let dateInterval = DateInterval(start: Date(), end: date) // let dateInterval = DateInterval(start: Date(), end: date)
self.currentCountdowns[countdown.stringId] = dateInterval // self.currentCountdowns[countdown.stringId] = dateInterval
//
// self._launchLiveActivity(countdown: countdown, endDate: date) //// self._launchLiveActivity(countdown: countdown, endDate: date)
//
// self._createTimerIntent(countdown) //// self._createTimerIntent(countdown)
//
// Logger.log("countdowns count = \(self.currentCountdowns.count)") //// Logger.log("countdowns count = \(self.currentCountdowns.count)")
//
// } //// }
} // }
func cancelCountdown(id: TimerID) { func cancelCountdown(id: TimerID) {
CountdownScheduler.master.cancelCurrentNotifications(countdownId: id) CountdownScheduler.master.cancelCurrentNotifications(countdownId: id)
@ -167,7 +167,7 @@ class Conductor: ObservableObject {
do { do {
let date = Date(timeIntervalSinceNow: countdown.duration) let date = Date(timeIntervalSinceNow: countdown.duration)
self.startCountdown(date, countdown: countdown) self.removeLiveTimer(id: countdown.stringId)
let soundFile = try SoundFile(fullName: countdown.soundName) let soundFile = try SoundFile(fullName: countdown.soundName)
let soundPlayer = try DelaySoundPlayer(timerID: countdown.stringId, soundFile: soundFile) let soundPlayer = try DelaySoundPlayer(timerID: countdown.stringId, soundFile: soundFile)
@ -175,6 +175,9 @@ class Conductor: ObservableObject {
try soundPlayer.start(in: countdown.duration, try soundPlayer.start(in: countdown.duration,
repeatCount: Int(countdown.repeatCount)) repeatCount: Int(countdown.repeatCount))
let dateInterval = DateInterval(start: Date(), end: date)
self.currentCountdowns[countdown.stringId] = dateInterval
if Preferences.playConfirmationSound { if Preferences.playConfirmationSound {
self._playSound(Const.confirmationSound.rawValue) self._playSound(Const.confirmationSound.rawValue)
} }
@ -315,11 +318,11 @@ class Conductor: ObservableObject {
func deactivateAudioSessionIfPossible() { func deactivateAudioSessionIfPossible() {
if self._delayedSoundPlayers.isEmpty { if self._delayedSoundPlayers.isEmpty {
do { // do {
try AVAudioSession.sharedInstance().setActive(false) // try AVAudioSession.sharedInstance().setActive(false)
} catch { // } catch {
Logger.error(error) // Logger.error(error)
} // }
} }
} }

@ -10,7 +10,7 @@ import AppIntents
import SwiftUI import SwiftUI
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) @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 let intentClassName = "StartTimerIntent"
static var title: LocalizedStringResource = "Launch Timer" static var title: LocalizedStringResource = "Launch Timer"
@ -23,6 +23,8 @@ struct StartTimerIntent: AppIntent, CustomIntentMigratedAppIntent {
Summary("") Summary("")
} }
static var openAppWhenRun: Bool = true
func perform() async throws -> some IntentResult & ProvidesDialog & ShowsSnippetView { func perform() async throws -> some IntentResult & ProvidesDialog & ShowsSnippetView {
let timerIdentifier: TimerIdentifierAppEntity let timerIdentifier: TimerIdentifierAppEntity

@ -25,18 +25,30 @@ struct LeCountdownApp: App {
UIPageControl.appearance().pageIndicatorTintColor = UIColor(white: 0.7, alpha: 1.0) UIPageControl.appearance().pageIndicatorTintColor = UIColor(white: 0.7, alpha: 1.0)
self._registerBackgroundRefreshes() 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 { do {
try persistenceController.container.initializeCloudKitSchema() let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback)
try audioSession.setActive(true)
} catch { } catch {
print("ERROR \(error)") Logger.error(error)
} }
} }
@Environment(\.scenePhase) var scenePhase @Environment(\.scenePhase) var scenePhase

@ -35,15 +35,16 @@ import AVFoundation
fileprivate func _play(in duration: TimeInterval, repeatCount: Int) throws { fileprivate func _play(in duration: TimeInterval, repeatCount: Int) throws {
let audioSession: AVAudioSession = AVAudioSession.sharedInstance() // let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback, options: .duckOthers) // try audioSession.setCategory(.playback, options: .duckOthers)
try audioSession.setActive(true) // try audioSession.setActive(true)
self._player.prepareToPlay() self._player.prepareToPlay()
self._player.volume = 1.0 self._player.volume = 1.0
self._player.delegate = self self._player.delegate = self
self._player.numberOfLoops = repeatCount self._player.numberOfLoops = repeatCount
Logger.log("self._player.deviceCurrentTime = \(self._player.deviceCurrentTime)")
self._player.play(atTime: self._player.deviceCurrentTime + duration) self._player.play(atTime: self._player.deviceCurrentTime + duration)
} }

@ -51,17 +51,20 @@ enum SoundPlayerError : Error {
throw SoundPlayerError.missingResourceError(file: soundFile) throw SoundPlayerError.missingResourceError(file: soundFile)
} }
let audioSession: AVAudioSession = AVAudioSession.sharedInstance() // let audioSession: AVAudioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(.playback) // try audioSession.setCategory(.playback)
try audioSession.setActive(true) // try audioSession.setActive(true)
_player = try AVAudioPlayer(contentsOf: url) let player = try AVAudioPlayer(contentsOf: url)
_player?.prepareToPlay() player.prepareToPlay()
_player?.volume = 1.0 player.volume = 1.0
_player?.delegate = self player.delegate = self
self._player = player
Logger.log("Plays \(url) on player: \(String(describing: self._player))") Logger.log("Plays \(url) on player: \(String(describing: self._player))")
_player?.play() Logger.log("SoundPlayer > .deviceCurrentTime = \(player.deviceCurrentTime)")
player.play()
} }

@ -16,6 +16,7 @@ enum PreferenceKey: String {
case lastSoundPlayed case lastSoundPlayed
case tips case tips
case timerSiriTips case timerSiriTips
case cloudKitSchemaInitialized
} }
class Preferences { class Preferences {
@ -26,6 +27,7 @@ class Preferences {
@UserDefault(PreferenceKey.tips.rawValue, defaultValue: nil) static var lastShownTip: Int? @UserDefault(PreferenceKey.tips.rawValue, defaultValue: nil) static var lastShownTip: Int?
@UserDefault(PreferenceKey.timerSiriTips.rawValue, defaultValue: []) static var timerSiriTips: Set<String> @UserDefault(PreferenceKey.timerSiriTips.rawValue, defaultValue: []) static var timerSiriTips: Set<String>
@UserDefault(PreferenceKey.playConfirmationSound.rawValue, defaultValue: true) static var playConfirmationSound: Bool @UserDefault(PreferenceKey.playConfirmationSound.rawValue, defaultValue: true) static var playConfirmationSound: Bool
@UserDefault(PreferenceKey.cloudKitSchemaInitialized.rawValue, defaultValue: false) static var cloudKitSchemaInitialized: Bool
static var hideSilentModeAlerts: Bool { static var hideSilentModeAlerts: Bool {
return UserDefaults.standard.bool(forKey: PreferenceKey.showSilentModeAlert.rawValue) return UserDefaults.standard.bool(forKey: PreferenceKey.showSilentModeAlert.rawValue)

Loading…
Cancel
Save