|
|
|
|
@ -7,6 +7,7 @@ |
|
|
|
|
|
|
|
|
|
import Foundation |
|
|
|
|
import UserNotifications |
|
|
|
|
import ActivityKit |
|
|
|
|
|
|
|
|
|
class CountdownScheduler { |
|
|
|
|
|
|
|
|
|
@ -88,6 +89,8 @@ class AppEnvironment : ObservableObject { |
|
|
|
|
DispatchQueue.main.async { |
|
|
|
|
let dateInterval = DateInterval(start: Date(), end: date) |
|
|
|
|
self.notificationDates[countdown.stringId] = dateInterval |
|
|
|
|
|
|
|
|
|
self._launchLiveActivity(countdown: countdown, endDate: date) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -102,6 +105,9 @@ class AppEnvironment : ObservableObject { |
|
|
|
|
self._recordActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
self.notificationDates.removeValue(forKey: countdownId) |
|
|
|
|
|
|
|
|
|
// self._updateLiveActivity(countdownId: countdownId, endDate: <#T##Date#>) |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -149,4 +155,53 @@ class AppEnvironment : ObservableObject { |
|
|
|
|
self.soundPlayer?.stop() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Live Activity |
|
|
|
|
|
|
|
|
|
fileprivate func _launchLiveActivity(countdown: Countdown, endDate: Date) { |
|
|
|
|
|
|
|
|
|
if ActivityAuthorizationInfo().areActivitiesEnabled { |
|
|
|
|
|
|
|
|
|
let contentState = LaunchWidgetAttributes.ContentState(ended: false) |
|
|
|
|
let attributes = LaunchWidgetAttributes(id: countdown.stringId, name: countdown.displayName, endDate: endDate) |
|
|
|
|
let activityContent = ActivityContent(state: contentState, staleDate: endDate.addingTimeInterval(30.0)) |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let liveActivity = try ActivityKit.Activity.request(attributes: attributes, content: activityContent) |
|
|
|
|
print("Requested a countdown Live Activity \(String(describing: liveActivity.id)).") |
|
|
|
|
} catch (let error) { |
|
|
|
|
print("Error requesting countdown Live Activity \(error.localizedDescription).") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _liveActivity(countdownId: String) -> ActivityKit.Activity<LaunchWidgetAttributes>? { |
|
|
|
|
return ActivityKit.Activity<LaunchWidgetAttributes>.activities.first(where: { $0.attributes.id == countdownId } ) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// fileprivate func _updateLiveActivity(countdownId: String, endDate: Date) { |
|
|
|
|
// if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
// Task { |
|
|
|
|
// let state = LaunchWidgetAttributes.ContentState(ended: true) |
|
|
|
|
// let content = ActivityContent(state: state, staleDate: endDate) |
|
|
|
|
// await activity.update(content) |
|
|
|
|
// print("Ending the Live Activity: \(activity.id)") |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
fileprivate func _endLiveActivity(countdownId: String) { |
|
|
|
|
|
|
|
|
|
print("Trt to end the Live Activity: \(countdownId)") |
|
|
|
|
|
|
|
|
|
if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
Task { |
|
|
|
|
let state = LaunchWidgetAttributes.ContentState(ended: true) |
|
|
|
|
let content = ActivityContent(state: state, staleDate: Date()) |
|
|
|
|
await activity.end(content, dismissalPolicy: .immediate) |
|
|
|
|
print("Ending the Live Activity: \(activity.id)") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|