|
|
|
|
@ -16,25 +16,34 @@ class Conductor : ObservableObject { |
|
|
|
|
@Published var soundPlayer: SoundPlayer? = nil |
|
|
|
|
|
|
|
|
|
@UserDefault(Key.dates.rawValue, defaultValue: [:]) static var savedDates: [String : DateInterval] |
|
|
|
|
|
|
|
|
|
@UserDefault(Key.stopwatch.rawValue, defaultValue: [:]) static var savedStopwatches: [String : Date] |
|
|
|
|
|
|
|
|
|
init() { |
|
|
|
|
self.notificationDates = Conductor.savedDates |
|
|
|
|
self.timerDates = Conductor.savedDates |
|
|
|
|
self.stopwatchIntervals = Conductor.savedStopwatches |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Published var timerDates: [String : DateInterval] = [:] { |
|
|
|
|
didSet { |
|
|
|
|
Conductor.savedDates = timerDates |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Published var notificationDates: [String : DateInterval] = [:] { |
|
|
|
|
@Published var stopwatchIntervals: [String : Date] = [:] { |
|
|
|
|
didSet { |
|
|
|
|
Conductor.savedDates = notificationDates |
|
|
|
|
Conductor.savedStopwatches = stopwatchIntervals |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
enum Key : String { |
|
|
|
|
case dates |
|
|
|
|
case stopwatch |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func startCountdown(_ date: Date, countdown: Countdown) { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
let dateInterval = DateInterval(start: Date(), end: date) |
|
|
|
|
self.notificationDates[countdown.stringId] = dateInterval |
|
|
|
|
self.timerDates[countdown.stringId] = dateInterval |
|
|
|
|
|
|
|
|
|
self._launchLiveActivity(countdown: countdown, endDate: date) |
|
|
|
|
} |
|
|
|
|
@ -51,7 +60,7 @@ class Conductor : ObservableObject { |
|
|
|
|
self._recordActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if self.notificationDates.removeValue(forKey: countdownId) != nil { |
|
|
|
|
if self.timerDates.removeValue(forKey: countdownId) != nil { |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -59,7 +68,7 @@ class Conductor : ObservableObject { |
|
|
|
|
|
|
|
|
|
func cleanup() { |
|
|
|
|
let now = Date() |
|
|
|
|
for (key, value) in self.notificationDates { |
|
|
|
|
for (key, value) in self.timerDates { |
|
|
|
|
if value.end < now { |
|
|
|
|
self.endCountdown(countdownId: key, cancel: false) |
|
|
|
|
} |
|
|
|
|
@ -69,9 +78,9 @@ class Conductor : ObservableObject { |
|
|
|
|
fileprivate func _recordActivity(countdownId: String) { |
|
|
|
|
let context = PersistenceController.shared.container.viewContext |
|
|
|
|
if let countdown = context.object(stringId: countdownId) as? Countdown, |
|
|
|
|
let dateInterval = self.notificationDates[countdownId] { |
|
|
|
|
let dateInterval = self.timerDates[countdownId] { |
|
|
|
|
do { |
|
|
|
|
try CoreDataRequests.recordActivity(countdown: countdown, dateInterval: dateInterval) |
|
|
|
|
try CoreDataRequests.recordActivity(timer: countdown, dateInterval: dateInterval) |
|
|
|
|
} catch { |
|
|
|
|
print("Could not record activity = \(error)") |
|
|
|
|
// TODO: show error to user |
|
|
|
|
@ -145,7 +154,7 @@ class Conductor : ObservableObject { |
|
|
|
|
func updateLiveActivities() { |
|
|
|
|
print("update live activity...") |
|
|
|
|
|
|
|
|
|
for (countdownId, interval) in self.notificationDates { |
|
|
|
|
for (countdownId, interval) in self.timerDates { |
|
|
|
|
|
|
|
|
|
if interval.end < Date() { |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
|