| 1 | import SwiftUI |
| 2 | |
| 3 | /// Tasks, grouped as the web groups them. |
| 4 | public struct TasksScreen: View { |
| 5 | let session: EquipoSession |
| 6 | var openTask: (String) -> Void |
| 7 | public init(session: EquipoSession, openTask: @escaping (String) -> Void) { self.session = session; self.openTask = openTask } |
| 8 | public var body: some View { |
| 9 | List { |
| 10 | group(String(localized: "tasks.needs_you", bundle: .module), session.tasks.filter { session.waitsOnYou($0) }) |
| 11 | group(String(localized: "tasks.in_progress", bundle: .module), session.tasks.filter { !session.waitsOnYou($0) && !Self.finished($0) }) |
| 12 | group(String(localized: "tasks.finished", bundle: .module), session.tasks.filter { !session.waitsOnYou($0) && Self.finished($0) }) |
| 13 | } |
| 14 | .scrollContentBackground(.hidden).background(Palette.background) |
| 15 | .refreshable { await session.refresh() } |
| 16 | .overlay { |
| 17 | if session.status == .ready && session.tasks.isEmpty { |
| 18 | ContentUnavailableView(String(localized: "tasks.empty_title", bundle: .module), systemImage: "checklist", |
| 19 | description: Text(String(localized: "tasks.empty", bundle: .module))) |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | static func finished(_ t: ThinTask) -> Bool { t.stage == "done" || t.stage == "cancelled" } |
| 24 | @ViewBuilder private func group(_ title: String, _ tasks: [ThinTask]) -> some View { |
| 25 | if !tasks.isEmpty { |
| 26 | Section(title) { |
| 27 | ForEach(tasks) { task in |
| 28 | TaskRowButton(task: task, needsYou: session.waitsOnYou(task)) { openTask(task.id) } |
| 29 | } |
| 30 | }.listRowBackground(Palette.surface) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | struct TaskRowButton: View { |
| 36 | let task: ThinTask |
| 37 | let needsYou: Bool |
| 38 | var showsNeedsBadge = true |
| 39 | var status: String? = nil |
| 40 | var action: () -> Void |
| 41 | var body: some View { |
| 42 | let report = TaskReport(task, needsYou: needsYou) |
| 43 | Button(action: action) { |
| 44 | HStack(alignment: .center, spacing: 10) { |
| 45 | VStack(alignment: .leading, spacing: 5) { |
| 46 | HStack(spacing: 8) { |
| 47 | if !task.project.isEmpty { ProjectChip(name: task.project) } |
| 48 | Spacer(minLength: 8) |
| 49 | if showsNeedsBadge && report.needsYou { NeedsChip() } |
| 50 | } |
| 51 | Text(task.title).font(Typo.cardTitle).foregroundStyle(Palette.text).multilineTextAlignment(.leading) |
| 52 | Text(status ?? report.pause?.stop ?? report.eyebrow).font(Typo.eyebrow).foregroundStyle(report.tone.eyebrow) |
| 53 | } |
| 54 | Image(systemName: "chevron.right").font(.system(size: 12, weight: .semibold)).foregroundStyle(Palette.faint) |
| 55 | } |
| 56 | .padding(.vertical, 4).contentShape(Rectangle()) |
| 57 | } |
| 58 | .buttonStyle(.plain) |
| 59 | .accessibilityElement(children: .combine) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// A task's open question or decision cards, as the conversation has them. |
| 64 | struct TaskCards: View { |
| 65 | let session: EquipoSession |
| 66 | let task: ThinTask |
| 67 | var body: some View { |
| 68 | ForEach(session.cards.filter { $0.taskId == task.id && $0.kind != "task" && $0.isOpen }) { card in |
| 69 | if card.kind == "question" { |
| 70 | LiveQuestionCard(card: card, taskTitle: nil, session: session) |
| 71 | } else { |
| 72 | LiveDecisionCard(card: card, taskTitle: task.title, session: session) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// "Talk about this": the task's own thread, with a compact composer. |
| 79 | struct TaskThread: View { |
| 80 | let session: EquipoSession |
| 81 | let task: ThinTask |
| 82 | var sent: () -> Void = {} |
| 83 | var body: some View { |
| 84 | let turns = (task.thread ?? []).map { ThreadTurn($0, me: session.me?.address) } |
| 85 | + session.outgoing.filter { $0.taskID == task.id }.map(ThreadTurn.init) |
| 86 | VStack(alignment: .leading, spacing: Metric.gap) { |
| 87 | if turns.isEmpty { |
| 88 | Text(String(localized: "task.thread_empty", bundle: .module)).font(Typo.secondary).foregroundStyle(Palette.secondary) |
| 89 | } |
| 90 | ForEach(turns) { turn in ThreadBubble(turn: turn) { Task { await session.retry(turn.id) } } } |
| 91 | MiniComposer(placeholder: String(localized: "task.reply", bundle: .module)) { text in |
| 92 | Task { await session.send(text, inThreadOf: task.id); sent() } |
| 93 | } |
| 94 | } |
| 95 | .padding(12) |
| 96 | .background(Palette.surface2, in: RoundedRectangle(cornerRadius: Metric.raised - 4, style: .continuous)) |
| 97 | .overlay(RoundedRectangle(cornerRadius: Metric.raised - 4, style: .continuous).strokeBorder(Palette.border, lineWidth: 1)) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /// The team answered, but not with what the screens need: say so, offer again. |
| 102 | struct ReadFailed: View { |
| 103 | var retry: () -> Void |
| 104 | var body: some View { |
| 105 | ContentUnavailableView { |
| 106 | Label(String(localized: "read.failed_title", bundle: .module), systemImage: "exclamationmark.triangle") |
| 107 | } description: { |
| 108 | Text(String(localized: "read.failed", bundle: .module)) |
| 109 | } actions: { |
| 110 | RosterButton(title: String(localized: "read.retry", bundle: .module), action: retry) |
| 111 | } |
| 112 | .padding(.top, 40) |
| 113 | } |
| 114 | } |
| 115 | |