| 1 | import Foundation |
| 2 | #if os(iOS) |
| 3 | import ActivityKit |
| 4 | #endif |
| 5 | |
| 6 | /// A task's Live Activity (native slice 2, D3): one for each task the person |
| 7 | /// started, at most three, started and moved by the server's pushes. |
| 8 | /// |
| 9 | /// The type's name is the push's `attributes-type` and its fields are |
| 10 | /// `wire.LiveActivityAttributes` and `wire.LiveActivityState`, so neither |
| 11 | /// may be renamed without the server (internal/equipo/push/payload.go). |
| 12 | /// ActivityKit decodes pushes with a plain JSONDecoder: the coding keys |
| 13 | /// spell out the wire's snake case. |
| 14 | public struct EquipoTaskAttributes: Codable, Hashable, Sendable { |
| 15 | public var taskID: String |
| 16 | /// The team's instance that started it: its token goes only to the |
| 17 | /// account at this origin. nil from a server that did not name one. |
| 18 | public var origin: String? |
| 19 | enum CodingKeys: String, CodingKey { case taskID = "task_id", origin } |
| 20 | public init(taskID: String, origin: String? = nil) { self.taskID = taskID; self.origin = origin } |
| 21 | |
| 22 | /// The task as the activity shows it: the task, its stage (Task.stage), |
| 23 | /// the specialist working on it, the newest activity line, what it needs |
| 24 | /// from you ("" when nothing), Roster's tone, and once it ended, the |
| 25 | /// outcome (done, cancelled, failed, or replaced by a newer task's). |
| 26 | public struct ContentState: Codable, Hashable, Sendable { |
| 27 | public var taskID: String |
| 28 | public var title: String |
| 29 | public var stage: String |
| 30 | public var specialist: String |
| 31 | public var activity: String |
| 32 | public var needsYou: String |
| 33 | public var tone: String |
| 34 | public var outcome: String? |
| 35 | |
| 36 | enum CodingKeys: String, CodingKey { |
| 37 | case taskID = "task_id", title, stage, specialist, activity, needsYou = "needs_you", tone, outcome |
| 38 | } |
| 39 | public init(taskID: String, title: String, stage: String, specialist: String, activity: String, |
| 40 | needsYou: String, tone: String, outcome: String? = nil) { |
| 41 | self.taskID = taskID; self.title = title; self.stage = stage; self.specialist = specialist |
| 42 | self.activity = activity; self.needsYou = needsYou; self.tone = tone; self.outcome = outcome |
| 43 | } |
| 44 | /// A field the server leaves out reads as empty: an activity that |
| 45 | /// cannot decode its update is never updated again. |
| 46 | public init(from decoder: Decoder) throws { |
| 47 | let c = try decoder.container(keyedBy: CodingKeys.self) |
| 48 | func text(_ key: CodingKeys) throws -> String { try c.decodeIfPresent(String.self, forKey: key) ?? "" } |
| 49 | taskID = try text(.taskID); title = try text(.title); stage = try text(.stage) |
| 50 | specialist = try text(.specialist); activity = try text(.activity); needsYou = try text(.needsYou) |
| 51 | tone = try text(.tone) |
| 52 | let ended = try c.decodeIfPresent(String.self, forKey: .outcome) |
| 53 | outcome = ended?.isEmpty == false ? ended : nil |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | #if os(iOS) |
| 59 | extension EquipoTaskAttributes: ActivityAttributes {} |
| 60 | #endif |
| 61 | |
| 62 | /// The app's own links: a Live Activity opens its task with one. |
| 63 | public enum EquipoLink { |
| 64 | public static let scheme = "works.oficina.equipo" |
| 65 | public static func task(_ id: String) -> URL { |
| 66 | var c = URLComponents() |
| 67 | c.scheme = scheme; c.host = "task"; c.path = "/" + id |
| 68 | return c.url ?? URL(string: scheme + "://task")! |
| 69 | } |
| 70 | /// The task a link opens, or nil for anything else. |
| 71 | public static func taskID(from url: URL) -> String? { |
| 72 | guard url.scheme == scheme, url.host == "task" else { return nil } |
| 73 | let id = String(url.path.dropFirst()) |
| 74 | return id.isEmpty || id.contains("/") ? nil : id |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// What a task's Live Activity shows, from its content state, in Roster's |
| 79 | /// rules: the five-stop track (none for a cancelled task), the stop's tone |
| 80 | /// (amber while it needs you), the specialist's symbol, and one line that |
| 81 | /// says what is happening, what it needs, or how it ended. |
| 82 | public struct TaskActivityPresentation: Sendable, Equatable { |
| 83 | public let state: EquipoTaskAttributes.ContentState |
| 84 | public init(_ state: EquipoTaskAttributes.ContentState) { self.state = state } |
| 85 | |
| 86 | public var title: String { state.title } |
| 87 | public var needsYou: Bool { !state.needsYou.isEmpty && !ended } |
| 88 | public var ended: Bool { state.outcome != nil } |
| 89 | public var cancelled: Bool { state.stage == "cancelled" || state.outcome == "cancelled" } |
| 90 | /// The server's tone; amber whenever the task waits on you. |
| 91 | public var tone: StageTone { |
| 92 | if needsYou { return state.tone == StageTone.negative.rawValue ? .negative : .warning } |
| 93 | return StageTone(rawValue: state.tone) ?? .positive |
| 94 | } |
| 95 | public var stage: Stage? { state.outcome == "done" ? .done : Stage(wire: state.stage) } |
| 96 | public var track: [TrackStop] { cancelled ? [] : Stage.track(at: stage, tone: tone) } |
| 97 | public var specialist: Specialist { state.specialist.isEmpty ? .engineering : Specialist(category: state.specialist) } |
| 98 | /// How it ended, in Roster's words; nil while it runs, or when a newer |
| 99 | /// task's activity replaced it (it is dismissed at once). |
| 100 | public var outcome: String? { |
| 101 | switch state.outcome { |
| 102 | case "done": return String(localized: "stage.done", bundle: .module) |
| 103 | case "cancelled": return String(localized: "stage.cancelled", bundle: .module) |
| 104 | case "failed": return String(localized: "step.failed", bundle: .module) |
| 105 | default: return nil |
| 106 | } |
| 107 | } |
| 108 | /// The one line under the title. |
| 109 | public var line: String { |
| 110 | if let outcome { return outcome } |
| 111 | if needsYou { return String(localized: "chip.needs_you", bundle: .module) } |
| 112 | if !state.activity.isEmpty { return state.activity } |
| 113 | return stage?.label ?? (cancelled ? String(localized: "stage.cancelled", bundle: .module) : "") |
| 114 | } |
| 115 | public var url: URL { EquipoLink.task(state.taskID) } |
| 116 | /// VoiceOver: the title, where it is, and the line. |
| 117 | public var spoken: String { |
| 118 | [title, stage?.label, line].compactMap { $0 }.filter { !$0.isEmpty }.reduce(into: [String]()) { out, s in |
| 119 | if out.last != s { out.append(s) } |
| 120 | }.joined(separator: ". ") |
| 121 | } |
| 122 | } |
| 123 | |