import Foundation #if os(iOS) import ActivityKit #endif /// What the app does with task Live Activities itself. It never starts one: /// the server does, by push-to-start, for at most three tasks a person /// started (D3), inside Apple's limits. The app only ends what is left over. public enum LiveActivityPolicy { /// Activities whose task the app knows has ended, with the outcome to /// end them on: the server's end push may have been dropped, and an /// ended task must not stay on the Lock Screen. public static func finished(running: [String], tasks: [ThinTask]) -> [String: String] { var out: [String: String] = [:] for task in tasks where running.contains(task.id) { switch task.stage { case "done": out[task.id] = "done" case "cancelled": out[task.id] = "cancelled" default: break } } return out } } #if os(iOS) /// Follows this device's task Live Activities: the push-to-start token for /// the device's registration, and each running activity's own token for /// the server's updates. Started at launch, so an activity the server /// started while the app was not running gets its token sent when the /// system wakes the app for it. @MainActor public final class LiveActivities { private let registrar: PushRegistrar private var observed: Set = [] private var observers: [Task] = [] private var started = false public init(registrar: PushRegistrar) { self.registrar = registrar // A task the server says is over (409 task_ended, or not this // person's) leaves the Lock Screen at once. registrar.onActivityClosed = { task in Task { await Self.end(task: task) } } // A new session: observers deliver their tokens again, for it. registrar.onSessionChanged = { [weak self] in self?.reobserve() } } private func reobserve() { observers.forEach { $0.cancel() } observers = []; observed = [] guard started else { return } for activity in Activity.activities { observe(activity) } } public func start() { guard !started else { return } started = true let registrar = self.registrar Task { for await token in Activity.pushToStartTokenUpdates { await registrar.startTokenChanged(token) } } for activity in Activity.activities { observe(activity) } Task { [weak self] in for await activity in Activity.activityUpdates { self?.observe(activity) } } } private func observe(_ activity: Activity) { guard observed.insert(activity.id).inserted else { return } let registrar = self.registrar let task = activity.attributes.taskID let tokens = activity.pushTokenUpdates let states = activity.activityStateUpdates let origin = activity.attributes.origin observers.append(Task { for await token in tokens { if Task.isCancelled { return } await registrar.activityTokenChanged(token, task: task, origin: origin) } }) Task { for await state in states where state == .ended || state == .dismissed { registrar.activityEnded(task: task) break } } } /// Ends the activities of tasks this device knows have ended. nonisolated public func endFinished(_ tasks: [ThinTask]) async { let running = Activity.activities let finished = LiveActivityPolicy.finished(running: running.map(\.attributes.taskID), tasks: tasks) for activity in running { guard let outcome = finished[activity.attributes.taskID] else { continue } var state = activity.content.state state.outcome = outcome if outcome == "done" { state.stage = "done" } state.needsYou = "" await activity.end(ActivityContent(state: state, staleDate: nil), dismissalPolicy: .default) } } /// One task's activity, ended because the server will never update it. nonisolated static func end(task: String) async { for activity in Activity.activities where activity.attributes.taskID == task { await activity.end(nil, dismissalPolicy: .immediate) } } /// Signed out: no task of that team stays on this device. nonisolated public func endAll() async { for activity in Activity.activities { await activity.end(nil, dismissalPolicy: .immediate) } } } #endif