From afbcd4b5d5df2079734faf0f08818106636863ff Mon Sep 17 00:00:00 2001 From: Laurent Date: Mon, 22 May 2023 14:30:51 +0200 Subject: [PATCH] Stability improvements --- LeCountdown.xcodeproj/project.pbxproj | 6 +++++ LeCountdown/Conductor.swift | 34 +++++++++++---------------- LeCountdown/CountdownScheduler.swift | 8 ++++--- LeCountdown/TimerRouter.swift | 3 +++ LeCountdown/Utils/AppError.swift | 2 +- LeCountdown/Utils/BoringContext.swift | 31 ++++++++++++++++++++++++ LeCountdown/Views/ContentView.swift | 11 +-------- 7 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 LeCountdown/Utils/BoringContext.swift diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index a7522d9..d3bc0dc 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -44,6 +44,8 @@ C4286EA62A150A7E0070D075 /* TimePickerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286EA52A150A7E0070D075 /* TimePickerView.swift */; }; C4286EAD2A1775390070D075 /* AppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286E952A14EC4E0070D075 /* AppError.swift */; }; C4286EAE2A17753A0070D075 /* AppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286E952A14EC4E0070D075 /* AppError.swift */; }; + C4286EB02A1B75AB0070D075 /* BoringContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286EAF2A1B75AB0070D075 /* BoringContext.swift */; }; + C4286EB22A1B75C60070D075 /* BoringContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286EAF2A1B75AB0070D075 /* BoringContext.swift */; }; C42E96FB29E59E72005B1B8C /* BackgroundBlurView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42E96FA29E59E72005B1B8C /* BackgroundBlurView.swift */; }; C42E96FD29E5B06D005B1B8C /* ActivityCalendarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C42E96FC29E5B06D005B1B8C /* ActivityCalendarView.swift */; }; C42E96FE29E5B5CD005B1B8C /* Filter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4BA2B6429A3C37D00CB4FBA /* Filter.swift */; }; @@ -377,6 +379,7 @@ C4286E952A14EC4E0070D075 /* AppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppError.swift; sourceTree = ""; }; C4286E9F2A1502FD0070D075 /* Stopwatch+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Stopwatch+CoreDataClass.swift"; sourceTree = ""; }; C4286EA52A150A7E0070D075 /* TimePickerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimePickerView.swift; sourceTree = ""; }; + C4286EAF2A1B75AB0070D075 /* BoringContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoringContext.swift; sourceTree = ""; }; C42E96FA29E59E72005B1B8C /* BackgroundBlurView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundBlurView.swift; sourceTree = ""; }; C42E96FC29E5B06D005B1B8C /* ActivityCalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityCalendarView.swift; sourceTree = ""; }; C42E970129E6B32B005B1B8C /* CalendarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarView.swift; sourceTree = ""; }; @@ -752,6 +755,7 @@ children = ( C4286E952A14EC4E0070D075 /* AppError.swift */, C4BA2AFC299A3A3700CB4FBA /* AppleMusicPlayer.swift */, + C4286EAF2A1B75AB0070D075 /* BoringContext.swift */, C4556F7029E40DCF00DEB40B /* Codable+Extensions.swift */, C4742B5629840F6400D5D950 /* CoolPic.swift */, C4BA2B6929A4BE1800CB4FBA /* Date+Extensions.swift */, @@ -1299,6 +1303,7 @@ C42E970229E6B32B005B1B8C /* CalendarView.swift in Sources */, C4BA2B0F299BE61E00CB4FBA /* Interval+CoreDataClass.swift in Sources */, C4F8B164298A9A92005C86A5 /* AlarmFormView.swift in Sources */, + C4286EB02A1B75AB0070D075 /* BoringContext.swift in Sources */, C40FDB622992985C0042A390 /* TextToSpeechRecorder.swift in Sources */, C4BA2B43299FCB2B00CB4FBA /* RecordsView.swift in Sources */, C46926BD29DDC49E0003E310 /* SubscriptionButtonView.swift in Sources */, @@ -1433,6 +1438,7 @@ C438C81A2982BFF100BF3EF9 /* NSManagedContext+Extensions.swift in Sources */, C4F8B19D298AC288005C86A5 /* AbstractTimer+CoreDataClass.swift in Sources */, C4BA2B3C299F838000CB4FBA /* Model+SharedExtensions.swift in Sources */, + C4286EB22A1B75C60070D075 /* BoringContext.swift in Sources */, C4A16D9929C4B06400143D5E /* StatePlayer.swift in Sources */, C4BA2AF82996A4F000CB4FBA /* CustomSound+CoreDataProperties.swift in Sources */, C4F8B199298AC288005C86A5 /* Record+CoreDataClass.swift in Sources */, diff --git a/LeCountdown/Conductor.swift b/LeCountdown/Conductor.swift index 7ac6f79..8c0972f 100644 --- a/LeCountdown/Conductor.swift +++ b/LeCountdown/Conductor.swift @@ -159,24 +159,22 @@ class Conductor: ObservableObject { func startCountdown(countdown: Countdown, handler: @escaping (Result) -> Void) { - DispatchQueue.main.async { - - let countdownId = countdown.stringId - self._cleanupPreviousTimerIfNecessary(countdownId) - - do { - let end = try self._scheduleSoundPlayer(countdown: countdown, in: countdown.duration) - if Preferences.playConfirmationSound { - self._playConfirmationSound(timer: countdown) - } - - handler(.success(end)) - } catch { - FileLogger.log("start error : \(error.localizedDescription)") - Logger.error(error) - handler(.failure(error)) + let countdownId = countdown.stringId + self._cleanupPreviousTimerIfNecessary(countdownId) + + do { + let end = try self._scheduleSoundPlayer(countdown: countdown, in: countdown.duration) + if Preferences.playConfirmationSound { + self._playConfirmationSound(timer: countdown) } + + handler(.success(end)) + } catch { + FileLogger.log("start error : \(error.localizedDescription)") + Logger.error(error) + handler(.failure(error)) } + } fileprivate func _scheduleSoundPlayer(countdown: Countdown, in interval: TimeInterval) throws -> Date { @@ -446,10 +444,6 @@ class Conductor: ObservableObject { } } -// func isSoundPlaying(_ sound: Sound) -> Bool { -// return self.soundPlayer.isSoundPlaying(sound) -// } - func stopMainPlayersIfPossible() { self.soundPlayer.stop() } diff --git a/LeCountdown/CountdownScheduler.swift b/LeCountdown/CountdownScheduler.swift index 0f80630..49ca3b7 100644 --- a/LeCountdown/CountdownScheduler.swift +++ b/LeCountdown/CountdownScheduler.swift @@ -15,9 +15,11 @@ class CountdownScheduler { static let notificationIdSeparator: String = "||" func scheduleIfPossible(countdown: Countdown, handler: @escaping (Result) -> Void) { - self.cancelCurrentNotifications(countdownId: countdown.stringId) - Conductor.maestro.startCountdown(countdown: countdown, handler: handler) - self._scheduleCountdownNotification(countdown: countdown, handler: handler) + DispatchQueue.main.async { + self.cancelCurrentNotifications(countdownId: countdown.stringId) + Conductor.maestro.startCountdown(countdown: countdown, handler: handler) + self._scheduleCountdownNotification(countdown: countdown, handler: handler) + } } fileprivate func _scheduleCountdownNotification(countdown: Countdown, handler: @escaping (Result) -> Void) { diff --git a/LeCountdown/TimerRouter.swift b/LeCountdown/TimerRouter.swift index 7107b1d..c4692c2 100644 --- a/LeCountdown/TimerRouter.swift +++ b/LeCountdown/TimerRouter.swift @@ -31,6 +31,9 @@ class TimerRouter { } static func performAction(timer: AbstractTimer, handler: @escaping (Result) -> Void) { + + BoringContext.main.reset() // should put the app back on the main screen + switch timer { case let countdown as Countdown: self._launchCountdown(countdown.stringId, handler: handler) diff --git a/LeCountdown/Utils/AppError.swift b/LeCountdown/Utils/AppError.swift index bd8967e..357d390 100644 --- a/LeCountdown/Utils/AppError.swift +++ b/LeCountdown/Utils/AppError.swift @@ -31,7 +31,7 @@ enum AppError: LocalizedError { return error.localizedDescription case .timerNotFound: return "Timer not found" - case .timerNotManaged(let timer): + case .timerNotManaged: return "Timer not managed" } } diff --git a/LeCountdown/Utils/BoringContext.swift b/LeCountdown/Utils/BoringContext.swift new file mode 100644 index 0000000..fa2e63f --- /dev/null +++ b/LeCountdown/Utils/BoringContext.swift @@ -0,0 +1,31 @@ +// +// BoringContext.swift +// LeCountdown +// +// Created by Laurent Morvillier on 22/05/2023. +// + +import Foundation +import SwiftUI + +class BoringContext : ObservableObject { + + static let main: BoringContext = BoringContext() + + @Published var isShowingNewData = false + @Published var error: Error? + @Published var showDefaultAlert: Bool = false + @Published var showPermissionAlert: Bool = false + @Published var siriTimer: AbstractTimer? = nil + + func reset() { + DispatchQueue.main.async { + self.isShowingNewData = false + self.showDefaultAlert = false + self.showPermissionAlert = false + } + } + +} + + diff --git a/LeCountdown/Views/ContentView.swift b/LeCountdown/Views/ContentView.swift index 0a9fa54..dfa2223 100644 --- a/LeCountdown/Views/ContentView.swift +++ b/LeCountdown/Views/ContentView.swift @@ -9,21 +9,13 @@ import SwiftUI import CoreData import Combine -class BoringContext : ObservableObject { - @Published var isShowingNewData = false - @Published var error: Error? - @Published var showDefaultAlert: Bool = false - @Published var showPermissionAlert: Bool = false - @Published var siriTimer: AbstractTimer? = nil -} - struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext @EnvironmentObject var conductor: Conductor @Environment(\.colorScheme) var colorScheme - @StateObject var boringContext: BoringContext = BoringContext() + @StateObject var boringContext: BoringContext = BoringContext.main @State private var isEditing: Bool = false @@ -33,7 +25,6 @@ struct ContentView: View { @State private var showSettingsSheet: Bool = false @State private var showStatsSheet: Bool = false - @State private var showSubscriptionSheet: Bool = false var body: some View {