#if os(iOS) import ActivityKit import SwiftUI import WidgetKit /// The task Live Activity's Lock Screen and Dynamic Island, drawn from /// Roster: the title, the five-stop track, the specialist's tile, the /// activity line, amber while it needs you, and the outcome at the end. /// Tapping it opens the task (EquipoLink). public struct EquipoTaskLiveActivity: Widget { public init() {} public var body: some WidgetConfiguration { ActivityConfiguration(for: EquipoTaskAttributes.self) { context in let p = TaskActivityPresentation(context.state) TaskLockScreen(p: p) .padding(16) .activityBackgroundTint(nil) .widgetURL(p.url) } dynamicIsland: { context in let p = TaskActivityPresentation(context.state) return DynamicIsland { DynamicIslandExpandedRegion(.leading) { ActivityTile(who: p.specialist, tone: p.needsYou ? p.tone : nil, size: 36) } DynamicIslandExpandedRegion(.trailing) { if p.needsYou { ActivityNeedsChip() } else if let outcome = p.outcome { Label(outcome, systemImage: p.state.outcome == "done" ? "checkmark" : "xmark") .font(.caption.weight(.semibold)).foregroundStyle(p.state.outcome == "done" ? ActivityPalette.positive : .secondary) .labelStyle(.titleAndIcon) } } DynamicIslandExpandedRegion(.center) { Text(p.title).font(.headline).lineLimit(1) } DynamicIslandExpandedRegion(.bottom) { VStack(alignment: .leading, spacing: 8) { if !p.track.isEmpty { ActivityTrack(track: p.track, labels: true) } ActivityLine(p: p) } .accessibilityElement(children: .combine) } } compactLeading: { Image(systemName: p.specialist.symbol) .foregroundStyle(p.needsYou ? p.tone.dot : .primary) .accessibilityLabel(p.specialist.name) } compactTrailing: { if p.track.isEmpty { Image(systemName: "xmark").foregroundStyle(.secondary) } else { ActivityTrack(track: p.track, labels: false).frame(width: 40) } } minimal: { Image(systemName: p.needsYou ? "hand.raised.fill" : p.specialist.symbol) .foregroundStyle(p.needsYou ? p.tone.dot : .primary) } .widgetURL(p.url) .keylineTint(p.needsYou ? p.tone.dot : nil) } } } struct TaskLockScreen: View { let p: TaskActivityPresentation var body: some View { VStack(alignment: .leading, spacing: 12) { HStack(alignment: .center, spacing: 12) { ActivityTile(who: p.specialist, tone: p.needsYou ? p.tone : nil, size: 40) VStack(alignment: .leading, spacing: 3) { Text(p.title).font(.headline).lineLimit(2) ActivityLine(p: p) } Spacer(minLength: 0) if p.needsYou { ActivityNeedsChip() } } if !p.track.isEmpty { ActivityTrack(track: p.track, labels: true) } } .accessibilityElement(children: .ignore) .accessibilityLabel(p.spoken) } } /// The activity line, or what it needs, or how it ended. struct ActivityLine: View { let p: TaskActivityPresentation var body: some View { Text(p.line) .font(.subheadline.weight(p.needsYou || p.ended ? .semibold : .regular)) .foregroundStyle(p.needsYou ? p.tone.dot : p.state.outcome == "done" ? ActivityPalette.positive : .secondary) .lineLimit(1).truncationMode(.tail) } } /// The specialist's SF Symbol in Roster's rounded tile; amber while it needs you. struct ActivityTile: View { let who: Specialist var tone: StageTone? var size: CGFloat var body: some View { let shape = RoundedRectangle(cornerRadius: size * 0.3, style: .continuous) Image(systemName: who.symbol) .font(.system(size: size * 0.46, weight: .medium)) .foregroundStyle(tone?.dot ?? .primary) .frame(width: size, height: size) .background(tone?.halo ?? Color.primary.opacity(0.08), in: shape) .overlay(shape.strokeBorder(tone?.dot.opacity(0.4) ?? Color.primary.opacity(0.12), lineWidth: 1)) .accessibilityLabel(who.name) } } struct ActivityNeedsChip: View { var body: some View { HStack(spacing: 4) { Image(systemName: "hand.raised").font(.system(size: 11, weight: .semibold)) Text(String(localized: "chip.needs_you", bundle: .module)).textCase(.uppercase).kerning(0.3) } .font(.caption2.weight(.semibold)).foregroundStyle(ActivityPalette.warning) .padding(.horizontal, 8).padding(.vertical, 3) .background(ActivityPalette.warningBackground, in: Capsule()) .fixedSize() } } /// Roster's five stops: done stops filled green, the current one in its /// tone with a halo, the rest hollow. With labels, the current stop's name. struct ActivityTrack: View { let track: [TrackStop] var labels: Bool var body: some View { VStack(alignment: .leading, spacing: 4) { Canvas { context, size in let y = size.height / 2 let r: CGFloat = labels ? 9.5 : 3.5 let xs = track.indices.map { r + CGFloat($0) * (size.width - 2 * r) / CGFloat(max(1, track.count - 1)) } for i in 0..<(xs.count - 1) { let line = Path(CGRect(x: xs[i], y: y - (labels ? 1 : 0.5), width: xs[i + 1] - xs[i], height: labels ? 2 : 1)) context.fill(line, with: .color(track[i].state == .done ? ActivityPalette.positive : ActivityPalette.todo)) } for (i, stop) in track.enumerated() { let c = CGPoint(x: xs[i], y: y) let small: CGFloat = labels ? 4 : 2 switch stop.state { case .done: context.fill(Path(ellipseIn: CGRect(x: c.x - small, y: c.y - small, width: 2 * small, height: 2 * small)), with: .color(ActivityPalette.positive)) case .current: let tone = stop.tone ?? .positive let halo = labels ? 9.5 : 3.5, dot = labels ? 5.5 : 2.5 context.fill(Path(ellipseIn: CGRect(x: c.x - halo, y: c.y - halo, width: 2 * halo, height: 2 * halo)), with: .color(tone.halo)) context.fill(Path(ellipseIn: CGRect(x: c.x - dot, y: c.y - dot, width: 2 * dot, height: 2 * dot)), with: .color(tone.dot)) case .todo: let dot = Path(ellipseIn: CGRect(x: c.x - small, y: c.y - small, width: 2 * small, height: 2 * small)) context.stroke(dot, with: .color(ActivityPalette.todo), lineWidth: 1) } } } .frame(height: labels ? 20 : 8) if labels, let current = track.first(where: { $0.state == .current }) { Text(current.stage.label).font(.caption.weight(.semibold)).foregroundStyle((current.tone ?? .positive).dot) } } .accessibilityHidden(true) } } /// The web day theme's status colours (theme.css), as Roster uses them. enum ActivityPalette { static let positive = Color(light: 0x166534, dark: 0x6EE7A8) static let positiveBackground = Color(light: 0xDCFCE7, dark: 0x10281C) static let warning = Color(light: 0x854D0E, dark: 0xF0C46A) static let warningBackground = Color(light: 0xFEF3C7, dark: 0x2C2110) static let negative = Color(light: 0xB91C1C, dark: 0xF79AA0) static let negativeBackground = Color(light: 0xFEE2E2, dark: 0x331819) static let todo = Color(light: 0x868691, dark: 0x71737C) } extension StageTone { var dot: Color { self == .warning ? ActivityPalette.warning : self == .negative ? ActivityPalette.negative : ActivityPalette.positive } var halo: Color { self == .warning ? ActivityPalette.warningBackground : self == .negative ? ActivityPalette.negativeBackground : ActivityPalette.positiveBackground } } private extension Color { init(light: UInt32, dark: UInt32) { self.init(uiColor: UIColor { traits in let hex = traits.userInterfaceStyle == .dark ? dark : light return UIColor(red: Double((hex >> 16) & 255) / 255, green: Double((hex >> 8) & 255) / 255, blue: Double(hex & 255) / 255, alpha: 1) }) } } #endif