| 1 | import Foundation |
| 2 | #if os(iOS) |
| 3 | import ActivityKit |
| 4 | #endif |
| 5 | |
| 6 | /// What the app does with task Live Activities itself. It never starts one: |
| 7 | /// the server does, by push-to-start, for at most three tasks a person |
| 8 | /// started (D3), inside Apple's limits. The app only ends what is left over. |
| 9 | public enum LiveActivityPolicy { |
| 10 | /// Activities whose task the app knows has ended, with the outcome to |
| 11 | /// end them on: the server's end push may have been dropped, and an |
| 12 | /// ended task must not stay on the Lock Screen. |
| 13 | public static func finished(running: [String], tasks: [ThinTask]) -> [String: String] { |
| 14 | var out: [String: String] = [:] |
| 15 | for task in tasks where running.contains(task.id) { |
| 16 | switch task.stage { |
| 17 | case "done": out[task.id] = "done" |
| 18 | case "cancelled": out[task.id] = "cancelled" |
| 19 | default: break |
| 20 | } |
| 21 | } |
| 22 | return out |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | #if os(iOS) |
| 27 | /// Follows this device's task Live Activities: the push-to-start token for |
| 28 | /// the device's registration, and each running activity's own token for |
| 29 | /// the server's updates. Started at launch, so an activity the server |
| 30 | /// started while the app was not running gets its token sent when the |
| 31 | /// system wakes the app for it. |
| 32 | @MainActor |
| 33 | public final class LiveActivities { |
| 34 | private let registrar: PushRegistrar |
| 35 | private var observed: Set<String> = [] |
| 36 | private var observers: [Task<Void, Never>] = [] |
| 37 | private var started = false |
| 38 | |
| 39 | public init(registrar: PushRegistrar) { |
| 40 | self.registrar = registrar |
| 41 | // A task the server says is over (409 task_ended, or not this |
| 42 | // person's) leaves the Lock Screen at once. |
| 43 | registrar.onActivityClosed = { task in Task { await Self.end(task: task) } } |
| 44 | // A new session: observers deliver their tokens again, for it. |
| 45 | registrar.onSessionChanged = { [weak self] in self?.reobserve() } |
| 46 | } |
| 47 | |
| 48 | private func reobserve() { |
| 49 | observers.forEach { $0.cancel() } |
| 50 | observers = []; observed = [] |
| 51 | guard started else { return } |
| 52 | for activity in Activity<EquipoTaskAttributes>.activities { observe(activity) } |
| 53 | } |
| 54 | |
| 55 | public func start() { |
| 56 | guard !started else { return } |
| 57 | started = true |
| 58 | let registrar = self.registrar |
| 59 | Task { |
| 60 | for await token in Activity<EquipoTaskAttributes>.pushToStartTokenUpdates { await registrar.startTokenChanged(token) } |
| 61 | } |
| 62 | for activity in Activity<EquipoTaskAttributes>.activities { observe(activity) } |
| 63 | Task { [weak self] in |
| 64 | for await activity in Activity<EquipoTaskAttributes>.activityUpdates { self?.observe(activity) } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private func observe(_ activity: Activity<EquipoTaskAttributes>) { |
| 69 | guard observed.insert(activity.id).inserted else { return } |
| 70 | let registrar = self.registrar |
| 71 | let task = activity.attributes.taskID |
| 72 | let tokens = activity.pushTokenUpdates |
| 73 | let states = activity.activityStateUpdates |
| 74 | let origin = activity.attributes.origin |
| 75 | observers.append(Task { |
| 76 | for await token in tokens { |
| 77 | if Task.isCancelled { return } |
| 78 | await registrar.activityTokenChanged(token, task: task, origin: origin) |
| 79 | } |
| 80 | }) |
| 81 | Task { |
| 82 | for await state in states where state == .ended || state == .dismissed { |
| 83 | registrar.activityEnded(task: task) |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Ends the activities of tasks this device knows have ended. |
| 90 | nonisolated public func endFinished(_ tasks: [ThinTask]) async { |
| 91 | let running = Activity<EquipoTaskAttributes>.activities |
| 92 | let finished = LiveActivityPolicy.finished(running: running.map(\.attributes.taskID), tasks: tasks) |
| 93 | for activity in running { |
| 94 | guard let outcome = finished[activity.attributes.taskID] else { continue } |
| 95 | var state = activity.content.state |
| 96 | state.outcome = outcome |
| 97 | if outcome == "done" { state.stage = "done" } |
| 98 | state.needsYou = "" |
| 99 | await activity.end(ActivityContent(state: state, staleDate: nil), dismissalPolicy: .default) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// One task's activity, ended because the server will never update it. |
| 104 | nonisolated static func end(task: String) async { |
| 105 | for activity in Activity<EquipoTaskAttributes>.activities where activity.attributes.taskID == task { |
| 106 | await activity.end(nil, dismissalPolicy: .immediate) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// Signed out: no task of that team stays on this device. |
| 111 | nonisolated public func endAll() async { |
| 112 | for activity in Activity<EquipoTaskAttributes>.activities { await activity.end(nil, dismissalPolicy: .immediate) } |
| 113 | } |
| 114 | } |
| 115 | #endif |
| 116 | |