import SwiftUI /// "Started {when}" (ask.js `startedText`, the web's approved form): how /// long ago in minutes or hours, then from a day on the day in Base's time /// zone ("Started on 12 November"). public enum StartedText { public static func text(_ at: Date, now: Date, timeZone: TimeZone?) -> String { let minutes = Int(floor(now.timeIntervalSince(at) / 60)), hours = minutes / 60 let when: String if minutes < 1 { when = BaseText.text("ask.when.now") } else if minutes == 1 { when = BaseText.text("ask.when.minute") } else if minutes < 60 { when = BaseText.format("ask.when.minutes", ["n": String(minutes)]) } else if hours == 1 { when = BaseText.text("ask.when.hour") } else if hours < 24 { when = BaseText.format("ask.when.hours", ["n": String(hours)]) } else { var style = Date.FormatStyle(locale: Locale(identifier: "en_GB")).day().month(.wide) style.timeZone = timeZone ?? .current when = BaseText.format("ask.when.day", ["day": at.formatted(style)]) } return BaseText.format("native.job.started", ["when": when]) } } /// A job, a pill (ask.js `addJob`, chat.html `ask-job`): its state's mark, /// the title and its status on one line, the state's colour washed in from /// the left, and once there is something to see (ready, done) the link to /// it. Pressing it opens the job's view: a popover on the Mac, a sheet on /// the iPhone. The cards waiting on the person hang under it. struct JobPill: View { let job: AskModel.Job let busy: Set let timeZone: TimeZone? /// A paused job's "Carry on now", from its view, and why the last one /// failed (job id to text). var carryOn: (AskCard) -> Void = { _ in } var carryOnErrors: [String: String] = [:] let answer: (AskCard, AskCard.Option) -> Void @State private var viewing = false @Environment(\.colorScheme) private var scheme @Environment(\.openURL) private var openURL var body: some View { VStack(alignment: .leading, spacing: 6) { pill ForEach(job.open) { card in CardView(card: card, busy: busy.contains(card.id), hanging: true) { answer(card, $0) } .padding(.leading, 22) .background(alignment: .topLeading) { ThreadLine(color: Palette(scheme)[.pillEdge]) } } } } /// At accessibility text sizes the pill wraps: the title over its /// status, each on as many lines as it needs, and Try it on a row of /// its own under them, so nothing that names the job is cut short. private var stacked: Bool { typeSize.isAccessibilitySize } @Environment(\.dynamicTypeSize) private var typeSize private var pill: some View { let p = Palette(scheme) let st = StateMark.color(job.state, p) let label = BaseText.format("ask.pill_a11y", ["title": job.card.question, "stage": job.card.result]) let shape = stacked ? AnyShape(RoundedRectangle(cornerRadius: 22, style: .continuous)) : AnyShape(Capsule()) let main = Button { viewing = true } label: { Group { if stacked { HStack(alignment: .top, spacing: 8) { StateMark(job.state) VStack(alignment: .leading, spacing: 2) { Text(verbatim: job.card.question).font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)) if !job.card.result.isEmpty { Text(verbatim: job.card.result).font(BaseFont.body(13, relativeTo: .subheadline)).foregroundStyle(p[.haze]) } } .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) } .padding(.vertical, 8) } else { HStack(spacing: 8) { StateMark(job.state) Text(verbatim: job.card.question) .font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)) .lineLimit(1).truncationMode(.tail) .frame(minWidth: 40, alignment: .leading) .layoutPriority(0) if !job.card.result.isEmpty { Text(verbatim: "ยท " + job.card.result) .font(BaseFont.body(13, relativeTo: .subheadline)).foregroundStyle(p[.haze]) .lineLimit(1) .layoutPriority(1) } } } } .padding(.leading, 8).padding(.trailing, 14) .frame(minHeight: 44) .contentShape(shape) } .buttonStyle(.plain) .accessibilityLabel(Text(verbatim: label)) .accessibilityAddTraits(.isButton) .modifier(JobViewPresentation(isPresented: $viewing, job: job, timeZone: timeZone, carrying: busy.contains(job.card.id), carryOnError: carryOnErrors[job.card.id], carryOn: { carryOn(job.card) })) let link = job.card.go.flatMap { go in go.url.map { (go.label, $0) } }.map { label, url in Button { openURL(url) } label: { Text(verbatim: label) .font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)).underline() .foregroundStyle(p[.focus]) .padding(.leading, stacked ? 44 : 10).padding(.trailing, 14) .frame(minHeight: 44, alignment: .leading) .contentShape(Rectangle()) } .buttonStyle(.plain) .accessibilityAddTraits(.isLink) } let layout = stacked ? AnyLayout(VStackLayout(alignment: .leading, spacing: 0)) : AnyLayout(HStackLayout(spacing: 0)) return layout { main if let link { Rectangle().fill(p[.pillEdge]).frame(width: stacked ? nil : 1, height: stacked ? 1 : nil) link } } .foregroundStyle(p[.frost]) .fixedSize(horizontal: false, vertical: true) .background { ZStack { shape.fill(p[.pill]) shape.fill(LinearGradient(stops: [.init(color: st.opacity(p.stateWash), location: 0), .init(color: st.opacity(0), location: 0.7)], startPoint: .leading, endPoint: .trailing)) } .shadow(color: p[.pillLift].opacity(0.6), radius: 9, y: 8) } .overlay { ZStack { shape.stroke(p[.pillEdge], lineWidth: 1) TopShine(radius: 22, color: p[.pillShine]) } .allowsHitTesting(false) } } } /// The line from a pill down to a card that hangs under it: left -10, /// from 6 above the card to its middle, turning in with a 10 pt corner. struct ThreadLine: View { let color: Color var body: some View { GeometryReader { geo in Path { path in let x: CGFloat = 12, bottom = geo.size.height / 2 path.move(to: CGPoint(x: x, y: -6)) path.addLine(to: CGPoint(x: x, y: bottom - 10)) path.addQuadCurve(to: CGPoint(x: x + 10, y: bottom), control: CGPoint(x: x, y: bottom)) } .stroke(color, lineWidth: 2) } .frame(width: 22) .accessibilityHidden(true) } } /// The job view: a popover beside the pill on the Mac, a sheet from the /// foot of the screen on the iPhone. struct JobViewPresentation: ViewModifier { @Binding var isPresented: Bool let job: AskModel.Job let timeZone: TimeZone? let carrying: Bool let carryOnError: String? let carryOn: () -> Void func body(content: Content) -> some View { #if os(macOS) content.popover(isPresented: $isPresented, arrowEdge: .top) { JobView(job: job, timeZone: timeZone, close: { isPresented = false }, carrying: carrying, carryOnError: carryOnError, carryOn: carryOn) .frame(width: 320) .frame(maxHeight: 520) } #else content.sheet(isPresented: $isPresented) { JobView(job: job, timeZone: timeZone, close: { isPresented = false }, carrying: carrying, carryOnError: carryOnError, carryOn: carryOn) .presentationDetents([.medium, .large]) .presentationDragIndicator(.visible) } #endif } } /// What the job view shows (chat.html `ask-jobview`): the title, linked to /// the job's own page in Equipo; the status; one sentence more; for a /// paused job that offers it, "Carry on now" and the line under it; the cards /// that have ended, each its question and how it ended; when it started; /// and the link to the work once there is something to see. The jobs /// design (11-20) adds its decisions here later. public struct JobView: View { let job: AskModel.Job let timeZone: TimeZone? let close: () -> Void var now: Date = .now /// Carry on now is on its way: its button is disabled. let carrying: Bool /// Why the last Carry on now failed, shown under it. let carryOnError: String? let carryOn: () -> Void @Environment(\.colorScheme) private var scheme @Environment(\.openURL) private var openURL public init(job: AskModel.Job, timeZone: TimeZone?, close: @escaping () -> Void, now: Date = .now, carrying: Bool = false, carryOnError: String? = nil, carryOn: @escaping () -> Void = {}) { self.job = job; self.timeZone = timeZone; self.close = close; self.now = now self.carrying = carrying; self.carryOnError = carryOnError; self.carryOn = carryOn } public var body: some View { let p = Palette(scheme) VStack(alignment: .leading, spacing: 0) { HStack(spacing: 10) { StateMark(job.state, size: 26) title(p) Spacer(minLength: 0) Button(action: close) { Icon(.close).frame(width: 20, height: 20).frame(width: 44, height: 44).contentShape(Circle()) } .buttonStyle(.plain) .accessibilityLabel(Text(BaseText.text("ask.close"))) } // Long enough to overflow its sheet or popover (many ended // cards, large text), the details scroll under the header, // whose Close stays put. ViewThatFits(in: .vertical) { details(p) ScrollView { details(p) }.scrollBounceBehavior(.basedOnSize) } } .foregroundStyle(p[.frost]) .padding(.top, 10).padding(.trailing, 10).padding(.bottom, 12).padding(.leading, 16) .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) .background(p[.menuBase]) .accessibilityElement(children: .contain) .accessibilityLabel(Text(verbatim: job.card.question)) } private func details(_ p: Palette) -> some View { VStack(alignment: .leading, spacing: 2) { if !job.card.result.isEmpty { Text(verbatim: job.card.result).font(BaseFont.body(14, .medium)) } if !job.card.detail.isEmpty { Text(verbatim: job.card.detail).font(BaseFont.body(14)).foregroundStyle(p[.haze]) } if job.card.offersCarryOn { VStack(alignment: .leading, spacing: 6) { Button(action: carryOn) { Text(BaseText.text("assistant.job.carry_on")) } .buttonStyle(BaseButtonStyle(.ghost)) .disabled(carrying) // A held job (stopped) carries on on the same plan: no note. if job.card.jobState == .paused { Text(BaseText.text("assistant.job.carry_on_note")) .font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) } if let carryOnError { Text(verbatim: carryOnError) .font(BaseFont.body(13, relativeTo: .footnote)) } } .padding(.top, 12) } if !job.ended.isEmpty { VStack(alignment: .leading, spacing: 6) { ForEach(job.ended) { card in VStack(alignment: .leading, spacing: 0) { Text(verbatim: card.question).font(BaseFont.body(14)) Text(verbatim: card.result).font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) } } } .padding(.top, 8) .overlay(alignment: .top) { Rectangle().fill(p[.rule]).frame(height: 1) } .padding(.top, 10) } if let started = job.card.startedAt { Text(verbatim: StartedText.text(started, now: now, timeZone: timeZone)) .font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) .padding(.top, 8) } if let go = job.card.go, let url = go.url { Button { openURL(url) } label: { Text(verbatim: go.label) } .buttonStyle(BaseButtonStyle(.primary)) .padding(.top, 14) } } .padding(.leading, 36).padding(.trailing, 10) .fixedSize(horizontal: false, vertical: true) .frame(maxWidth: .infinity, alignment: .leading) } @ViewBuilder private func title(_ p: Palette) -> some View { let text = Text(verbatim: job.card.question).font(BaseFont.heading(16, .semibold, relativeTo: .headline)) if let page = job.card.equipoLink, let url = page.url { Button { openURL(url) } label: { HStack(spacing: 6) { text.multilineTextAlignment(.leading) Icon(.arrowOut, weight: 2.2).foregroundStyle(p[.haze]).frame(width: 13, height: 13) } .frame(minHeight: 44) } .buttonStyle(.plain) .help(Text(verbatim: page.label)) .accessibilityAddTraits(.isLink) } else { text.padding(.vertical, 11) } } }