| 1 | #if os(iOS) |
| 2 | import ActivityKit |
| 3 | import SwiftUI |
| 4 | import WidgetKit |
| 5 | |
| 6 | /// The task Live Activity's Lock Screen and Dynamic Island, drawn from |
| 7 | /// Roster: the title, the five-stop track, the specialist's tile, the |
| 8 | /// activity line, amber while it needs you, and the outcome at the end. |
| 9 | /// Tapping it opens the task (EquipoLink). |
| 10 | public struct EquipoTaskLiveActivity: Widget { |
| 11 | public init() {} |
| 12 | public var body: some WidgetConfiguration { |
| 13 | ActivityConfiguration(for: EquipoTaskAttributes.self) { context in |
| 14 | let p = TaskActivityPresentation(context.state) |
| 15 | TaskLockScreen(p: p) |
| 16 | .padding(16) |
| 17 | .activityBackgroundTint(nil) |
| 18 | .widgetURL(p.url) |
| 19 | } dynamicIsland: { context in |
| 20 | let p = TaskActivityPresentation(context.state) |
| 21 | return DynamicIsland { |
| 22 | DynamicIslandExpandedRegion(.leading) { |
| 23 | ActivityTile(who: p.specialist, tone: p.needsYou ? p.tone : nil, size: 36) |
| 24 | } |
| 25 | DynamicIslandExpandedRegion(.trailing) { |
| 26 | if p.needsYou { ActivityNeedsChip() } else if let outcome = p.outcome { |
| 27 | Label(outcome, systemImage: p.state.outcome == "done" ? "checkmark" : "xmark") |
| 28 | .font(.caption.weight(.semibold)).foregroundStyle(p.state.outcome == "done" ? ActivityPalette.positive : .secondary) |
| 29 | .labelStyle(.titleAndIcon) |
| 30 | } |
| 31 | } |
| 32 | DynamicIslandExpandedRegion(.center) { |
| 33 | Text(p.title).font(.headline).lineLimit(1) |
| 34 | } |
| 35 | DynamicIslandExpandedRegion(.bottom) { |
| 36 | VStack(alignment: .leading, spacing: 8) { |
| 37 | if !p.track.isEmpty { ActivityTrack(track: p.track, labels: true) } |
| 38 | ActivityLine(p: p) |
| 39 | } |
| 40 | .accessibilityElement(children: .combine) |
| 41 | } |
| 42 | } compactLeading: { |
| 43 | Image(systemName: p.specialist.symbol) |
| 44 | .foregroundStyle(p.needsYou ? p.tone.dot : .primary) |
| 45 | .accessibilityLabel(p.specialist.name) |
| 46 | } compactTrailing: { |
| 47 | if p.track.isEmpty { Image(systemName: "xmark").foregroundStyle(.secondary) } |
| 48 | else { ActivityTrack(track: p.track, labels: false).frame(width: 40) } |
| 49 | } minimal: { |
| 50 | Image(systemName: p.needsYou ? "hand.raised.fill" : p.specialist.symbol) |
| 51 | .foregroundStyle(p.needsYou ? p.tone.dot : .primary) |
| 52 | } |
| 53 | .widgetURL(p.url) |
| 54 | .keylineTint(p.needsYou ? p.tone.dot : nil) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | struct TaskLockScreen: View { |
| 60 | let p: TaskActivityPresentation |
| 61 | var body: some View { |
| 62 | VStack(alignment: .leading, spacing: 12) { |
| 63 | HStack(alignment: .center, spacing: 12) { |
| 64 | ActivityTile(who: p.specialist, tone: p.needsYou ? p.tone : nil, size: 40) |
| 65 | VStack(alignment: .leading, spacing: 3) { |
| 66 | Text(p.title).font(.headline).lineLimit(2) |
| 67 | ActivityLine(p: p) |
| 68 | } |
| 69 | Spacer(minLength: 0) |
| 70 | if p.needsYou { ActivityNeedsChip() } |
| 71 | } |
| 72 | if !p.track.isEmpty { ActivityTrack(track: p.track, labels: true) } |
| 73 | } |
| 74 | .accessibilityElement(children: .ignore) |
| 75 | .accessibilityLabel(p.spoken) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// The activity line, or what it needs, or how it ended. |
| 80 | struct ActivityLine: View { |
| 81 | let p: TaskActivityPresentation |
| 82 | var body: some View { |
| 83 | Text(p.line) |
| 84 | .font(.subheadline.weight(p.needsYou || p.ended ? .semibold : .regular)) |
| 85 | .foregroundStyle(p.needsYou ? p.tone.dot : p.state.outcome == "done" ? ActivityPalette.positive : .secondary) |
| 86 | .lineLimit(1).truncationMode(.tail) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// The specialist's SF Symbol in Roster's rounded tile; amber while it needs you. |
| 91 | struct ActivityTile: View { |
| 92 | let who: Specialist |
| 93 | var tone: StageTone? |
| 94 | var size: CGFloat |
| 95 | var body: some View { |
| 96 | let shape = RoundedRectangle(cornerRadius: size * 0.3, style: .continuous) |
| 97 | Image(systemName: who.symbol) |
| 98 | .font(.system(size: size * 0.46, weight: .medium)) |
| 99 | .foregroundStyle(tone?.dot ?? .primary) |
| 100 | .frame(width: size, height: size) |
| 101 | .background(tone?.halo ?? Color.primary.opacity(0.08), in: shape) |
| 102 | .overlay(shape.strokeBorder(tone?.dot.opacity(0.4) ?? Color.primary.opacity(0.12), lineWidth: 1)) |
| 103 | .accessibilityLabel(who.name) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | struct ActivityNeedsChip: View { |
| 108 | var body: some View { |
| 109 | HStack(spacing: 4) { |
| 110 | Image(systemName: "hand.raised").font(.system(size: 11, weight: .semibold)) |
| 111 | Text(String(localized: "chip.needs_you", bundle: .module)).textCase(.uppercase).kerning(0.3) |
| 112 | } |
| 113 | .font(.caption2.weight(.semibold)).foregroundStyle(ActivityPalette.warning) |
| 114 | .padding(.horizontal, 8).padding(.vertical, 3) |
| 115 | .background(ActivityPalette.warningBackground, in: Capsule()) |
| 116 | .fixedSize() |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// Roster's five stops: done stops filled green, the current one in its |
| 121 | /// tone with a halo, the rest hollow. With labels, the current stop's name. |
| 122 | struct ActivityTrack: View { |
| 123 | let track: [TrackStop] |
| 124 | var labels: Bool |
| 125 | var body: some View { |
| 126 | VStack(alignment: .leading, spacing: 4) { |
| 127 | Canvas { context, size in |
| 128 | let y = size.height / 2 |
| 129 | let r: CGFloat = labels ? 9.5 : 3.5 |
| 130 | let xs = track.indices.map { r + CGFloat($0) * (size.width - 2 * r) / CGFloat(max(1, track.count - 1)) } |
| 131 | for i in 0..<(xs.count - 1) { |
| 132 | let line = Path(CGRect(x: xs[i], y: y - (labels ? 1 : 0.5), width: xs[i + 1] - xs[i], height: labels ? 2 : 1)) |
| 133 | context.fill(line, with: .color(track[i].state == .done ? ActivityPalette.positive : ActivityPalette.todo)) |
| 134 | } |
| 135 | for (i, stop) in track.enumerated() { |
| 136 | let c = CGPoint(x: xs[i], y: y) |
| 137 | let small: CGFloat = labels ? 4 : 2 |
| 138 | switch stop.state { |
| 139 | case .done: |
| 140 | context.fill(Path(ellipseIn: CGRect(x: c.x - small, y: c.y - small, width: 2 * small, height: 2 * small)), with: .color(ActivityPalette.positive)) |
| 141 | case .current: |
| 142 | let tone = stop.tone ?? .positive |
| 143 | let halo = labels ? 9.5 : 3.5, dot = labels ? 5.5 : 2.5 |
| 144 | context.fill(Path(ellipseIn: CGRect(x: c.x - halo, y: c.y - halo, width: 2 * halo, height: 2 * halo)), with: .color(tone.halo)) |
| 145 | context.fill(Path(ellipseIn: CGRect(x: c.x - dot, y: c.y - dot, width: 2 * dot, height: 2 * dot)), with: .color(tone.dot)) |
| 146 | case .todo: |
| 147 | let dot = Path(ellipseIn: CGRect(x: c.x - small, y: c.y - small, width: 2 * small, height: 2 * small)) |
| 148 | context.stroke(dot, with: .color(ActivityPalette.todo), lineWidth: 1) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | .frame(height: labels ? 20 : 8) |
| 153 | if labels, let current = track.first(where: { $0.state == .current }) { |
| 154 | Text(current.stage.label).font(.caption.weight(.semibold)).foregroundStyle((current.tone ?? .positive).dot) |
| 155 | } |
| 156 | } |
| 157 | .accessibilityHidden(true) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /// The web day theme's status colours (theme.css), as Roster uses them. |
| 162 | enum ActivityPalette { |
| 163 | static let positive = Color(light: 0x166534, dark: 0x6EE7A8) |
| 164 | static let positiveBackground = Color(light: 0xDCFCE7, dark: 0x10281C) |
| 165 | static let warning = Color(light: 0x854D0E, dark: 0xF0C46A) |
| 166 | static let warningBackground = Color(light: 0xFEF3C7, dark: 0x2C2110) |
| 167 | static let negative = Color(light: 0xB91C1C, dark: 0xF79AA0) |
| 168 | static let negativeBackground = Color(light: 0xFEE2E2, dark: 0x331819) |
| 169 | static let todo = Color(light: 0x868691, dark: 0x71737C) |
| 170 | } |
| 171 | |
| 172 | extension StageTone { |
| 173 | var dot: Color { self == .warning ? ActivityPalette.warning : self == .negative ? ActivityPalette.negative : ActivityPalette.positive } |
| 174 | var halo: Color { self == .warning ? ActivityPalette.warningBackground : self == .negative ? ActivityPalette.negativeBackground : ActivityPalette.positiveBackground } |
| 175 | } |
| 176 | |
| 177 | private extension Color { |
| 178 | init(light: UInt32, dark: UInt32) { |
| 179 | self.init(uiColor: UIColor { traits in |
| 180 | let hex = traits.userInterfaceStyle == .dark ? dark : light |
| 181 | return UIColor(red: Double((hex >> 16) & 255) / 255, green: Double((hex >> 8) & 255) / 255, blue: Double(hex & 255) / 255, alpha: 1) |
| 182 | }) |
| 183 | } |
| 184 | } |
| 185 | #endif |
| 186 | |