From cd34b42882dacd6fcd11e1e50bae0753d5a189e5 Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 19 May 2023 11:44:27 +0200 Subject: [PATCH] minor improvements --- LeCountdown.xcodeproj/project.pbxproj | 6 +++++- LeCountdown/Intent/StartTimerIntent.swift | 8 ++++---- LeCountdown/Intent/TimerIdentifierAppEntity.swift | 2 +- LeCountdown/Model/NSManagedContext+Extensions.swift | 5 +---- LeCountdown/TimerRouter.swift | 1 + LeCountdown/Utils/AppError.swift | 5 +++++ 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/LeCountdown.xcodeproj/project.pbxproj b/LeCountdown.xcodeproj/project.pbxproj index b0d42c6..a7522d9 100644 --- a/LeCountdown.xcodeproj/project.pbxproj +++ b/LeCountdown.xcodeproj/project.pbxproj @@ -42,6 +42,8 @@ C4286EA32A1503320070D075 /* Stopwatch+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286E9F2A1502FD0070D075 /* Stopwatch+CoreDataClass.swift */; }; C4286EA42A1503330070D075 /* Stopwatch+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4286E9F2A1502FD0070D075 /* Stopwatch+CoreDataClass.swift */; }; 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 */; }; 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 */; }; @@ -748,6 +750,7 @@ C438C80A2981DE1A00BF3EF9 /* Utils */ = { isa = PBXGroup; children = ( + C4286E952A14EC4E0070D075 /* AppError.swift */, C4BA2AFC299A3A3700CB4FBA /* AppleMusicPlayer.swift */, C4556F7029E40DCF00DEB40B /* Codable+Extensions.swift */, C4742B5629840F6400D5D950 /* CoolPic.swift */, @@ -763,7 +766,6 @@ C4060DF4297AE9A7003FAB80 /* TimeInterval+Extensions.swift */, C473C33B29ACEC4F0056B38A /* Tip.swift */, C4BA2B7829A65C1400CB4FBA /* UIDevice+Extensions.swift */, - C4286E952A14EC4E0070D075 /* AppError.swift */, ); path = Utils; sourceTree = ""; @@ -1360,6 +1362,7 @@ C47C933729F01B7A00C780E2 /* Codable+Extensions.swift in Sources */, C438C7D62981216200BF3EF9 /* LaunchWidgetBundle.swift in Sources */, C4BA2B16299BE6A000CB4FBA /* IntervalGroup+CoreDataClass.swift in Sources */, + C4286EAE2A17753A0070D075 /* AppError.swift in Sources */, C4F8B18B298AC288005C86A5 /* Record+CoreDataClass.swift in Sources */, C4F8B195298AC288005C86A5 /* Activity+CoreDataClass.swift in Sources */, C4BA2B22299BE82E00CB4FBA /* AbstractSoundTimer+CoreDataProperties.swift in Sources */, @@ -1454,6 +1457,7 @@ C4A16DC929D311C800143D5E /* Extensions.swift in Sources */, C473C2F529A8DAF30056B38A /* PropertyWrappers.swift in Sources */, C473C2F029A8CFFC0056B38A /* TimerRouter.swift in Sources */, + C4286EAD2A1775390070D075 /* AppError.swift in Sources */, C438C800298130E900BF3EF9 /* IntentDataProvider.swift in Sources */, C473C33A29ACDBD70056B38A /* TipView.swift in Sources */, ); diff --git a/LeCountdown/Intent/StartTimerIntent.swift b/LeCountdown/Intent/StartTimerIntent.swift index beb6319..83938ac 100644 --- a/LeCountdown/Intent/StartTimerIntent.swift +++ b/LeCountdown/Intent/StartTimerIntent.swift @@ -36,16 +36,16 @@ struct StartTimerIntent: AudioStartingIntent { if let abstractTimer = IntentDataProvider.main.timer(id: timerIdentifier.id) { do { - let _ = try await TimerRouter.performAction(timer: abstractTimer) - print("perform() success !") + _ = try await TimerRouter.performAction(timer: abstractTimer) return .result(value: 1) } catch { Logger.error(error) // let dialog: IntentDialog = "\(error.localizedDescription)" - return .result(value: 0) + throw error } + } else { + throw AppError.timerNotFound(id: timerIdentifier.id) } - return .result(value: 0) } func timerEntities() -> [TimerIdentifierAppEntity] { diff --git a/LeCountdown/Intent/TimerIdentifierAppEntity.swift b/LeCountdown/Intent/TimerIdentifierAppEntity.swift index 25c1169..5904081 100644 --- a/LeCountdown/Intent/TimerIdentifierAppEntity.swift +++ b/LeCountdown/Intent/TimerIdentifierAppEntity.swift @@ -10,7 +10,7 @@ import AppIntents @available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) struct TimerIdentifierAppEntity: AppEntity { - static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Timer Identifier") + static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Timer Identifier") struct TimerIdentifierAppEntityQuery: EntityQuery { func entities(for identifiers: [TimerIdentifierAppEntity.ID]) async throws -> [TimerIdentifierAppEntity] { diff --git a/LeCountdown/Model/NSManagedContext+Extensions.swift b/LeCountdown/Model/NSManagedContext+Extensions.swift index 9619b5f..9b407fc 100644 --- a/LeCountdown/Model/NSManagedContext+Extensions.swift +++ b/LeCountdown/Model/NSManagedContext+Extensions.swift @@ -12,7 +12,7 @@ extension NSManagedObjectContext { func object(stringId: String) -> T? { guard let url = URL(string: stringId) else { return nil } - guard let objectId = PersistenceController.shared.container.persistentStoreCoordinator.managedObjectID(forURIRepresentation: url) else { return nil } + guard let objectId: NSManagedObjectID = PersistenceController.shared.container.persistentStoreCoordinator.managedObjectID(forURIRepresentation: url) else { return nil } return self.object(with: objectId) as? T } @@ -47,9 +47,6 @@ extension NSManagedObjectContext { } } - // public func fetch(_ request: NSFetchRequest) throws -> [T] where T : NSFetchRequestResult - - func fetch(entityName: String, predicate: NSPredicate? = nil, sortDescriptor: NSSortDescriptor? = nil) -> [T] where T : NSFetchRequestResult { let request = NSFetchRequest(entityName: entityName) request.predicate = predicate diff --git a/LeCountdown/TimerRouter.swift b/LeCountdown/TimerRouter.swift index ca03673..75ca1ac 100644 --- a/LeCountdown/TimerRouter.swift +++ b/LeCountdown/TimerRouter.swift @@ -48,6 +48,7 @@ class TimerRouter { fileprivate static func _launchCountdown(_ countdown: Countdown, handler: @escaping (Result) -> Void) { UNUserNotificationCenter.current().getNotificationSettings { settings in + DispatchQueue.main.async { CountdownScheduler.master.scheduleIfPossible(countdown: countdown) { result in switch result { diff --git a/LeCountdown/Utils/AppError.swift b/LeCountdown/Utils/AppError.swift index 708a4a8..dda59aa 100644 --- a/LeCountdown/Utils/AppError.swift +++ b/LeCountdown/Utils/AppError.swift @@ -10,11 +10,14 @@ import Foundation enum AppError: LocalizedError { case defaultError(error: Error) + case timerNotFound(id: String) var errorDescription: String? { switch self { case .defaultError(let error): return error.localizedDescription + case .timerNotFound(let id): + return "Timer not found: \(id)" } } @@ -22,6 +25,8 @@ enum AppError: LocalizedError { switch self { case .defaultError(let error): return error.localizedDescription + case .timerNotFound: + return "Timer not found" } }