|
|
|
|
@ -7,6 +7,7 @@ |
|
|
|
|
|
|
|
|
|
import Foundation |
|
|
|
|
import ActivityKit |
|
|
|
|
import BackgroundTasks |
|
|
|
|
|
|
|
|
|
class Conductor : ObservableObject { |
|
|
|
|
|
|
|
|
|
@ -49,10 +50,10 @@ class Conductor : ObservableObject { |
|
|
|
|
if !cancel { |
|
|
|
|
self._recordActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
self.notificationDates.removeValue(forKey: countdownId) |
|
|
|
|
|
|
|
|
|
// self._updateLiveActivity(countdownId: countdownId, endDate: <#T##Date#>) |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
if self.notificationDates.removeValue(forKey: countdownId) != nil { |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -114,11 +115,25 @@ class Conductor : ObservableObject { |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let liveActivity = try ActivityKit.Activity.request(attributes: attributes, content: activityContent) |
|
|
|
|
print("Requested a countdown Live Activity \(String(describing: liveActivity.id)).") |
|
|
|
|
print("Requested a Countdown Live Activity: \(String(describing: liveActivity.id)).") |
|
|
|
|
} catch (let error) { |
|
|
|
|
print("Error requesting countdown Live Activity \(error.localizedDescription).") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
self._scheduleAppRefresh(countdown: countdown) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _scheduleAppRefresh(countdown: Countdown) { |
|
|
|
|
let request = BGAppRefreshTaskRequest(identifier: BGTaskIdentifier.refresh.rawValue) |
|
|
|
|
request.earliestBeginDate = Date(timeIntervalSinceNow: countdown.duration) |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
try BGTaskScheduler.shared.submit(request) |
|
|
|
|
print("request submitted with date: \(String(describing: request.earliestBeginDate))") |
|
|
|
|
} catch { |
|
|
|
|
print("Could not schedule app refresh: \(error)") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -127,25 +142,36 @@ class Conductor : ObservableObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func updateLiveActivities() { |
|
|
|
|
print("update live activity...") |
|
|
|
|
|
|
|
|
|
for (countdownId, interval) in self.notificationDates { |
|
|
|
|
if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
|
|
|
|
|
Task { |
|
|
|
|
let ended = interval.end < Date() |
|
|
|
|
let state = LaunchWidgetAttributes.ContentState(ended: ended) |
|
|
|
|
let content = ActivityContent(state: state, staleDate: interval.end) |
|
|
|
|
await activity.update(content) |
|
|
|
|
print("Ending the Live Activity: \(activity.id)") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if interval.end < Date() { |
|
|
|
|
self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
// |
|
|
|
|
// Task { |
|
|
|
|
// |
|
|
|
|
// if ended { |
|
|
|
|
// self._endLiveActivity(countdownId: countdownId) |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
//// let state = LaunchWidgetAttributes.ContentState(ended: ended) |
|
|
|
|
//// let content = ActivityContent(state: state, staleDate: interval.end) |
|
|
|
|
//// await activity.update(content) |
|
|
|
|
//// print("Ending the Live Activity: \(activity.id)") |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _endLiveActivity(countdownId: String) { |
|
|
|
|
|
|
|
|
|
print("Trt to end the Live Activity: \(countdownId)") |
|
|
|
|
print("Try to end the Live Activity: \(countdownId)") |
|
|
|
|
|
|
|
|
|
if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
Task { |
|
|
|
|
|