import Foundation #if os(iOS) import ActivityKit #endif /// A task's Live Activity (native slice 2, D3): one for each task the person /// started, at most three, started and moved by the server's pushes. /// /// The type's name is the push's `attributes-type` and its fields are /// `wire.LiveActivityAttributes` and `wire.LiveActivityState`, so neither /// may be renamed without the server (internal/equipo/push/payload.go). /// ActivityKit decodes pushes with a plain JSONDecoder: the coding keys /// spell out the wire's snake case. public struct EquipoTaskAttributes: Codable, Hashable, Sendable { public var taskID: String /// The team's instance that started it: its token goes only to the /// account at this origin. nil from a server that did not name one. public var origin: String? enum CodingKeys: String, CodingKey { case taskID = "task_id", origin } public init(taskID: String, origin: String? = nil) { self.taskID = taskID; self.origin = origin } /// The task as the activity shows it: the task, its stage (Task.stage), /// the specialist working on it, the newest activity line, what it needs /// from you ("" when nothing), Roster's tone, and once it ended, the /// outcome (done, cancelled, failed, or replaced by a newer task's). public struct ContentState: Codable, Hashable, Sendable { public var taskID: String public var title: String public var stage: String public var specialist: String public var activity: String public var needsYou: String public var tone: String public var outcome: String? enum CodingKeys: String, CodingKey { case taskID = "task_id", title, stage, specialist, activity, needsYou = "needs_you", tone, outcome } public init(taskID: String, title: String, stage: String, specialist: String, activity: String, needsYou: String, tone: String, outcome: String? = nil) { self.taskID = taskID; self.title = title; self.stage = stage; self.specialist = specialist self.activity = activity; self.needsYou = needsYou; self.tone = tone; self.outcome = outcome } /// A field the server leaves out reads as empty: an activity that /// cannot decode its update is never updated again. public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) func text(_ key: CodingKeys) throws -> String { try c.decodeIfPresent(String.self, forKey: key) ?? "" } taskID = try text(.taskID); title = try text(.title); stage = try text(.stage) specialist = try text(.specialist); activity = try text(.activity); needsYou = try text(.needsYou) tone = try text(.tone) let ended = try c.decodeIfPresent(String.self, forKey: .outcome) outcome = ended?.isEmpty == false ? ended : nil } } } #if os(iOS) extension EquipoTaskAttributes: ActivityAttributes {} #endif /// The app's own links: a Live Activity opens its task with one. public enum EquipoLink { public static let scheme = "works.oficina.equipo" public static func task(_ id: String) -> URL { var c = URLComponents() c.scheme = scheme; c.host = "task"; c.path = "/" + id return c.url ?? URL(string: scheme + "://task")! } /// The task a link opens, or nil for anything else. public static func taskID(from url: URL) -> String? { guard url.scheme == scheme, url.host == "task" else { return nil } let id = String(url.path.dropFirst()) return id.isEmpty || id.contains("/") ? nil : id } } /// What a task's Live Activity shows, from its content state, in Roster's /// rules: the five-stop track (none for a cancelled task), the stop's tone /// (amber while it needs you), the specialist's symbol, and one line that /// says what is happening, what it needs, or how it ended. public struct TaskActivityPresentation: Sendable, Equatable { public let state: EquipoTaskAttributes.ContentState public init(_ state: EquipoTaskAttributes.ContentState) { self.state = state } public var title: String { state.title } public var needsYou: Bool { !state.needsYou.isEmpty && !ended } public var ended: Bool { state.outcome != nil } public var cancelled: Bool { state.stage == "cancelled" || state.outcome == "cancelled" } /// The server's tone; amber whenever the task waits on you. public var tone: StageTone { if needsYou { return state.tone == StageTone.negative.rawValue ? .negative : .warning } return StageTone(rawValue: state.tone) ?? .positive } public var stage: Stage? { state.outcome == "done" ? .done : Stage(wire: state.stage) } public var track: [TrackStop] { cancelled ? [] : Stage.track(at: stage, tone: tone) } public var specialist: Specialist { state.specialist.isEmpty ? .engineering : Specialist(category: state.specialist) } /// How it ended, in Roster's words; nil while it runs, or when a newer /// task's activity replaced it (it is dismissed at once). public var outcome: String? { switch state.outcome { case "done": return String(localized: "stage.done", bundle: .module) case "cancelled": return String(localized: "stage.cancelled", bundle: .module) case "failed": return String(localized: "step.failed", bundle: .module) default: return nil } } /// The one line under the title. public var line: String { if let outcome { return outcome } if needsYou { return String(localized: "chip.needs_you", bundle: .module) } if !state.activity.isEmpty { return state.activity } return stage?.label ?? (cancelled ? String(localized: "stage.cancelled", bundle: .module) : "") } public var url: URL { EquipoLink.task(state.taskID) } /// VoiceOver: the title, where it is, and the line. public var spoken: String { [title, stage?.label, line].compactMap { $0 }.filter { !$0.isEmpty }.reduce(into: [String]()) { out, s in if out.last != s { out.append(s) } }.joined(separator: ". ") } }