From 01f45af8f32f8dfe90f1f7672caa22d04c287dd8 Mon Sep 17 00:00:00 2001 From: fmodf Date: Tue, 2 Jul 2024 12:23:27 +0200 Subject: [PATCH] wip --- .../AttachmentMediaPickerView.swift | 73 +++++++++++++++---- .../Attachments/AttachmentPickerScreen.swift | 14 +++- 2 files changed, 72 insertions(+), 15 deletions(-) diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift index bf3df4d..77d16c8 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentMediaPickerView.swift @@ -1,28 +1,73 @@ +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 mediaType: UIImagePickerController.SourceType = .photoLibrary + @State private var selectedPhoto: UIImage? var body: some View { - VStack { - Button(action: { - mediaType = .photoLibrary - isPickerPresented = true - }) { - Text("Select from Photo Library") - } + ScrollView { + LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) { + if let cameraFeed = mediaManager.cameraFeed { + Button(action: { + isPickerPresented = true + }) { + Image(uiImage: cameraFeed) + .resizable() + .aspectRatio(contentMode: .fill) + } + } - Button(action: { - mediaType = .camera - isPickerPresented = true - }) { - Text("Take Photo or Video") + ForEach(mediaManager.photos, id: \.self) { photo in + Button(action: { + selectedPhoto = photo + }) { + Image(uiImage: photo) + .resizable() + .aspectRatio(contentMode: .fill) + } + } } } .sheet(isPresented: $isPickerPresented) { - ImagePicker(sourceType: mediaType) + ImagePicker(sourceType: .camera) } } } diff --git a/ConversationsClassic/View/Screens/Attachments/AttachmentPickerScreen.swift b/ConversationsClassic/View/Screens/Attachments/AttachmentPickerScreen.swift index 1ca9083..5208d58 100644 --- a/ConversationsClassic/View/Screens/Attachments/AttachmentPickerScreen.swift +++ b/ConversationsClassic/View/Screens/Attachments/AttachmentPickerScreen.swift @@ -17,7 +17,19 @@ struct AttachmentPickerScreen: View { AttachmentHeader() // Pickers - Spacer() + switch selectedTab { + case .media: + AttachmentMediaPickerView() + + case .files: + AttachmentFilesPickerView() + + case .location: + AttachmentLocationPickerView() + + case .contacts: + AttachmentContactsPickerView() + } // Tab bar AttachmentTabBar(selectedTab: $selectedTab)