From adf793cb47dc199f8ae88f5c719f4d3ba06a4c4e Mon Sep 17 00:00:00 2001 From: Paul Schmiedmayer Date: Tue, 9 Jan 2024 17:18:14 -0800 Subject: [PATCH] Improve Scheduler Initializer and Fix Issue Where Tasks That Are Overdue at Creation Are Not Updated (#35) # Improve Scheduler Initializer and Fix Issue Where Tasks That Are Overdue at Creation Are Not Updated ## :gear: Release Notes - `Scheduler` conforms to `DefaultInitializable` - Fix issue where tasks that are overdue at creation are not updated ## :pencil: Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md). --- Sources/SpeziScheduler/Model/Event.swift | 9 ++++++++- Sources/SpeziScheduler/Scheduler.swift | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Sources/SpeziScheduler/Model/Event.swift b/Sources/SpeziScheduler/Model/Event.swift index d1e3260..06d0d7a 100644 --- a/Sources/SpeziScheduler/Model/Event.swift +++ b/Sources/SpeziScheduler/Model/Event.swift @@ -71,7 +71,14 @@ public final class Event: Identifiable, @unchecked Sendable { } convenience init(taskId: UUID, scheduledAt: Date) { - self.init(state: .scheduled(at: scheduledAt), taskId: taskId) + let state: EventState + if scheduledAt < .now { + state = .overdue(since: scheduledAt) + } else { + state = .scheduled(at: scheduledAt) + } + + self.init(state: state, taskId: taskId) } diff --git a/Sources/SpeziScheduler/Scheduler.swift b/Sources/SpeziScheduler/Scheduler.swift index 0d1595d..72e5b50 100644 --- a/Sources/SpeziScheduler/Scheduler.swift +++ b/Sources/SpeziScheduler/Scheduler.swift @@ -19,7 +19,8 @@ import UserNotifications /// Use the ``Scheduler/init(prescheduleNotificationLimit:tasks:)`` initializer or the ``Scheduler/schedule(task:)`` function /// to schedule tasks that you can obtain using the ``Scheduler/tasks`` property. /// You can use the ``Scheduler`` as an `ObservableObject` to automatically update your SwiftUI views when new events are emitted or events change. -public class Scheduler: NSObject, UNUserNotificationCenterDelegate, Module, LifecycleHandler, EnvironmentAccessible { +public class Scheduler: NSObject, UNUserNotificationCenterDelegate, + Module, LifecycleHandler, EnvironmentAccessible, DefaultInitializable { private let logger = Logger(subsystem: "edu.stanford.spezi.scheduler", category: "Scheduler") @Dependency private var storage: SchedulerStorage @@ -28,7 +29,7 @@ public class Scheduler: NSObject, UNUserNotificationCenterDele private let initialTasks: [Task] private let prescheduleNotificationLimit: Int - private let taskList: TaskList + private let taskList: TaskList = TaskList() public var tasks: [Task] { taskList.tasks @@ -54,15 +55,11 @@ public class Scheduler: NSObject, UNUserNotificationCenterDele prescheduleNotificationLimit >= 1 && prescheduleNotificationLimit <= 64, "The prescheduleLimit must be bigger than 1 and smaller than the limit of 64 local notifications at a time" ) - - let list = TaskList() - + self.prescheduleNotificationLimit = prescheduleNotificationLimit self.initialTasks = initialTasks - self.taskList = list - - self._storage = Dependency(wrappedValue: SchedulerStorage(taskList: list)) - + self._storage = Dependency(wrappedValue: SchedulerStorage(taskList: self.taskList)) + super.init() // Only run the notification setup when not running unit tests: @@ -76,6 +73,10 @@ public class Scheduler: NSObject, UNUserNotificationCenterDele } } + override public required convenience init() { + self.init(tasks: []) + } + public func configure() { _Concurrency.Task {