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 SwiftUI
import UIKit 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 { struct AttachmentMediaPickerView: View {
@StateObject private var mediaManager = MediaManager()
@State private var isPickerPresented = false @State private var isPickerPresented = false
@State private var mediaType: UIImagePickerController.SourceType = .photoLibrary @State private var selectedPhoto: UIImage?
var body: some View { var body: some View {
VStack { ScrollView {
Button(action: { LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
mediaType = .photoLibrary if let cameraFeed = mediaManager.cameraFeed {
isPickerPresented = true Button(action: {
}) { isPickerPresented = true
Text("Select from Photo Library") }) {
} Image(uiImage: cameraFeed)
.resizable()
.aspectRatio(contentMode: .fill)
}
}
Button(action: { ForEach(mediaManager.photos, id: \.self) { photo in
mediaType = .camera Button(action: {
isPickerPresented = true selectedPhoto = photo
}) { }) {
Text("Take Photo or Video") Image(uiImage: photo)
.resizable()
.aspectRatio(contentMode: .fill)
}
}
} }
} }
.sheet(isPresented: $isPickerPresented) { .sheet(isPresented: $isPickerPresented) {
ImagePicker(sourceType: mediaType) ImagePicker(sourceType: .camera)
} }
} }
} }

View file

@ -17,7 +17,19 @@ struct AttachmentPickerScreen: View {
AttachmentHeader() AttachmentHeader()
// Pickers // Pickers
Spacer() switch selectedTab {
case .media:
AttachmentMediaPickerView()
case .files:
AttachmentFilesPickerView()
case .location:
AttachmentLocationPickerView()
case .contacts:
AttachmentContactsPickerView()
}
// Tab bar // Tab bar
AttachmentTabBar(selectedTab: $selectedTab) AttachmentTabBar(selectedTab: $selectedTab)