Branchwell

OpenTalon / app / native / CompanionCore / Sources / CompanionCore / TaskActivity.swift

1import SwiftUI
2
3public enum TaskRevisionRequestCopy {
4 public static let heading = "Revision requests"
5 public static let field = "Revision request"
6 public static let add = "Add revision request"
7 public static let send = "Send revision request"
8 public static let example = "For example, add a retry test and report what passed."
9}
10
11public struct TaskActivityImage: Codable, Equatable, Identifiable, Sendable {
12 public let id: String
13 public let name: String
14 public let mimeType: String
15}
16
17public struct TaskActivityEntry: Codable, Equatable, Identifiable, Sendable {
18 public let id: String
19 public let sequence: Int
20 public let createdAt: String
21 public let actor: String
22 public let kind: String
23 public let summary: String
24 public let detail: String
25 public let images: [TaskActivityImage]
26
27 public var symbol: String {
28 switch kind {
29 case "agent": "sparkles"
30 case "comment": "bubble.left"
31 case "legacy": "clock.arrow.circlepath"
32 case "restore": "arrow.uturn.backward"
33 default: "arrow.left.arrow.right"
34 }
35 }
36 public var dateLabel: String {
37 TaskCreationOrder.date(from: createdAt)?.formatted(date: .abbreviated, time: .shortened) ?? createdAt
38 }
39}
40
41public struct TaskActivityPage: Codable, Equatable, Sendable {
42 public let taskID: String
43 public let revision: Int
44 public let entries: [TaskActivityEntry]
45 public let nextBefore: Int?
46 public var commentID: String? = nil
47
48 public func merging(_ older: Self) -> Self {
49 guard taskID == older.taskID, commentID == older.commentID else { return self }
50 let values = Dictionary((entries + older.entries).map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first })
51 return Self(taskID: taskID, revision: max(revision, older.revision),
52 entries: values.values.sorted { $0.sequence > $1.sequence }, nextBefore: older.nextBefore, commentID: commentID)
53 }
54
55 public func keepingOlder(from cached: Self) -> Self {
56 guard taskID == cached.taskID, commentID == cached.commentID, nextBefore != nil else { return self }
57 let known = Set(cached.entries.map(\.id))
58 if entries.contains(where: { $0.kind == "restore" && !known.contains($0.id) }) { return self }
59 guard entries.contains(where: { known.contains($0.id) }) ||
60 cached.entries.first?.sequence == (entries.last?.sequence ?? 0) - 1 else { return self }
61 return merging(cached)
62 }
63}
64
65public struct TaskActivitySummary: View {
66 let page: TaskActivityPage
67 let colors: OpenTalonColors
68 let open: () -> Void
69 public init(page: TaskActivityPage, colors: OpenTalonColors, open: @escaping () -> Void) {
70 self.page = page; self.colors = colors; self.open = open
71 }
72 public var body: some View {
73 Button("View activity", systemImage: "clock.arrow.circlepath", action: open)
74 .buttonStyle(ActivityPillStyle(colors: colors))
75 .accessibilityIdentifier("taskActivity")
76 }
77}
78
79public struct TaskActivityView: View {
80 @Environment(\.dismiss) private var dismiss
81 @State private var expanded = Set<String>()
82 @State private var failure: String?
83 @State private var loading = false
84 let commentID: String?
85 let page: TaskActivityPage?
86 let colors: OpenTalonColors
87 let waitingForMac: Bool
88 let load: @MainActor (Int?) async throws -> Void
89 let openImage: ((TaskActivityImage) -> Void)?
90
91 public init(page: TaskActivityPage?, colors: OpenTalonColors, waitingForMac: Bool = false, commentID: String? = nil,
92 load: @escaping @MainActor (Int?) async throws -> Void,
93 openImage: ((TaskActivityImage) -> Void)? = nil) {
94 self.page = page; self.colors = colors; self.waitingForMac = waitingForMac; self.commentID = commentID
95 self.load = load; self.openImage = openImage
96 }
97 public var body: some View {
98 VStack(spacing: 0) {
99 HStack {
100 Text(commentID == nil ? "Activity" : "Revision request activity").font(.headline)
101 Spacer()
102 Button("Close") { dismiss() }.accessibilityIdentifier("activityClose")
103 }.padding(20)
104 ScrollView {
105 LazyVStack(alignment: .leading, spacing: 16) {
106 Text(commentID == nil ? "Task changes and agent updates." : "The agent’s response and recorded changes for your revision request.")
107 .font(.callout).foregroundStyle(colors.muted)
108 if let failure { Text(failure).foregroundStyle(colors.danger) }
109 if waitingForMac {
110 Label("Waiting for your Mac to load activity…", systemImage: "clock")
111 .font(.footnote).foregroundStyle(colors.muted)
112 }
113 if let page {
114 ForEach(page.entries) { entry in activityRow(entry) }
115 if page.entries.isEmpty { Text("No activity yet.").foregroundStyle(colors.muted) }
116 if let before = page.nextBefore {
117 Button("Load older activity") { fetch(before) }
118 .disabled(loading || waitingForMac).accessibilityIdentifier("activityLoadOlder")
119 }
120 } else { ProgressView("Loading activity…") }
121 }.padding(20)
122 }
123 }
124 .foregroundStyle(colors.text).background(colors.canvas)
125 .buttonStyle(ActivityPillStyle(colors: colors)).tint(colors.actionAccent)
126 .task { fetch(nil) }
127 .task(id: page?.entries.first?.id) {
128 if commentID != nil, let report = page?.entries.first(where: { $0.kind == "agent" }) { expanded.insert(report.id) }
129 }
130 }
131 private func activityRow(_ entry: TaskActivityEntry) -> some View {
132 VStack(alignment: .leading, spacing: 12) {
133 HStack(alignment: .top, spacing: 12) {
134 Image(systemName: entry.symbol).foregroundStyle(colors.actionAccent).frame(width: 22)
135 VStack(alignment: .leading, spacing: 5) {
136 Text(entry.summary).font(.headline)
137 Text(entry.actor + " · " + entry.dateLabel).font(.caption).foregroundStyle(colors.muted)
138 }
139 }
140 if !entry.detail.isEmpty {
141 if commentID != nil && entry.kind == "change" {
142 Text("Changes").font(.caption.weight(.semibold)).foregroundStyle(colors.muted)
143 Text(entry.detail).font(.callout).textSelection(.enabled)
144 .fixedSize(horizontal: false, vertical: true)
145 } else {
146 DisclosureGroup("Details", isExpanded: Binding(get: { expanded.contains(entry.id) }, set: { value in
147 if value { expanded.insert(entry.id) } else { expanded.remove(entry.id) }
148 })) {
149 Text(entry.detail).font(.callout).textSelection(.enabled)
150 .fixedSize(horizontal: false, vertical: true)
151 .frame(maxWidth: .infinity, alignment: .leading).padding(.top, 8)
152 }
153 .accessibilityIdentifier("activityDetails-" + entry.id)
154 }
155 }
156 ForEach(entry.images) { image in
157 if let openImage {
158 Button(image.name, systemImage: "photo") { openImage(image) }
159 } else { Label(image.name + " · Saved on your Mac", systemImage: "photo").font(.footnote) }
160 }
161 }
162 .padding(16).frame(maxWidth: .infinity, alignment: .leading)
163 .background(colors.card, in: RoundedRectangle(cornerRadius: 18))
164 .accessibilityIdentifier("activityEntry-" + entry.id)
165 }
166 private func fetch(_ before: Int?) {
167 guard !loading else { return }; loading = true; failure = nil
168 Task { @MainActor in
169 defer { loading = false }
170 do { try await load(before) } catch { failure = error.localizedDescription }
171 }
172 }
173}
174
175private struct ActivityPillStyle: ButtonStyle {
176 let colors: OpenTalonColors
177 func makeBody(configuration: Configuration) -> some View {
178 configuration.label.font(.callout.weight(.semibold))
179 .padding(.horizontal, 16).padding(.vertical, 10)
180 .background(colors.card.opacity(configuration.isPressed ? 0.65 : 1), in: Capsule())
181 .overlay { Capsule().strokeBorder(colors.line, lineWidth: 1) }
182 }
183}
184
185public struct EarlierTaskActivityView: View {
186 @Environment(\.dismiss) private var dismiss
187 let comments: [PhoneComment]
188 let colors: OpenTalonColors
189 public init(comments: [PhoneComment], colors: OpenTalonColors) { self.comments=comments; self.colors=colors }
190 public var body: some View {
191 VStack(spacing: 0) {
192 HStack {
193 Text("Earlier activity").font(.headline)
194 Spacer()
195 Button("Close") { dismiss() }.accessibilityIdentifier("earlierActivityClose")
196 }.padding(20)
197 ScrollView {
198 LazyVStack(alignment: .leading, spacing: 16) {
199 Text("Messages from an older Mac are kept here. Update OpenTalon on your Mac to link new agent responses to your revision requests.")
200 .font(.callout).foregroundStyle(colors.muted)
201 ForEach(comments.reversed()) { entry in
202 DisclosureGroup {
203 Text(entry.text).textSelection(.enabled).fixedSize(horizontal: false, vertical: true)
204 .padding(.top, 8)
205 } label: {
206 VStack(alignment: .leading, spacing: 4) {
207 Text("Earlier message").font(.headline)
208 Text(TaskCreationOrder.date(from: entry.createdAt)?.formatted(date: .abbreviated, time: .shortened) ?? entry.createdAt)
209 .font(.caption).foregroundStyle(colors.muted)
210 }
211 }
212 .padding(16).background(colors.card, in: RoundedRectangle(cornerRadius: 18))
213 }
214 }.padding(20)
215 }
216 }
217 .background(colors.canvas).foregroundStyle(colors.text).tint(colors.actionAccent)
218 .buttonStyle(ActivityPillStyle(colors: colors))
219 }
220}