| 1 | #if os(iOS) |
| 2 | import SwiftUI |
| 3 | import PhotosUI |
| 4 | import UniformTypeIdentifiers |
| 5 | |
| 6 | struct PhoneAttachmentPicker: View { |
| 7 | let draft: AttachmentDraft |
| 8 | var screenshot: RecentScreenshot? = nil |
| 9 | @State private var explanation = false |
| 10 | @State private var limited = false |
| 11 | @State private var photos: [PhotosPickerItem] = [] |
| 12 | @State private var choosingPhotos = false |
| 13 | @State private var files = false |
| 14 | @State private var camera = false |
| 15 | var body: some View { |
| 16 | Group { |
| 17 | if let screenshot, !screenshot.policy.explained { |
| 18 | Button { screenshot.explain(); explanation = true } label: { plus } |
| 19 | } else { sources } |
| 20 | } |
| 21 | .sheet(isPresented: $explanation, onDismiss: { screenshot?.decline() }) { |
| 22 | ScreenshotPermission(choose: { explanation = false; Task { await screenshot?.requestPermission() } }, decline: { screenshot?.decline(); explanation = false }) |
| 23 | } |
| 24 | .sheet(isPresented: $limited) { LimitedScreenshotPicker { limited = false; screenshot?.refresh() } } |
| 25 | |
| 26 | .buttonStyle(.plain) |
| 27 | .accessibilityLabel(EquipoStrings.text("chat.attach.add")) |
| 28 | .photosPicker(isPresented: $choosingPhotos, selection: $photos, maxSelectionCount: max(1, AttachmentLimits.maxCount - draft.items.count), matching: .images) |
| 29 | .fileImporter(isPresented: $files, allowedContentTypes: [.item], allowsMultipleSelection: true) { result in |
| 30 | if case .success(let urls) = result { for url in urls { Task { await draft.add(url: url) } } } |
| 31 | } |
| 32 | .fullScreenCover(isPresented: $camera) { |
| 33 | CameraAttachment { data in |
| 34 | camera = false |
| 35 | if let data { Task { await draft.add(data: data, name: "photo.jpg") } } |
| 36 | }.ignoresSafeArea() |
| 37 | } |
| 38 | .onChange(of: photos) { _, selected in |
| 39 | photos = [] |
| 40 | for photo in selected { |
| 41 | Task { |
| 42 | await draft.add(name: "photo") { |
| 43 | guard let data = try await photo.loadTransferable(type: Data.self) else { throw AttachmentFailure.type } |
| 44 | return data |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | private var plus: some View { |
| 51 | |
| 52 | Image(systemName: "plus").font(.system(size: 16, weight: .semibold)) |
| 53 | .foregroundStyle(Palette.secondary) |
| 54 | .frame(width: 34, height: 34).background(Palette.surface2, in: Circle()) |
| 55 | .frame(width: 44, height: 44).contentShape(Circle()) |
| 56 | } |
| 57 | private var sources: some View { |
| 58 | Menu { |
| 59 | // COPY APPROVED: Luna, 2026-11-25, all native.attach.* source labels. |
| 60 | Button { choosingPhotos = true } label: { |
| 61 | Label(EquipoStrings.text("native.attach.photos"), systemImage: "photo") |
| 62 | } |
| 63 | if UIImagePickerController.isSourceTypeAvailable(.camera) { |
| 64 | Button { camera = true } label: { Label(EquipoStrings.text("native.attach.camera"), systemImage: "camera") } |
| 65 | } |
| 66 | Button { files = true } label: { Label(EquipoStrings.text("native.attach.files"), systemImage: "folder") } |
| 67 | Button { paste() } label: { Label(EquipoStrings.text("native.attach.paste"), systemImage: "document.on.clipboard") } |
| 68 | if let screenshot { |
| 69 | if screenshot.permission == .denied || screenshot.permission == .restricted { |
| 70 | Button(EquipoStrings.text("native.screenshot.settings")) { |
| 71 | if let url = URL(string: UIApplication.openSettingsURLString) { UIApplication.shared.open(url) } |
| 72 | } |
| 73 | } else if screenshot.permission == .limited || screenshot.permission == .notDetermined { |
| 74 | Button(EquipoStrings.text("native.screenshot.choose")) { |
| 75 | if screenshot.permission == .limited { limited = true } |
| 76 | else { Task { await screenshot.requestPermission() } } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } label: { plus } |
| 81 | } |
| 82 | private func paste() { |
| 83 | let pasteboard = UIPasteboard.general |
| 84 | for type in [UTType.png, .jpeg, .heic, .heif, .gif, .webP, .tiff] { |
| 85 | if let data = pasteboard.data(forPasteboardType: type.identifier) { |
| 86 | Task { await draft.add(data: data, name: "image." + (type.preferredFilenameExtension ?? "png")) }; return |
| 87 | } |
| 88 | } |
| 89 | if let urls = pasteboard.urls { for url in urls where url.isFileURL { Task { await draft.add(url: url) } } } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private struct CameraAttachment: UIViewControllerRepresentable { |
| 94 | let completed: (Data?) -> Void |
| 95 | func makeCoordinator() -> Coordinator { Coordinator(completed: completed) } |
| 96 | func makeUIViewController(context: Context) -> UIImagePickerController { |
| 97 | let picker = UIImagePickerController(); picker.sourceType = .camera; picker.delegate = context.coordinator; return picker |
| 98 | } |
| 99 | func updateUIViewController(_ view: UIImagePickerController, context: Context) {} |
| 100 | final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate { |
| 101 | let completed: (Data?) -> Void |
| 102 | init(completed: @escaping (Data?) -> Void) { self.completed = completed } |
| 103 | func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { completed(nil) } |
| 104 | func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { |
| 105 | completed((info[.originalImage] as? UIImage)?.jpegData(compressionQuality: 0.9)) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | #endif |
| 110 | |