This commit is contained in:
fmodf 2024-07-02 12:23:27 +02:00
parent 21b4e772d8
commit 01f45af8f3
2 changed files with 72 additions and 15 deletions

View file

@ -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)
}
}
}

View file

@ -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)