| 1 | import RastrilloApple |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// The conversation: Roster's bylines and cards over live data, with the |
| 5 | /// composer at the bottom. Used as is on iPhone; the Mac draws the same feed |
| 6 | /// in AppKit. |
| 7 | public struct ConversationScreen: View { |
| 8 | let session: EquipoSession |
| 9 | /// A card to bring into view (a tapped notification); cleared once shown. |
| 10 | @Binding var focus: String? |
| 11 | var openTask: (String) -> Void |
| 12 | @State private var viewport: CGFloat = 0 |
| 13 | public init(session: EquipoSession, focus: Binding<String?> = .constant(nil), openTask: @escaping (String) -> Void) { |
| 14 | self.session = session; _focus = focus; self.openTask = openTask |
| 15 | } |
| 16 | |
| 17 | public var body: some View { |
| 18 | let items = Feed.build(session) |
| 19 | ScrollViewReader { proxy in |
| 20 | ScrollView { |
| 21 | LazyVStack(alignment: .leading, spacing: 20) { |
| 22 | if items.isEmpty && session.status != .ready && session.refreshFailed { |
| 23 | ReadFailed { Task { await session.refresh() } } |
| 24 | } |
| 25 | if items.isEmpty && session.status == .ready { |
| 26 | Text(String(localized: "conversation.empty", bundle: .module)) |
| 27 | .font(Typo.body).foregroundStyle(Palette.secondary).padding(.top, 40) |
| 28 | } |
| 29 | ForEach(items) { item in row(item).id(item.id) } |
| 30 | Color.clear.frame(height: 8).id("bottom") |
| 31 | } |
| 32 | .padding(.horizontal, 16).padding(.top, 12).padding(.bottom, 20) |
| 33 | .frame(maxWidth: 760).frame(maxWidth: .infinity) |
| 34 | // At least the visible height, so a short conversation's empty |
| 35 | // space is background too. |
| 36 | .frame(minHeight: viewport, alignment: .bottom) |
| 37 | .tapDismissesKeyboard() |
| 38 | } |
| 39 | .onScrollGeometryChange(for: CGFloat.self) { $0.containerSize.height } action: { _, height in viewport = height } |
| 40 | .accessibilityIdentifier("conversation-scroll") |
| 41 | .defaultScrollAnchor(.bottom) |
| 42 | .refreshable { await session.refresh() } |
| 43 | .onChange(of: items.count) { _, _ in |
| 44 | if focus != nil { show(focus, items, proxy) } else { withAnimation { proxy.scrollTo("bottom", anchor: .bottom) } } |
| 45 | } |
| 46 | .onChange(of: focus, initial: true) { _, id in show(id, items, proxy) } |
| 47 | } |
| 48 | .background(Palette.background) |
| 49 | .safeAreaInset(edge: .top, spacing: 0) { NativeOfflineBanner(origin: session.origin) } |
| 50 | .safeAreaInset(edge: .bottom, spacing: 0) { |
| 51 | VStack(spacing: 0) { |
| 52 | ProjectSelectionChip(catalog: session.projectCatalog) |
| 53 | Composer(placeholder: String(localized: "conversation.placeholder", bundle: .module), attachments: session.attachments) { text in |
| 54 | _ = sendFromComposer(text) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | .dismissesKeyboardWhileReading() |
| 59 | .keyboardPinned() |
| 60 | } |
| 61 | |
| 62 | func sendFromComposer(_ text: String) -> Task<Void, Never> { session.sendFromComposer(text) } |
| 63 | |
| 64 | /// Scrolls to the card once the feed has it; reads again if it does not yet. |
| 65 | private func show(_ id: String?, _ items: [FeedItem], _ proxy: ScrollViewProxy) { |
| 66 | guard let id else { return } |
| 67 | if items.contains(where: { $0.id == id }) { |
| 68 | Task { @MainActor in |
| 69 | withAnimation { proxy.scrollTo(id, anchor: .center) } |
| 70 | focus = nil |
| 71 | } |
| 72 | } else { |
| 73 | // Not in the feed yet: read again, and give up if it still is not. |
| 74 | Task { @MainActor in |
| 75 | await session.refresh() |
| 76 | if !Feed.build(session).contains(where: { $0.id == id }) { focus = nil } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | @ViewBuilder private func row(_ item: FeedItem) -> some View { |
| 82 | if let reply = item.reply { |
| 83 | CoordinatorMessage(reply: reply, messageID: item.id, origin: session.origin, transcript: session.markdown, openTask: openTask) |
| 84 | } else { |
| 85 | switch item { |
| 86 | case .draft, .coordinator, .notice: EmptyView() |
| 87 | case .own(let m): OwnMessage(text: m.text, attachments: m.attachments ?? [], client: session.client) |
| 88 | case .outgoing(let o): OutgoingMessage(item: o, client: session.client) { Task { await session.retry(o.id) } } |
| 89 | case .report(_, let byline, let report, let compact): |
| 90 | VStack(alignment: .leading, spacing: 8) { |
| 91 | Byline(byline: byline) |
| 92 | LiveReport(report: report, session: session, compact: compact, onRunNow: { run in Task { await session.runNow(task: report.task.id, run) } }) { openTask(report.task.id) } |
| 93 | } |
| 94 | case .question(let card, let byline): |
| 95 | VStack(alignment: .leading, spacing: 8) { |
| 96 | Byline(byline: byline) |
| 97 | LiveQuestionCard(card: card, taskTitle: session.task(card.taskId)?.title, session: session) |
| 98 | } |
| 99 | case .decision(let card, let byline): |
| 100 | VStack(alignment: .leading, spacing: 8) { |
| 101 | Byline(byline: byline) |
| 102 | LiveDecisionCard(card: card, taskTitle: session.task(card.taskId)?.title, session: session) |
| 103 | } |
| 104 | case .receipt(let card, _): |
| 105 | LiveReceipt(card: card) |
| 106 | case .thinking(let turn, let activity): |
| 107 | ThinkingLine(turn: turn, activity: activity, stopping: session.stopping.contains(turn.id)) { Task { await session.stop(turn) } } |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /// A message this device is still sending, and how it is going. |
| 114 | struct OutgoingMessage: View { |
| 115 | let item: EquipoSession.Outgoing |
| 116 | var client: EquipoClient? = nil |
| 117 | var retry: () -> Void |
| 118 | var body: some View { |
| 119 | VStack(alignment: .trailing, spacing: 4) { |
| 120 | if let project = item.projectName { ProjectChip(name: project) } |
| 121 | OwnMessage(text: item.text, attachments: item.attachments, client: client).opacity(item.state == .sending ? 0.7 : 1) |
| 122 | if let notice = item.notice { Text(notice).font(Typo.secondary).foregroundStyle(Palette.negative) } |
| 123 | switch item.state { |
| 124 | case .sending: Text(String(localized: "message.sending", bundle: .module)).font(Typo.caption).foregroundStyle(Palette.faint) |
| 125 | case .waiting: |
| 126 | Button(action: retry) { Text(String(localized: "message.waiting", bundle: .module)).underline() } |
| 127 | .buttonStyle(.plain).font(Typo.caption).foregroundStyle(Palette.secondary).frame(minHeight: Metric.target) |
| 128 | case .failed: |
| 129 | Button(action: retry) { Text(String(localized: "message.failed", bundle: .module)).underline() } |
| 130 | .buttonStyle(.plain).font(Typo.caption).foregroundStyle(Palette.negative).frame(minHeight: Metric.target) |
| 131 | } |
| 132 | } |
| 133 | .frame(maxWidth: .infinity, alignment: .trailing) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /// The composer: attachments above, an add button beside the tinted field. |
| 138 | struct Composer: View { |
| 139 | let placeholder: String |
| 140 | var attachments: AttachmentDraft? = nil |
| 141 | var onSend: (String) -> Void |
| 142 | @State private var text = "" |
| 143 | @FocusState private var focused: Bool |
| 144 | #if os(iOS) |
| 145 | @State private var screenshot = RecentScreenshot() |
| 146 | @Environment(\.scenePhase) private var scenePhase |
| 147 | #endif |
| 148 | var body: some View { |
| 149 | let has = attachments?.canSend(text: text) ?? !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 150 | let shape = RoundedRectangle(cornerRadius: 10, style: .continuous) |
| 151 | VStack(spacing: 4) { |
| 152 | #if os(iOS) |
| 153 | if let attachments { ScreenshotChip(model: screenshot, draft: attachments) } |
| 154 | #endif |
| 155 | if let attachments { AttachmentDraftStrip(draft: attachments) } |
| 156 | HStack(alignment: .bottom, spacing: 8) { |
| 157 | #if os(iOS) |
| 158 | if let attachments { PhoneAttachmentPicker(draft: attachments, screenshot: screenshot) } |
| 159 | #endif |
| 160 | HStack(alignment: .bottom, spacing: 0) { |
| 161 | TextField("", text: $text, prompt: Text(placeholder).foregroundColor(Palette.placeholder), axis: .vertical) |
| 162 | .font(Typo.body).foregroundStyle(Palette.controlText).textFieldStyle(.plain) |
| 163 | .lineLimit(1...6).focused($focused) |
| 164 | .padding(.leading, 14).padding(.vertical, 11) |
| 165 | .frame(minHeight: 44) |
| 166 | .accessibilityLabel(placeholder) |
| 167 | .onSubmit(send) |
| 168 | Button(action: send) { |
| 169 | Image(systemName: "arrow.up").font(.system(size: 16, weight: .bold)) |
| 170 | .foregroundStyle(has ? Color.white : Palette.faint) |
| 171 | .frame(width: 34, height: 34) |
| 172 | .background(has ? Palette.primaryFill : Palette.surface2, in: Circle()) |
| 173 | .frame(width: 44, height: 44).contentShape(Circle()) |
| 174 | } |
| 175 | .buttonStyle(.plain).disabled(!has) |
| 176 | .accessibilityLabel(Text(String(localized: "composer.send", bundle: .module))) |
| 177 | } |
| 178 | .background(Palette.surface2, in: shape) |
| 179 | .overlay(shape.strokeBorder(Palette.controlBorder, lineWidth: 1)) |
| 180 | .background { |
| 181 | if focused { |
| 182 | RoundedRectangle(cornerRadius: 13, style: .continuous) |
| 183 | .strokeBorder(Palette.accent, lineWidth: 2).padding(-3) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | .padding(.horizontal, 16).padding(.top, 10).padding(.bottom, 8) |
| 189 | .frame(maxWidth: 760).frame(maxWidth: .infinity) |
| 190 | .background(Palette.background) |
| 191 | #if os(iOS) |
| 192 | .task { |
| 193 | if attachments != nil { |
| 194 | screenshot.start() |
| 195 | while !Task.isCancelled { try? await Task.sleep(for: .seconds(30)); if !Task.isCancelled { screenshot.refresh() } } |
| 196 | } |
| 197 | } |
| 198 | // Leaving the conversation (a task link, another tab) gives up the |
| 199 | // keyboard; coming back never raises it until the field is tapped. |
| 200 | .onDisappear { screenshot.stop(); focused = false } |
| 201 | .onChange(of: scenePhase) { _, phase in if phase == .active && attachments != nil { screenshot.start() } else { screenshot.stop() } } |
| 202 | .onChange(of: attachments.map(ObjectIdentifier.init)) { _, _ in screenshot.stop(); screenshot = RecentScreenshot(); if attachments != nil { screenshot.start() } } |
| 203 | #endif |
| 204 | } |
| 205 | private func send() { |
| 206 | let value = text.trimmingCharacters(in: .whitespacesAndNewlines) |
| 207 | guard attachments?.canSend(text: value) ?? !value.isEmpty else { return } |
| 208 | onSend(value); text = "" |
| 209 | } |
| 210 | } |
| 211 | |