import AVFoundation import MobileCoreServices import Photos import SwiftUI struct SelectedMedia { let id: String } struct AttachmentMediaPickerView: View { @EnvironmentObject var store: AppStore @State private var selectedMedia = [SelectedMedia]() @State private var showCameraPicker = false var body: some View { let columns = Array(repeating: GridItem(.flexible(), spacing: 0), count: 3) VStack(spacing: 0) { // List of media ScrollView(showsIndicators: false) { LazyVGrid(columns: columns, spacing: 0) { // For camera if store.state.sharingState.isCameraAccessGranted { ZStack { CameraView() .aspectRatio(1, contentMode: .fit) .frame(maxWidth: .infinity) Image(systemName: "camera") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 40, height: 40) .foregroundColor(.white) .padding(8) .background(Color.black.opacity(0.5)) .clipShape(Circle()) .padding(8) } .onTapGesture { showCameraPicker = true } } else { Button { openAppSettings() } label: { ZStack { Rectangle() .fill(Color.Material.Background.light) .overlay { VStack { Image(systemName: "camera") .foregroundColor(.Material.Elements.active) .font(.system(size: 30)) Text("Allow camera access") .foregroundColor(.Material.Text.main) .font(.body3) } } .frame(height: 100) } } } // For gallery if store.state.sharingState.isGalleryAccessGranted { ForEach(store.state.sharingState.galleryItems) { item in GridViewItem(item: item) } } else { Button { openAppSettings() } label: { ZStack { Rectangle() .fill(Color.Material.Background.light) .overlay { VStack { Image(systemName: "photo") .foregroundColor(.Material.Elements.active) .font(.system(size: 30)) Text("Allow gallery access") .foregroundColor(.Material.Text.main) .font(.body3) } } .frame(height: 100) } } } } } .fullScreenCover(isPresented: $showCameraPicker) { CameraPicker(sourceType: .camera) { data, type in store.dispatch(.sharingAction(.cameraCaptured(media: data, type: type))) showCameraPicker = false store.dispatch(.sharingAction(.showSharing(false))) } .edgesIgnoringSafeArea(.all) } // Send panel Rectangle() .foregroundColor(.Material.Shape.black) .frame(maxWidth: .infinity) .frame(height: self.selectedMedia.isEmpty ? 0 : 50) .overlay { HStack { Text(L10n.Attachment.Send.media) .foregroundColor(.Material.Text.white) .font(.body1) Image(systemName: "arrow.up.circle") .foregroundColor(.Material.Text.white) .font(.body1) .padding(.leading, 8) } .padding() } .clipped() .onTapGesture { let ids = selectedMedia.map { $0.id } // sendGalleryMedia(ids: ids) store.dispatch(.sharingAction(.showSharing(false))) } } .onAppear { store.dispatch(.sharingAction(.checkCameraAccess)) store.dispatch(.sharingAction(.checkGalleryAccess)) // DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.2) { // checkCameraAccess() // checkGalleryAccess() // } } .onChange(of: store.state.sharingState.isGalleryAccessGranted) { granted in if granted { store.dispatch(.sharingAction(.fetchGallery)) } } } } private struct GridViewItem: View { let item: SharingGalleryItem var body: some View { if let data = item.thumbnail { ZStack { Image(uiImage: UIImage(data: data) ?? UIImage()) .resizable() .aspectRatio(contentMode: .fill) .frame(width: Const.galleryGridSize, height: Const.galleryGridSize) .clipped() if let duration = item.duration { VStack { Spacer() HStack { Spacer() Text(duration) .foregroundColor(.Material.Text.white) .font(.sub1) .shadow(color: .black, radius: 2) .padding(4) } } } // if selected { // VStack { // HStack { // Spacer() // Circle() // .frame(width: 30, height: 30) // .shadow(color: .black, radius: 2) // .foregroundColor(.Material.Shape.white) // .overlay { // Image(systemName: "checkmark") // .foregroundColor(.Material.Elements.active) // .font(.body3) // } // .padding(4) // } // Spacer() // } // } } .onTapGesture { // withAnimation { // selected.toggle() // if selected { // selectedMedia.append(SelectedMedia(id: id)) // } else { // selectedMedia.removeAll { $0.id == id } // } // } } } else { ZStack { Rectangle() .fill(Color.Material.Background.light) .overlay { ProgressView() } .frame(width: Const.galleryGridSize, height: Const.galleryGridSize) } } } } class CameraUIView: UIView { var previewLayer: AVCaptureVideoPreviewLayer? override func layoutSubviews() { super.layoutSubviews() previewLayer?.frame = bounds } } struct CameraView: UIViewRepresentable { func makeUIView(context _: Context) -> CameraUIView { let view = CameraUIView() let captureSession = AVCaptureSession() guard let captureDevice = AVCaptureDevice.default(for: .video) else { return view } guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return view } captureSession.addInput(input) let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer.videoGravity = .resizeAspectFill view.layer.addSublayer(previewLayer) view.previewLayer = previewLayer captureSession.startRunning() return view } func updateUIView(_ uiView: CameraUIView, context _: Context) { uiView.previewLayer?.frame = uiView.bounds } } struct CameraPicker: UIViewControllerRepresentable { var sourceType: UIImagePickerController.SourceType var completionHandler: (Data, SharingCameraMediaType) -> Void func makeUIViewController(context: Context) -> UIImagePickerController { let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = context.coordinator picker.mediaTypes = [UTType.movie.identifier, UTType.image.identifier] picker.videoQuality = .typeHigh picker.videoMaximumDuration = Const.videoDurationLimit picker.view.backgroundColor = .clear return picker } func updateUIViewController(_: UIImagePickerController, context _: Context) {} func makeCoordinator() -> Coordinator { Coordinator(self) } class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { let parent: CameraPicker init(_ parent: CameraPicker) { self.parent = parent } func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) { // swiftlint:disable:next force_cast let mediaType = info[.mediaType] as! String if mediaType == UTType.image.identifier { if let image = info[.originalImage] as? UIImage { let data = image.jpegData(compressionQuality: 1.0) ?? Data() parent.completionHandler(data, .photo) } } else if mediaType == UTType.movie.identifier { if let url = info[.mediaURL] as? URL { let data = try? Data(contentsOf: url) parent.completionHandler(data ?? Data(), .video) } } } } }