| 1 | import SwiftUI |
| 2 | import ImageIO |
| 3 | import EquipoShareCore |
| 4 | |
| 5 | public enum ShareWords { |
| 6 | public static func text(_ key: String) -> String { String(localized: String.LocalizationValue(key), bundle: .module) } |
| 7 | public static func uploadError(_ error: ShareFailure) -> String { |
| 8 | if case .server(_, let code) = error, ["attachment_type", "attachment_too_large", "attachment_pixels", "attachment_limit"].contains(code) { return text("api." + code) } |
| 9 | return text("chat.attach.failed") |
| 10 | } |
| 11 | public static func error(_ error: ShareFailure) -> String { |
| 12 | switch error { |
| 13 | case .signedOut: text("native.share.signed_out") |
| 14 | case .readFailed: text("card.offline") |
| 15 | case .malformed: text("read.failed") |
| 16 | case .expiredJournal: text("native.share.check_conversation") |
| 17 | case .server(_, let code): |
| 18 | // Only reviewed server keys. Never show a server-supplied sentence. |
| 19 | if ["attachment_type", "attachment_too_large", "attachment_pixels", "attachment_limit"].contains(code) { text("api." + code) } |
| 20 | else if code == "project_not_allowed" { text("projects.selection_lost") } |
| 21 | else { text("card.failed") } |
| 22 | default: text("native.share.unconfirmed") |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | public struct ShareComposeView: View { |
| 27 | @Bindable var sender: ShareSender |
| 28 | let preparing: Bool |
| 29 | let notice: String? |
| 30 | let retryInput: (() -> Void)? |
| 31 | let removeInput: (() -> Void)? |
| 32 | let cancel: () -> Void |
| 33 | let send: () -> Void |
| 34 | public init(sender: ShareSender, preparing: Bool = false, notice: String? = nil, retryInput: (() -> Void)? = nil, removeInput: (() -> Void)? = nil, cancel: @escaping () -> Void, send: @escaping () -> Void) { |
| 35 | self.sender = sender; self.preparing = preparing; self.notice = notice; self.cancel = cancel; self.send = send; self.retryInput = retryInput; self.removeInput = removeInput |
| 36 | } |
| 37 | public var body: some View { |
| 38 | NavigationStack { |
| 39 | ScrollView { |
| 40 | VStack(alignment: .leading, spacing: 16) { |
| 41 | if let selection = sender.selection { |
| 42 | Text(selection.team).font(.headline) |
| 43 | Text(selection.person).font(.caption).foregroundStyle(.secondary) |
| 44 | ForEach(sender.earlierMessages, id: \.key) { write in |
| 45 | VStack(alignment: .leading, spacing: 8) { |
| 46 | let pending = sender.earlierPending.contains(where: { $0.key == write.key }) |
| 47 | if pending { Text(ShareWords.text("native.share.earlier_unconfirmed")) } |
| 48 | if let summary = write.summary { |
| 49 | Text(verbatim: summary.text).lineLimit(2) |
| 50 | LabeledContent(ShareWords.text("chat.attach.list")) { |
| 51 | Text(summary.attachmentCount, format: .number) |
| 52 | }.font(.caption) |
| 53 | } |
| 54 | if let error = sender.earlierErrors[write.key] { |
| 55 | Text(ShareWords.error(error)).foregroundStyle(.secondary) |
| 56 | } |
| 57 | if pending { |
| 58 | HStack { |
| 59 | Button(ShareWords.text("work.pause.try_again")) { sender.startEarlier(write.key) } |
| 60 | Button(ShareWords.text("settings.discard")) { sender.discardEarlier(write.key) } |
| 61 | }.disabled(sender.earlierBusy.contains(write.key)) |
| 62 | } else { |
| 63 | Button(ShareWords.text("chat.connections.dismiss")) { sender.dismissEarlierRefusal(write.key) } |
| 64 | } |
| 65 | }.accessibilityIdentifier("share-earlier") |
| 66 | } |
| 67 | Picker(ShareWords.text("nav.projects"), selection: $sender.project) { |
| 68 | Text(ShareWords.text("native.share.automatic")).tag(nil as String?) |
| 69 | ForEach(sender.projects) { Text($0.displayName).tag(Optional($0.id)) } |
| 70 | }.disabled(!sender.canEdit) |
| 71 | if sender.pending == nil { |
| 72 | TextField(ShareWords.text("conversation.placeholder"), text: $sender.text, axis: .vertical) |
| 73 | .lineLimit(3...10).padding(12).background(.quaternary, in: RoundedRectangle(cornerRadius: 12)) |
| 74 | .disabled(!sender.canEdit).accessibilityIdentifier("share-message") |
| 75 | } else { |
| 76 | if !sender.text.isEmpty { Text(sender.text).textSelection(.enabled) } |
| 77 | Text(ShareWords.text("native.share.unconfirmed")) |
| 78 | } |
| 79 | ForEach(sender.files) { file in |
| 80 | HStack { |
| 81 | ShareThumbnail(file: file) |
| 82 | VStack(alignment: .leading) { |
| 83 | Text(file.name).lineLimit(2) |
| 84 | if sender.failedFile == file.id, let error = sender.error { Text(ShareWords.uploadError(error)).font(.caption).foregroundStyle(.secondary) } |
| 85 | if sender.failedFile != file.id, let progress = sender.progress[file.id] { ProgressView(value: progress).accessibilityLabel(ShareWords.text("chat.attach.uploading")) } |
| 86 | } |
| 87 | Spacer() |
| 88 | if sender.failedFile == file.id { |
| 89 | Button(action: send) { Image(systemName: "arrow.clockwise").frame(width: 44, height: 44) } |
| 90 | .disabled(!sender.canSend).accessibilityLabel(ShareWords.text("read.retry")) |
| 91 | } |
| 92 | Button { sender.remove(file.id) } label: { Image(systemName: "xmark").frame(width: 44, height: 44) } |
| 93 | .disabled(!sender.canEdit).accessibilityLabel(String(format: ShareWords.text("chat.attach.remove"), file.name)) |
| 94 | } |
| 95 | } |
| 96 | if preparing { ProgressView().accessibilityLabel(ShareWords.text("chat.attach.uploading")) } |
| 97 | } |
| 98 | if sender.failedFile == nil, let error = sender.error { Text(ShareWords.error(error)).foregroundStyle(.secondary).accessibilityIdentifier("share-error") } |
| 99 | if let notice { |
| 100 | HStack { |
| 101 | Text(notice).foregroundStyle(.secondary) |
| 102 | if let retryInput { Button(action: retryInput) { Image(systemName: "arrow.clockwise").frame(width: 44, height: 44) }.accessibilityLabel(ShareWords.text("read.retry")) } |
| 103 | if let removeInput { Button(action: removeInput) { Image(systemName: "xmark").frame(width: 44, height: 44) }.accessibilityLabel(ShareWords.text("chat.connections.dismiss")) } |
| 104 | } |
| 105 | } |
| 106 | }.padding() |
| 107 | } |
| 108 | .scrollDismissesKeyboard(.interactively) |
| 109 | .navigationTitle(ShareWords.text("native.share.title")) |
| 110 | .toolbar { |
| 111 | ToolbarItem(placement: .cancellationAction) { Button(ShareWords.text("pairing.cancel"), action: cancel) } |
| 112 | ToolbarItem(placement: .confirmationAction) { |
| 113 | Button(sender.pending == nil ? ShareWords.text("composer.send") : ShareWords.text("read.retry"), action: send) |
| 114 | .disabled(!sender.canSend || preparing || notice != nil).accessibilityIdentifier("share-send") |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | private struct ShareThumbnail: View { |
| 121 | let file: FileAttachment |
| 122 | @State private var image: CGImage? |
| 123 | var body: some View { |
| 124 | Group { |
| 125 | if let image { Image(decorative: image, scale: 1).resizable().scaledToFit() } |
| 126 | else { Image(systemName: "doc") } |
| 127 | }.frame(width: 64, height: 64) |
| 128 | .task(id: file.id) { |
| 129 | image = await Task.detached { |
| 130 | guard let source = CGImageSourceCreateWithURL(file.url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil as CGImage? } |
| 131 | return CGImageSourceCreateThumbnailAtIndex(source, 0, [kCGImageSourceCreateThumbnailFromImageAlways: true, kCGImageSourceCreateThumbnailWithTransform: true, kCGImageSourceThumbnailMaxPixelSize: 128] as CFDictionary) |
| 132 | }.value |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |