From 024c9d85c8cbb727cd42c5109c64e6ab7fea4db8 Mon Sep 17 00:00:00 2001 From: fmodf Date: Wed, 3 Jul 2024 10:53:39 +0200 Subject: [PATCH] wip --- .../Attachments/AttachmentMediaManager.swift | 83 +++++++++++ .../AttachmentMediaPickerView.swift | 134 ++++++------------ 2 files changed, 125 insertions(+), 92 deletions(-) create mode 100644 ConversationsClassic/View/Screens/Attachments/AttachmentMediaManager.swift diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentMediaManager.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaManager.swift new file mode 100644 index 0000000..c5a9a8d --- /dev/null +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaManager.swift @@ -0,0 +1,83 @@ +import AVFoundation +import Photos +import SwiftUI +import UIKit + +class MediaManager: NSObject, ObservableObject { + @Published var photos: [UIImage] = [] + @Published var cameraFeed: UIImage? + + override init() { + super.init() + DispatchQueue.global(qos: .userInitiated).async { [weak self] in + self?.fetchPhotos() + self?.setupCameraFeed() + } + } + + private func fetchPhotos() { + let fetchOptions = PHFetchOptions() + fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] + let assets = PHAsset.fetchAssets(with: .image, options: fetchOptions) + + let manager = PHImageManager.default() + let option = PHImageRequestOptions() + option.isSynchronous = true + + assets.enumerateObjects { asset, _, _ in + manager.requestImage(for: asset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: option) { image, _ in + if let image = image { + DispatchQueue.main.async { + self.photos.append(image) + } + } + } + } + } + + private func setupCameraFeed() { + let captureSession = AVCaptureSession() + captureSession.sessionPreset = .medium + + guard let captureDevice = AVCaptureDevice.default(for: .video) else { + return + } + + let deviceInput: AVCaptureDeviceInput + do { + deviceInput = try AVCaptureDeviceInput(device: captureDevice) + } catch { + return + } + + if captureSession.canAddInput(deviceInput) { + captureSession.addInput(deviceInput) + } + + let videoOutput = AVCaptureVideoDataOutput() + videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue")) + if captureSession.canAddOutput(videoOutput) { + captureSession.addOutput(videoOutput) + } + + captureSession.startRunning() + } +} + +extension MediaManager: AVCaptureVideoDataOutputSampleBufferDelegate { + func captureOutput(_: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from _: AVCaptureConnection) { + guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { + return + } + + let ciImage = CIImage(cvPixelBuffer: pixelBuffer) + let context = CIContext() + guard let cgImage = context.createCGImage(ciImage, from: ciImage.extent) else { + return + } + + DispatchQueue.main.async { + self.cameraFeed = UIImage(cgImage: cgImage) + } + } +} diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift index 572d170..45619b5 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift @@ -1,107 +1,57 @@ -import AVFoundation -import Photos import SwiftUI -import UIKit - -class MediaManager: ObservableObject { - @Published var photos: [UIImage] = [] - @Published var cameraFeed: UIImage? - - init() { - fetchPhotos() - setupCameraFeed() - } - - private func fetchPhotos() { - let fetchOptions = PHFetchOptions() - fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)] - let assets = PHAsset.fetchAssets(with: .image, options: fetchOptions) - - let manager = PHImageManager.default() - let option = PHImageRequestOptions() - option.isSynchronous = true - - assets.enumerateObjects { asset, _, _ in - manager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: option) { image, _ in - if let image = image { - DispatchQueue.main.async { - self.photos.append(image) - } - } - } - } - } - - private func setupCameraFeed() { - // Setup the camera feed and store the feed in the `cameraFeed` property - } -} struct AttachmentMediaPickerView: View { - @StateObject private var mediaManager = MediaManager() - @State private var isPickerPresented = false - @State private var selectedPhoto: UIImage? + // @StateObject private var mediaManager = MediaManager() + + var hasCam: Bool = false + + let elements = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"] var body: some View { ScrollView { - LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) { - if let cameraFeed = mediaManager.cameraFeed { - Button(action: { - isPickerPresented = true - }) { - Image(uiImage: cameraFeed) - .resizable() - .aspectRatio(contentMode: .fill) - } - } - - ForEach(mediaManager.photos, id: \.self) { photo in - Button(action: { - selectedPhoto = photo - }) { - Image(uiImage: photo) - .resizable() - .aspectRatio(contentMode: .fill) + LazyVGrid(columns: Array(repeating: .init(.flexible()), count: 3)) { + ForEach(0 ..< 10) { index in + if index == 0 { + VStack { + Color.red + .frame(height: 100) + Color.red + .frame(height: 100) + } + } else { + Color.blue + .frame(height: 100) } } } - } - .sheet(isPresented: $isPickerPresented) { - ImagePicker(sourceType: .camera) + .padding(.horizontal) + // LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) { + // if let cameraFeed = mediaManager.cameraFeed { + // Button(action: { + // isPickerPresented = true + // }) { + // Image(uiImage: cameraFeed) + // .resizable() + // .aspectRatio(contentMode: .fill) + // } + // } + // + // ForEach(mediaManager.photos, id: \.self) { photo in + // Button(action: { + // selectedPhoto = photo + // }) { + // Image(uiImage: photo) + // .resizable() + // .aspectRatio(contentMode: .fill) + // } + // } + // } } } } -struct ImagePicker: UIViewControllerRepresentable { - var sourceType: UIImagePickerController.SourceType - - func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIImagePickerController { - let picker = UIImagePickerController() - picker.sourceType = sourceType - picker.allowsEditing = true - picker.delegate = context.coordinator - return picker - } - - func updateUIViewController(_: UIImagePickerController, context _: UIViewControllerRepresentableContext) {} - - func makeCoordinator() -> Coordinator { - Coordinator(self) - } - - class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { - var parent: ImagePicker - - init(_ parent: ImagePicker) { - self.parent = parent - } - - func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo _: [UIImagePickerController.InfoKey: Any]) { - // Handle the selected media - } - - func imagePickerControllerDidCancel(_: UIImagePickerController) { - // Handle cancellation - } +struct AttachmentMediaPickerView_Previews: PreviewProvider { + static var previews: some View { + AttachmentMediaPickerView() } }