|
|
|
|
@ -20,15 +20,13 @@ fileprivate enum Const: String { |
|
|
|
|
case confirmationSound = "PVP_Stab_Oneshot_Bleep_Em.wav" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
typealias TimerID = String |
|
|
|
|
|
|
|
|
|
class Conductor: ObservableObject { |
|
|
|
|
|
|
|
|
|
static let maestro: Conductor = Conductor() |
|
|
|
|
|
|
|
|
|
@Published var soundPlayer: SoundPlayer? = nil |
|
|
|
|
|
|
|
|
|
var delayedSoundPlayers: [TimerID : DelaySoundPlayer] = [:] |
|
|
|
|
fileprivate var _delayedSoundPlayers: [TimerID : DelaySoundPlayer] = [:] |
|
|
|
|
|
|
|
|
|
@UserDefault(PreferenceKey.countdowns.rawValue, defaultValue: [:]) static var savedCountdowns: [String : DateInterval] |
|
|
|
|
@UserDefault(PreferenceKey.stopwatches.rawValue, defaultValue: [:]) static var savedStopwatches: [String : Date] |
|
|
|
|
@ -62,8 +60,13 @@ class Conductor: ObservableObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func removeLiveTimer(id: String) { |
|
|
|
|
|
|
|
|
|
Logger.log("removeLiveTimer") |
|
|
|
|
self.liveTimers.removeAll(where: { $0.id == id }) |
|
|
|
|
self.cancelledCountdowns.removeAll(where: { $0 == id }) |
|
|
|
|
if let soundPlayer = self._delayedSoundPlayers[id] { |
|
|
|
|
soundPlayer.stop() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _buildLiveTimers() { |
|
|
|
|
@ -162,17 +165,15 @@ class Conductor: ObservableObject { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let date = Date(timeIntervalSinceNow: countdown.duration) |
|
|
|
|
|
|
|
|
|
self.startCountdown(date, countdown: countdown) |
|
|
|
|
|
|
|
|
|
let soundFile = try SoundFile(fullName: countdown.soundName) |
|
|
|
|
|
|
|
|
|
let soundPlayer = DelaySoundPlayer(soundFile: soundFile) |
|
|
|
|
|
|
|
|
|
self.delayedSoundPlayers[countdown.stringId] = soundPlayer |
|
|
|
|
|
|
|
|
|
let soundPlayer = try DelaySoundPlayer(timerID: countdown.stringId, soundFile: soundFile) |
|
|
|
|
self._delayedSoundPlayers[countdown.stringId] = soundPlayer |
|
|
|
|
try soundPlayer.start(in: countdown.duration, |
|
|
|
|
repeatCount: Int(countdown.repeatCount)) |
|
|
|
|
|
|
|
|
|
self.startCountdown(date, countdown: countdown) |
|
|
|
|
|
|
|
|
|
if Preferences.playConfirmationSound { |
|
|
|
|
self._playSound(Const.confirmationSound.rawValue) |
|
|
|
|
} |
|
|
|
|
@ -215,6 +216,28 @@ class Conductor: ObservableObject { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func restore() { |
|
|
|
|
|
|
|
|
|
for (countdownId, interval) in self.currentCountdowns { |
|
|
|
|
if !self._delayedSoundPlayers.contains(where: { $0.key == countdownId }) { |
|
|
|
|
|
|
|
|
|
let context = PersistenceController.shared.container.viewContext |
|
|
|
|
if let countdown = context.object(stringId: countdownId) as? Countdown { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let soundFile = try SoundFile(fullName: countdown.soundName) |
|
|
|
|
let soundPlayer = try DelaySoundPlayer(timerID: countdownId, soundFile: soundFile) |
|
|
|
|
self._delayedSoundPlayers[countdown.stringId] = soundPlayer |
|
|
|
|
try soundPlayer.restore(for: interval.end, repeatCount: Int(countdown.repeatCount)) |
|
|
|
|
} catch { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Cleanup |
|
|
|
|
|
|
|
|
|
func cleanup() { |
|
|
|
|
@ -273,9 +296,9 @@ class Conductor: ObservableObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func cancelSoundPlayer(id: TimerID) { |
|
|
|
|
if let soundPlayer = self.delayedSoundPlayers[id] { |
|
|
|
|
if let soundPlayer = self._delayedSoundPlayers[id] { |
|
|
|
|
soundPlayer.stop() |
|
|
|
|
self.delayedSoundPlayers.removeValue(forKey: id) |
|
|
|
|
self._delayedSoundPlayers.removeValue(forKey: id) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|