From 406732e4b99d43f37ba0e7cc23e4290a6d3165d3 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 28 Mar 2023 11:05:17 +0200 Subject: [PATCH] Fixes sound duration issue --- LeCountdown/Sound/Sound.swift | 6 +++--- LeCountdown/Sound/SoundPlayer.swift | 8 +++++++- LeCountdown/Subscription/AppGuard.swift | 2 +- LeCountdown/Utils/Preferences.swift | 2 +- LeCountdown/Views/HomeView.swift | 14 +++++--------- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/LeCountdown/Sound/Sound.swift b/LeCountdown/Sound/Sound.swift index 852ce99..5841a69 100644 --- a/LeCountdown/Sound/Sound.swift +++ b/LeCountdown/Sound/Sound.swift @@ -217,9 +217,9 @@ enum Sound: Int, CaseIterable, Identifiable, Localized { static func computeSoundDurationsIfNecessary() { Task { for sound in Sound.allCases { - if Preferences.soundDurations[sound.rawValue] == nil { + if Preferences.soundDurations[sound.fileName] == nil { if let duration = try? await sound.duration() { - Preferences.soundDurations[sound.rawValue] = duration + Preferences.soundDurations[sound.fileName] = duration } } } @@ -227,7 +227,7 @@ enum Sound: Int, CaseIterable, Identifiable, Localized { } var formattedDuration: String { - if let duration = Preferences.soundDurations[self.rawValue] { + if let duration = Preferences.soundDurations[self.fileName] { return duration.minuteSecond } else { return "" diff --git a/LeCountdown/Sound/SoundPlayer.swift b/LeCountdown/Sound/SoundPlayer.swift index c575f74..be98d88 100644 --- a/LeCountdown/Sound/SoundPlayer.swift +++ b/LeCountdown/Sound/SoundPlayer.swift @@ -59,7 +59,7 @@ enum SoundPlayerError : Error { player.prepareToPlay() player.volume = 1.0 player.delegate = self - + self._player = player Logger.log("Plays \(url) on player: \(String(describing: self._player))") @@ -78,4 +78,10 @@ enum SoundPlayerError : Error { Conductor.maestro.deactivateAudioSessionIfPossible() } + func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) { + if let error { + Logger.error(error) + } + } + } diff --git a/LeCountdown/Subscription/AppGuard.swift b/LeCountdown/Subscription/AppGuard.swift index 17cf484..550a3a0 100644 --- a/LeCountdown/Subscription/AppGuard.swift +++ b/LeCountdown/Subscription/AppGuard.swift @@ -124,7 +124,7 @@ extension Notification.Name { } var isAuthorized: Bool { - return false //self.currentPlan != .none + return self.currentPlan != .none } var currentPlan: StorePlan { diff --git a/LeCountdown/Utils/Preferences.swift b/LeCountdown/Utils/Preferences.swift index 0043d6e..dbe47a7 100644 --- a/LeCountdown/Utils/Preferences.swift +++ b/LeCountdown/Utils/Preferences.swift @@ -23,7 +23,7 @@ enum PreferenceKey: String { class Preferences { @UserDefault(PreferenceKey.countdowns.rawValue, defaultValue: [:]) static var savedCountdowns: [String : DateInterval] - @UserDefault(PreferenceKey.soundDurations.rawValue, defaultValue: [:]) static var soundDurations: [Int : TimeInterval] + @UserDefault(PreferenceKey.soundDurations.rawValue, defaultValue: [:]) static var soundDurations: [String : TimeInterval] @UserDefault(PreferenceKey.lastSoundPlayed.rawValue, defaultValue: [:]) static var lastSelectedSound: [String : Int] @UserDefault(PreferenceKey.tips.rawValue, defaultValue: nil) static var lastShownTip: Int? @UserDefault(PreferenceKey.timerSiriTips.rawValue, defaultValue: []) static var timerSiriTips: Set diff --git a/LeCountdown/Views/HomeView.swift b/LeCountdown/Views/HomeView.swift index 2f0db8e..9b2e6f8 100644 --- a/LeCountdown/Views/HomeView.swift +++ b/LeCountdown/Views/HomeView.swift @@ -94,15 +94,11 @@ struct RegularHomeView: View { } fileprivate func _setColumnVisibility() { - do { - let count = try viewContext.count(entityName: "AbstractTimer") - if count > 0 { - self.columnVisibility = .detailOnly - } else { - self.columnVisibility = .automatic - } - } catch { - Logger.error(error) + let count = viewContext.count(entityName: "AbstractTimer") + if count > 0 { + self.columnVisibility = .detailOnly + } else { + self.columnVisibility = .automatic } }