|
|
|
|
@ -6,11 +6,11 @@ |
|
|
|
|
// |
|
|
|
|
|
|
|
|
|
import Foundation |
|
|
|
|
import ActivityKit |
|
|
|
|
import BackgroundTasks |
|
|
|
|
import SwiftUI |
|
|
|
|
import Intents |
|
|
|
|
import AudioToolbox |
|
|
|
|
import ActivityKit |
|
|
|
|
|
|
|
|
|
enum BGTaskIdentifier : String { |
|
|
|
|
case refresh = "com.staxriver.lecountdown.refresh" |
|
|
|
|
@ -243,29 +243,7 @@ class Conductor: ObservableObject { |
|
|
|
|
let interaction = INInteraction(intent: intent, response: nil) |
|
|
|
|
interaction.donate() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Live Activity |
|
|
|
|
|
|
|
|
|
fileprivate func _launchLiveActivity(stopwatch: Stopwatch, start: Date) { |
|
|
|
|
|
|
|
|
|
if ActivityAuthorizationInfo().areActivitiesEnabled { |
|
|
|
|
|
|
|
|
|
let contentState = LaunchWidgetAttributes.ContentState(ended: false) |
|
|
|
|
let attributes = LaunchWidgetAttributes(id: stopwatch.stringId, name: stopwatch.displayName, date: start) |
|
|
|
|
let activityContent = ActivityContent(state: contentState, staleDate: nil) |
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
let liveActivity = try ActivityKit.Activity.request(attributes: attributes, content: activityContent) |
|
|
|
|
print("Requested a Live Activity: \(String(describing: liveActivity.id)).") |
|
|
|
|
} catch (let error) { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// self._scheduleAppRefresh(countdown: countdown) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _scheduleAppRefresh(countdown: Countdown) { |
|
|
|
|
let request = BGAppRefreshTaskRequest(identifier: BGTaskIdentifier.refresh.rawValue) |
|
|
|
|
request.earliestBeginDate = Date(timeIntervalSinceNow: countdown.duration) |
|
|
|
|
@ -276,7 +254,33 @@ class Conductor: ObservableObject { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// MARK: - Live Activity |
|
|
|
|
|
|
|
|
|
fileprivate func _launchLiveActivity(stopwatch: Stopwatch, start: Date) { |
|
|
|
|
|
|
|
|
|
if #available(iOS 16.2, *) { |
|
|
|
|
|
|
|
|
|
if ActivityAuthorizationInfo().areActivitiesEnabled { |
|
|
|
|
|
|
|
|
|
let contentState = LaunchWidgetAttributes.ContentState(ended: false) |
|
|
|
|
let attributes = LaunchWidgetAttributes(id: stopwatch.stringId, name: stopwatch.displayName, date: start) |
|
|
|
|
|
|
|
|
|
let activityContent = ActivityContent(state: contentState, staleDate: nil) |
|
|
|
|
do { |
|
|
|
|
let liveActivity = try ActivityKit.Activity.request(attributes: attributes, content: activityContent) |
|
|
|
|
print("Requested a Live Activity: \(String(describing: liveActivity.id)).") |
|
|
|
|
} catch (let error) { |
|
|
|
|
Logger.error(error) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Fallback on earlier versions |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _liveActivity(timerId: String) -> ActivityKit.Activity<LaunchWidgetAttributes>? { |
|
|
|
|
return ActivityKit.Activity<LaunchWidgetAttributes>.activities.first(where: { $0.attributes.id == timerId } ) |
|
|
|
|
} |
|
|
|
|
@ -289,7 +293,6 @@ class Conductor: ObservableObject { |
|
|
|
|
if interval.end < Date() { |
|
|
|
|
self._endLiveActivity(timerId: countdownId) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if let activity = self._liveActivity(countdownId: countdownId) { |
|
|
|
|
// |
|
|
|
|
@ -310,17 +313,17 @@ class Conductor: ObservableObject { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _endLiveActivity(timerId: String) { |
|
|
|
|
|
|
|
|
|
print("Try to end the Live Activity: \(timerId)") |
|
|
|
|
|
|
|
|
|
if let activity = self._liveActivity(timerId: timerId) { |
|
|
|
|
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)") |
|
|
|
|
if #available(iOS 16.2, *) { |
|
|
|
|
print("Try to end the Live Activity: \(timerId)") |
|
|
|
|
if let activity = self._liveActivity(timerId: timerId) { |
|
|
|
|
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)") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|