| 1 | import Foundation |
| 2 | |
| 3 | /// Open-only routing for default taps, old payloads and Live Activities. Read from |
| 4 | /// the alert's `equipo` object (wire.PushData) or the app's own link. |
| 5 | public enum PushRoute: Equatable, Sendable { |
| 6 | /// A card waits on you: the conversation, at that card. |
| 7 | case card(String) |
| 8 | /// The coordinator replied: the conversation. |
| 9 | case conversation |
| 10 | /// A task finished, stopped or is running: the task view. |
| 11 | case task(String) |
| 12 | |
| 13 | /// The alert's routing data; nil for a badge, or anything unknown. |
| 14 | public init?(userInfo: [AnyHashable: Any]) { |
| 15 | guard let data = userInfo["equipo"] as? [String: Any], let kind = data["kind"] as? String else { return nil } |
| 16 | func id(_ key: String) -> String? { (data[key] as? String).flatMap { $0.isEmpty ? nil : $0 } } |
| 17 | switch kind { |
| 18 | case "card": |
| 19 | // A card the device cannot place still opens beside its task. |
| 20 | if let card = id("card_id") { self = .card(card) } else if let task = id("task_id") { self = .task(task) } else { return nil } |
| 21 | case "reply": self = .conversation |
| 22 | case "task": |
| 23 | guard let task = id("task_id") else { return nil } |
| 24 | self = .task(task) |
| 25 | default: return nil |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /// A Live Activity's link (EquipoLink). |
| 30 | public init?(url: URL) { |
| 31 | guard let task = EquipoLink.taskID(from: url) else { return nil } |
| 32 | self = .task(task) |
| 33 | } |
| 34 | |
| 35 | /// The screen it opens. |
| 36 | public var destination: Destination { |
| 37 | switch self { |
| 38 | case .card(let id): return Destination(pane: .conversation, cardID: id) |
| 39 | case .conversation: return Destination(pane: .conversation) |
| 40 | case .task(let id): return Destination(pane: .tasks, taskID: id) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// A screen in the app: a pane (a tab on iPhone, the sidebar on the Mac), |
| 46 | /// and the card to bring into view or the task to open in it. |
| 47 | public struct Destination: Equatable, Sendable { |
| 48 | public enum Pane: Hashable, Sendable { case conversation, needs, tasks, projects } |
| 49 | public var pane: Pane |
| 50 | public var cardID: String? |
| 51 | public var taskID: String? |
| 52 | public var projectID: String? |
| 53 | public var macAction: MacTaskAction? |
| 54 | public var focusComposer: Bool |
| 55 | public init(pane: Pane, cardID: String? = nil, taskID: String? = nil, projectID: String? = nil, focusComposer: Bool = false, macAction: MacTaskAction? = nil) { |
| 56 | self.pane = pane; self.cardID = cardID; self.taskID = taskID; self.projectID = projectID; self.focusComposer = focusComposer; self.macAction = macAction |
| 57 | } |
| 58 | } |
| 59 | |