import AVFoundation import Photos import SwiftUI struct SelectedMedia { let path: String } struct AttachmentMediaPickerView: View { @State private var isCameraAccessGranted = AVCaptureDevice.authorizationStatus(for: .video) == .authorized @State private var isGalleryAccessGranted = PHPhotoLibrary.authorizationStatus() == .authorized @State private var thumbnails = [ThumbnailView]() @State private var selectedMedia = [SelectedMedia]() let gridSize = UIScreen.main.bounds.width / 3 var body: some View { let columns = Array(repeating: GridItem(.flexible(), spacing: 0), count: 3) ScrollView(showsIndicators: false) { LazyVGrid(columns: columns, spacing: 0) { // For camera if 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) } } 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 pictures if isGalleryAccessGranted { ForEach(thumbnails) { photo in photo } } 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) } } } } } .onAppear { DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.2) { checkCameraAccess() checkGalleryAccess() } } } private func checkCameraAccess() { let status = AVCaptureDevice.authorizationStatus(for: .video) switch status { case .authorized: isCameraAccessGranted = true case .notDetermined: AVCaptureDevice.requestAccess(for: .video) { granted in DispatchQueue.main.async { self.isCameraAccessGranted = granted } } case .denied, .restricted: isCameraAccessGranted = false @unknown default: isCameraAccessGranted = false } } private func checkGalleryAccess() { let status = PHPhotoLibrary.authorizationStatus() switch status { case .authorized, .limited: isGalleryAccessGranted = true fetchImages() case .notDetermined: PHPhotoLibrary.requestAuthorization { status in DispatchQueue.main.async { self.isGalleryAccessGranted = status == .authorized if self.isGalleryAccessGranted { self.fetchImages() } } } case .denied, .restricted: isGalleryAccessGranted = false @unknown default: isGalleryAccessGranted = false } } private func fetchImages() { let fetchOptions = PHFetchOptions() fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] let assets = PHAsset.fetchAssets(with: fetchOptions) let manager = PHImageManager.default() let option = PHImageRequestOptions() option.isSynchronous = true assets.enumerateObjects { asset, _, _ in if asset.mediaType == .image { manager.requestImage( for: asset, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFill, options: option ) { image, _ in image?.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in if let image { DispatchQueue.main.async { self.thumbnails.append(ThumbnailView(image: image, gridSize: gridSize, selected: $selectedMedia)) } } }) } } else if asset.mediaType == .video { manager.requestAVAsset(forVideo: asset, options: nil) { avAsset, _, _ in if let avAsset { let imageGenerator = AVAssetImageGenerator(asset: avAsset) imageGenerator.appliesPreferredTrackTransform = true let time = CMTimeMake(value: 1, timescale: 2) do { let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil) let thumbnail = UIImage(cgImage: imageRef) thumbnail.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in if let image { DispatchQueue.main.async { self.thumbnails.append(ThumbnailView(image: image, gridSize: gridSize, selected: $selectedMedia, duration: asset.duration.minAndSec)) } } }) } catch { print("Failed to create thumbnail image") } } } } } } func openAppSettings() { if let appSettingsUrl = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(appSettingsUrl) { UIApplication.shared.open(appSettingsUrl, completionHandler: nil) } } } private struct ThumbnailView: Identifiable, View { let id = UUID() let gridSize: CGFloat let duration: String? @State private var image: UIImage @State private var ready = false @State private var selected = false @Binding var selectedMedia: [SelectedMedia] init(image: UIImage, gridSize: CGFloat, selected: Binding<[SelectedMedia]>, duration: String? = nil) { self.image = image self.gridSize = gridSize _selectedMedia = selected self.duration = duration } var body: some View { if ready { ZStack { Image(uiImage: image) .resizable() .aspectRatio(contentMode: .fill) .frame(width: gridSize, height: gridSize) .clipped() if let 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) // Circle() // .fill(Color.Material.Shape.white) // .frame(width: 30, height: 30) // .shadow(color: .black, radius: 2) .overlay { Image(systemName: "checkmark") .foregroundColor(.Material.Elements.active) .font(.body3) } .padding(4) } Spacer() } } } .onTapGesture { withAnimation { selected.toggle() if selected { selectedMedia.append(SelectedMedia(path: id.uuidString)) } else { selectedMedia.removeAll { $0.path == id.uuidString } } } } } else { ZStack { Rectangle() .fill(Color.Material.Background.light) .overlay { ProgressView() } .frame(width: gridSize, height: gridSize) } .onAppear { image.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in if let image { self.image = image ready = true } }) } } } } 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 } }