| 1 | import SwiftUI |
| 2 | |
| 3 | /// "Started {when}" (ask.js `startedText`, the web's approved form): how |
| 4 | /// long ago in minutes or hours, then from a day on the day in Base's time |
| 5 | /// zone ("Started on 12 November"). |
| 6 | public enum StartedText { |
| 7 | public static func text(_ at: Date, now: Date, timeZone: TimeZone?) -> String { |
| 8 | let minutes = Int(floor(now.timeIntervalSince(at) / 60)), hours = minutes / 60 |
| 9 | let when: String |
| 10 | if minutes < 1 { when = BaseText.text("ask.when.now") } |
| 11 | else if minutes == 1 { when = BaseText.text("ask.when.minute") } |
| 12 | else if minutes < 60 { when = BaseText.format("ask.when.minutes", ["n": String(minutes)]) } |
| 13 | else if hours == 1 { when = BaseText.text("ask.when.hour") } |
| 14 | else if hours < 24 { when = BaseText.format("ask.when.hours", ["n": String(hours)]) } |
| 15 | else { |
| 16 | var style = Date.FormatStyle(locale: Locale(identifier: "en_GB")).day().month(.wide) |
| 17 | style.timeZone = timeZone ?? .current |
| 18 | when = BaseText.format("ask.when.day", ["day": at.formatted(style)]) |
| 19 | } |
| 20 | return BaseText.format("native.job.started", ["when": when]) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /// A job, a pill (ask.js `addJob`, chat.html `ask-job`): its state's mark, |
| 25 | /// the title and its status on one line, the state's colour washed in from |
| 26 | /// the left, and once there is something to see (ready, done) the link to |
| 27 | /// it. Pressing it opens the job's view: a popover on the Mac, a sheet on |
| 28 | /// the iPhone. The cards waiting on the person hang under it. |
| 29 | struct JobPill: View { |
| 30 | let job: AskModel.Job |
| 31 | let busy: Set<String> |
| 32 | let timeZone: TimeZone? |
| 33 | /// A paused job's "Carry on now", from its view, and why the last one |
| 34 | /// failed (job id to text). |
| 35 | var carryOn: (AskCard) -> Void = { _ in } |
| 36 | var carryOnErrors: [String: String] = [:] |
| 37 | let answer: (AskCard, AskCard.Option) -> Void |
| 38 | @State private var viewing = false |
| 39 | @Environment(\.colorScheme) private var scheme |
| 40 | @Environment(\.openURL) private var openURL |
| 41 | |
| 42 | var body: some View { |
| 43 | VStack(alignment: .leading, spacing: 6) { |
| 44 | pill |
| 45 | ForEach(job.open) { card in |
| 46 | CardView(card: card, busy: busy.contains(card.id), hanging: true) { answer(card, $0) } |
| 47 | .padding(.leading, 22) |
| 48 | .background(alignment: .topLeading) { ThreadLine(color: Palette(scheme)[.pillEdge]) } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// At accessibility text sizes the pill wraps: the title over its |
| 54 | /// status, each on as many lines as it needs, and Try it on a row of |
| 55 | /// its own under them, so nothing that names the job is cut short. |
| 56 | private var stacked: Bool { typeSize.isAccessibilitySize } |
| 57 | @Environment(\.dynamicTypeSize) private var typeSize |
| 58 | |
| 59 | private var pill: some View { |
| 60 | let p = Palette(scheme) |
| 61 | let st = StateMark.color(job.state, p) |
| 62 | let label = BaseText.format("ask.pill_a11y", ["title": job.card.question, "stage": job.card.result]) |
| 63 | let shape = stacked ? AnyShape(RoundedRectangle(cornerRadius: 22, style: .continuous)) : AnyShape(Capsule()) |
| 64 | let main = Button { viewing = true } label: { |
| 65 | Group { |
| 66 | if stacked { |
| 67 | HStack(alignment: .top, spacing: 8) { |
| 68 | StateMark(job.state) |
| 69 | VStack(alignment: .leading, spacing: 2) { |
| 70 | Text(verbatim: job.card.question).font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)) |
| 71 | if !job.card.result.isEmpty { |
| 72 | Text(verbatim: job.card.result).font(BaseFont.body(13, relativeTo: .subheadline)).foregroundStyle(p[.haze]) |
| 73 | } |
| 74 | } |
| 75 | .fixedSize(horizontal: false, vertical: true) |
| 76 | .frame(maxWidth: .infinity, alignment: .leading) |
| 77 | } |
| 78 | .padding(.vertical, 8) |
| 79 | } else { |
| 80 | HStack(spacing: 8) { |
| 81 | StateMark(job.state) |
| 82 | Text(verbatim: job.card.question) |
| 83 | .font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)) |
| 84 | .lineLimit(1).truncationMode(.tail) |
| 85 | .frame(minWidth: 40, alignment: .leading) |
| 86 | .layoutPriority(0) |
| 87 | if !job.card.result.isEmpty { |
| 88 | Text(verbatim: "· " + job.card.result) |
| 89 | .font(BaseFont.body(13, relativeTo: .subheadline)).foregroundStyle(p[.haze]) |
| 90 | .lineLimit(1) |
| 91 | .layoutPriority(1) |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | .padding(.leading, 8).padding(.trailing, 14) |
| 97 | .frame(minHeight: 44) |
| 98 | .contentShape(shape) |
| 99 | } |
| 100 | .buttonStyle(.plain) |
| 101 | .accessibilityLabel(Text(verbatim: label)) |
| 102 | .accessibilityAddTraits(.isButton) |
| 103 | .modifier(JobViewPresentation(isPresented: $viewing, job: job, timeZone: timeZone, |
| 104 | carrying: busy.contains(job.card.id), carryOnError: carryOnErrors[job.card.id], |
| 105 | carryOn: { carryOn(job.card) })) |
| 106 | |
| 107 | let link = job.card.go.flatMap { go in go.url.map { (go.label, $0) } }.map { label, url in |
| 108 | Button { openURL(url) } label: { |
| 109 | Text(verbatim: label) |
| 110 | .font(BaseFont.body(13.5, .semibold, relativeTo: .subheadline)).underline() |
| 111 | .foregroundStyle(p[.focus]) |
| 112 | .padding(.leading, stacked ? 44 : 10).padding(.trailing, 14) |
| 113 | .frame(minHeight: 44, alignment: .leading) |
| 114 | .contentShape(Rectangle()) |
| 115 | } |
| 116 | .buttonStyle(.plain) |
| 117 | .accessibilityAddTraits(.isLink) |
| 118 | } |
| 119 | |
| 120 | let layout = stacked ? AnyLayout(VStackLayout(alignment: .leading, spacing: 0)) : AnyLayout(HStackLayout(spacing: 0)) |
| 121 | return layout { |
| 122 | main |
| 123 | if let link { |
| 124 | Rectangle().fill(p[.pillEdge]).frame(width: stacked ? nil : 1, height: stacked ? 1 : nil) |
| 125 | link |
| 126 | } |
| 127 | } |
| 128 | .foregroundStyle(p[.frost]) |
| 129 | .fixedSize(horizontal: false, vertical: true) |
| 130 | .background { |
| 131 | ZStack { |
| 132 | shape.fill(p[.pill]) |
| 133 | shape.fill(LinearGradient(stops: [.init(color: st.opacity(p.stateWash), location: 0), .init(color: st.opacity(0), location: 0.7)], |
| 134 | startPoint: .leading, endPoint: .trailing)) |
| 135 | } |
| 136 | .shadow(color: p[.pillLift].opacity(0.6), radius: 9, y: 8) |
| 137 | } |
| 138 | .overlay { |
| 139 | ZStack { |
| 140 | shape.stroke(p[.pillEdge], lineWidth: 1) |
| 141 | TopShine(radius: 22, color: p[.pillShine]) |
| 142 | } |
| 143 | .allowsHitTesting(false) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// The line from a pill down to a card that hangs under it: left -10, |
| 149 | /// from 6 above the card to its middle, turning in with a 10 pt corner. |
| 150 | struct ThreadLine: View { |
| 151 | let color: Color |
| 152 | var body: some View { |
| 153 | GeometryReader { geo in |
| 154 | Path { path in |
| 155 | let x: CGFloat = 12, bottom = geo.size.height / 2 |
| 156 | path.move(to: CGPoint(x: x, y: -6)) |
| 157 | path.addLine(to: CGPoint(x: x, y: bottom - 10)) |
| 158 | path.addQuadCurve(to: CGPoint(x: x + 10, y: bottom), control: CGPoint(x: x, y: bottom)) |
| 159 | } |
| 160 | .stroke(color, lineWidth: 2) |
| 161 | } |
| 162 | .frame(width: 22) |
| 163 | .accessibilityHidden(true) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /// The job view: a popover beside the pill on the Mac, a sheet from the |
| 168 | /// foot of the screen on the iPhone. |
| 169 | struct JobViewPresentation: ViewModifier { |
| 170 | @Binding var isPresented: Bool |
| 171 | let job: AskModel.Job |
| 172 | let timeZone: TimeZone? |
| 173 | let carrying: Bool |
| 174 | let carryOnError: String? |
| 175 | let carryOn: () -> Void |
| 176 | func body(content: Content) -> some View { |
| 177 | #if os(macOS) |
| 178 | content.popover(isPresented: $isPresented, arrowEdge: .top) { |
| 179 | JobView(job: job, timeZone: timeZone, close: { isPresented = false }, carrying: carrying, carryOnError: carryOnError, carryOn: carryOn) |
| 180 | .frame(width: 320) |
| 181 | .frame(maxHeight: 520) |
| 182 | } |
| 183 | #else |
| 184 | content.sheet(isPresented: $isPresented) { |
| 185 | JobView(job: job, timeZone: timeZone, close: { isPresented = false }, carrying: carrying, carryOnError: carryOnError, carryOn: carryOn) |
| 186 | .presentationDetents([.medium, .large]) |
| 187 | .presentationDragIndicator(.visible) |
| 188 | } |
| 189 | #endif |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// What the job view shows (chat.html `ask-jobview`): the title, linked to |
| 194 | /// the job's own page in Equipo; the status; one sentence more; for a |
| 195 | /// paused job that offers it, "Carry on now" and the line under it; the cards |
| 196 | /// that have ended, each its question and how it ended; when it started; |
| 197 | /// and the link to the work once there is something to see. The jobs |
| 198 | /// design (11-20) adds its decisions here later. |
| 199 | public struct JobView: View { |
| 200 | let job: AskModel.Job |
| 201 | let timeZone: TimeZone? |
| 202 | let close: () -> Void |
| 203 | var now: Date = .now |
| 204 | /// Carry on now is on its way: its button is disabled. |
| 205 | let carrying: Bool |
| 206 | /// Why the last Carry on now failed, shown under it. |
| 207 | let carryOnError: String? |
| 208 | let carryOn: () -> Void |
| 209 | @Environment(\.colorScheme) private var scheme |
| 210 | @Environment(\.openURL) private var openURL |
| 211 | |
| 212 | public init(job: AskModel.Job, timeZone: TimeZone?, close: @escaping () -> Void, now: Date = .now, |
| 213 | carrying: Bool = false, carryOnError: String? = nil, carryOn: @escaping () -> Void = {}) { |
| 214 | self.job = job; self.timeZone = timeZone; self.close = close; self.now = now |
| 215 | self.carrying = carrying; self.carryOnError = carryOnError; self.carryOn = carryOn |
| 216 | } |
| 217 | |
| 218 | public var body: some View { |
| 219 | let p = Palette(scheme) |
| 220 | VStack(alignment: .leading, spacing: 0) { |
| 221 | HStack(spacing: 10) { |
| 222 | StateMark(job.state, size: 26) |
| 223 | title(p) |
| 224 | Spacer(minLength: 0) |
| 225 | Button(action: close) { |
| 226 | Icon(.close).frame(width: 20, height: 20).frame(width: 44, height: 44).contentShape(Circle()) |
| 227 | } |
| 228 | .buttonStyle(.plain) |
| 229 | .accessibilityLabel(Text(BaseText.text("ask.close"))) |
| 230 | } |
| 231 | // Long enough to overflow its sheet or popover (many ended |
| 232 | // cards, large text), the details scroll under the header, |
| 233 | // whose Close stays put. |
| 234 | ViewThatFits(in: .vertical) { |
| 235 | details(p) |
| 236 | ScrollView { details(p) }.scrollBounceBehavior(.basedOnSize) |
| 237 | } |
| 238 | } |
| 239 | .foregroundStyle(p[.frost]) |
| 240 | .padding(.top, 10).padding(.trailing, 10).padding(.bottom, 12).padding(.leading, 16) |
| 241 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) |
| 242 | .background(p[.menuBase]) |
| 243 | .accessibilityElement(children: .contain) |
| 244 | .accessibilityLabel(Text(verbatim: job.card.question)) |
| 245 | } |
| 246 | |
| 247 | private func details(_ p: Palette) -> some View { |
| 248 | VStack(alignment: .leading, spacing: 2) { |
| 249 | if !job.card.result.isEmpty { Text(verbatim: job.card.result).font(BaseFont.body(14, .medium)) } |
| 250 | if !job.card.detail.isEmpty { Text(verbatim: job.card.detail).font(BaseFont.body(14)).foregroundStyle(p[.haze]) } |
| 251 | if job.card.offersCarryOn { |
| 252 | VStack(alignment: .leading, spacing: 6) { |
| 253 | Button(action: carryOn) { Text(BaseText.text("assistant.job.carry_on")) } |
| 254 | .buttonStyle(BaseButtonStyle(.ghost)) |
| 255 | .disabled(carrying) |
| 256 | // A held job (stopped) carries on on the same plan: no note. |
| 257 | if job.card.jobState == .paused { |
| 258 | Text(BaseText.text("assistant.job.carry_on_note")) |
| 259 | .font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) |
| 260 | } |
| 261 | if let carryOnError { |
| 262 | Text(verbatim: carryOnError) |
| 263 | .font(BaseFont.body(13, relativeTo: .footnote)) |
| 264 | } |
| 265 | } |
| 266 | .padding(.top, 12) |
| 267 | } |
| 268 | if !job.ended.isEmpty { |
| 269 | VStack(alignment: .leading, spacing: 6) { |
| 270 | ForEach(job.ended) { card in |
| 271 | VStack(alignment: .leading, spacing: 0) { |
| 272 | Text(verbatim: card.question).font(BaseFont.body(14)) |
| 273 | Text(verbatim: card.result).font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | .padding(.top, 8) |
| 278 | .overlay(alignment: .top) { Rectangle().fill(p[.rule]).frame(height: 1) } |
| 279 | .padding(.top, 10) |
| 280 | } |
| 281 | if let started = job.card.startedAt { |
| 282 | Text(verbatim: StartedText.text(started, now: now, timeZone: timeZone)) |
| 283 | .font(BaseFont.body(13, relativeTo: .footnote)).foregroundStyle(p[.haze]) |
| 284 | .padding(.top, 8) |
| 285 | } |
| 286 | if let go = job.card.go, let url = go.url { |
| 287 | Button { openURL(url) } label: { Text(verbatim: go.label) } |
| 288 | .buttonStyle(BaseButtonStyle(.primary)) |
| 289 | .padding(.top, 14) |
| 290 | } |
| 291 | } |
| 292 | .padding(.leading, 36).padding(.trailing, 10) |
| 293 | .fixedSize(horizontal: false, vertical: true) |
| 294 | .frame(maxWidth: .infinity, alignment: .leading) |
| 295 | } |
| 296 | |
| 297 | @ViewBuilder private func title(_ p: Palette) -> some View { |
| 298 | let text = Text(verbatim: job.card.question).font(BaseFont.heading(16, .semibold, relativeTo: .headline)) |
| 299 | if let page = job.card.equipoLink, let url = page.url { |
| 300 | Button { openURL(url) } label: { |
| 301 | HStack(spacing: 6) { |
| 302 | text.multilineTextAlignment(.leading) |
| 303 | Icon(.arrowOut, weight: 2.2).foregroundStyle(p[.haze]).frame(width: 13, height: 13) |
| 304 | } |
| 305 | .frame(minHeight: 44) |
| 306 | } |
| 307 | .buttonStyle(.plain) |
| 308 | .help(Text(verbatim: page.label)) |
| 309 | .accessibilityAddTraits(.isLink) |
| 310 | } else { |
| 311 | text.padding(.vertical, 11) |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |