oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2import ImageIO
3import PDFKit
4
5/// Shared composer attachment list. State belongs to the session's draft.
6public struct AttachmentDraftStrip: View {
7 let draft: AttachmentDraft
8 public init(draft: AttachmentDraft) { self.draft = draft }
9 public var body: some View {
10 VStack(alignment: .leading, spacing: 6) {
11 if !draft.items.isEmpty {
12 ScrollView(.horizontal) {
13 HStack(alignment: .top, spacing: 12) {
14 ForEach(draft.items) { item in
15 VStack(alignment: .leading, spacing: 4) {
16 ZStack(alignment: .topTrailing) {
17 AttachmentPicture(data: item.file?.data, name: item.name)
18 .frame(width: 140, height: 86).background(Palette.surface2)
19 .clipShape(.rect(cornerRadius: 8))
20 HStack(spacing: 0) {
21 if item.failure != nil {
22 Button { Task { await draft.retry(item.id) } } label: {
23 Image(systemName: "arrow.clockwise")
24 .font(.system(size: 14, weight: .semibold))
25 .foregroundStyle(Palette.accent)
26 .padding(7).background(Palette.surface, in: Circle())
27 .frame(width: 44, height: 44).contentShape(Rectangle())
28 }
29 .buttonStyle(.plain)
30 .accessibilityLabel(EquipoStrings.text("read.retry"))
31 }
32 Button { draft.remove(item.id) } label: {
33 Image(systemName: "xmark.circle.fill").symbolRenderingMode(.palette)
34 .foregroundStyle(Palette.text, Palette.surface).frame(width: 44, height: 44).contentShape(Rectangle())
35 }
36 .buttonStyle(.plain)
37 .accessibilityLabel(String(format: EquipoStrings.text("chat.attach.remove"), item.name))
38 }
39 }
40 Text(item.name).font(Typo.caption).lineLimit(1).truncationMode(.middle)
41 switch item.state {
42 case .uploading(let progress):
43 ProgressView(value: progress).accessibilityLabel(EquipoStrings.text("chat.attach.uploading"))
44 Text(EquipoStrings.text("chat.attach.uploading")).font(Typo.caption).foregroundStyle(Palette.secondary)
45 case .failed(let words):
46 Text(words).font(Typo.caption).foregroundStyle(Palette.negative).fixedSize(horizontal: false, vertical: true)
47 case .ready: EmptyView()
48 }
49 }.frame(width: 140, alignment: .leading)
50 }
51 }.padding(.vertical, 4)
52 }
53 .accessibilityLabel(EquipoStrings.text("chat.attach.list"))
54 }
55 if let notice = draft.notice { Text(notice).font(Typo.caption).foregroundStyle(Palette.negative) }
56 }
57 }
58}
59
60struct AttachmentPicture: View {
61 let data: Data?
62 let name: String
63 @State private var image: CGImage?
64 var body: some View {
65 Group {
66 if let image {
67 Image(decorative: image, scale: 1).resizable().scaledToFit().accessibilityLabel(name)
68 } else {
69 Image(systemName: "doc").font(.title2).foregroundStyle(Palette.secondary).accessibilityLabel(name)
70 }
71 }.task(id: data) {
72 let bytes = data
73 let decoded = await Task.detached { bytes.flatMap { Self.thumbnail($0, side: 512) } }.value
74 guard !Task.isCancelled else { return }
75 image = decoded
76 }
77 }
78 nonisolated static func thumbnail(_ data: Data, side: Int) -> CGImage? {
79 guard let source = CGImageSourceCreateWithData(data as CFData, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil }
80 return CGImageSourceCreateThumbnailAtIndex(source, 0, [kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceCreateThumbnailWithTransform: true, kCGImageSourceThumbnailMaxPixelSize: side] as CFDictionary)
81 }
82}
83
84/// Sent thumbnails use authenticated API reads, never bare remote-image URLs.
85public struct MessageAttachments: View {
86 let attachments: [MessageAttachment]
87 let client: EquipoClient
88 @State private var selected: MessageAttachment?
89 public init(_ attachments: [MessageAttachment], client: EquipoClient) {
90 self.attachments = attachments; self.client = client
91 }
92 public var body: some View {
93 LazyVGrid(columns: [GridItem(.adaptive(minimum: 120, maximum: 180), alignment: .leading)], alignment: .leading, spacing: 8) {
94 ForEach(attachments) { file in
95 Button { selected = file } label: { SentAttachmentTile(file: file, client: client) }
96 .buttonStyle(.plain).accessibilityLabel(file.name)
97 }
98 }
99 #if os(iOS)
100 .fullScreenCover(item: $selected) { AttachmentPreview(file: $0, client: client) }
101 #else
102 .sheet(item: $selected) { AttachmentPreview(file: $0, client: client).frame(minWidth: 600, minHeight: 500) }
103 #endif
104 }
105}
106
107private struct SentAttachmentTile: View {
108 let file: MessageAttachment
109 let client: EquipoClient
110 @State private var data: Data?
111 var body: some View {
112 VStack(alignment: .leading, spacing: 4) {
113 AttachmentPicture(data: data, name: file.name).frame(height: 100).frame(maxWidth: .infinity)
114 .background(Palette.surface2).clipShape(.rect(cornerRadius: 8))
115 Text(file.name).font(Typo.caption).lineLimit(1).truncationMode(.middle).frame(maxWidth: .infinity, alignment: .leading)
116 }
117 .task(id: file.id) {
118 if file.kind == "image" { data = try? await client.attachmentData(file.id, preview: true) }
119 }
120 }
121}
122
123private struct AttachmentPreview: View {
124 let file: MessageAttachment
125 let client: EquipoClient
126 @Environment(\.dismiss) private var dismiss
127 @State private var data: Data?
128 @State private var failure = false
129 @State private var image: CGImage?
130 var body: some View {
131 NavigationStack {
132 Group {
133 if let data {
134 if let image {
135 ScrollView([.horizontal, .vertical]) {
136 Image(decorative: image, scale: 1).resizable().scaledToFit()
137 .containerRelativeFrame(.horizontal).accessibilityLabel(file.name)
138 }
139 } else if file.mediaType == "application/pdf" { AttachmentPDF(data: data) }
140 else { ScrollView { Text(String(decoding: data, as: UTF8.self)).font(.system(.body, design: .monospaced)).textSelection(.enabled).padding() } }
141 } else if failure {
142 VStack {
143 Text(EquipoStrings.text("read.failed_title"))
144 Button(EquipoStrings.text("read.retry")) { Task { await load() } }
145 }
146 } else { ProgressView().accessibilityLabel(file.name) }
147 }
148 .frame(maxWidth: .infinity, maxHeight: .infinity)
149 .background(Palette.background).navigationTitle(file.name)
150 .toolbar { ToolbarItem(placement: .confirmationAction) { Button(EquipoStrings.text("team.close")) { dismiss() } } }
151 }.task { await load() }
152 }
153 private func load() async {
154 failure = false
155 do {
156 let bytes = try await client.attachmentData(file.id)
157 let decoded = await Task.detached { AttachmentPicture.thumbnail(bytes, side: 4096) }.value
158 guard !Task.isCancelled else { return }
159 image = decoded; data = bytes
160 }
161 catch { failure = true }
162 }
163}
164
165#if os(iOS)
166private struct AttachmentPDF: UIViewRepresentable {
167 let data: Data
168 func makeUIView(context: Context) -> PDFView { let view = PDFView(); view.autoScales = true; view.document = PDFDocument(data: data); return view }
169 func updateUIView(_ view: PDFView, context: Context) {}
170}
171#else
172private struct AttachmentPDF: NSViewRepresentable {
173 let data: Data
174 func makeNSView(context: Context) -> PDFView { let view = PDFView(); view.autoScales = true; view.document = PDFDocument(data: data); return view }
175 func updateNSView(_ view: PDFView, context: Context) {}
176}
177#endif
178