This commit is contained in:
fmodf 2024-07-04 11:17:05 +02:00
parent 3aca0a69c1
commit 8aa1ed6b75

View file

@ -2,11 +2,16 @@ import AVFoundation
import Photos import Photos
import SwiftUI import SwiftUI
struct SelectedMedia {
let path: String
}
struct AttachmentMediaPickerView: View { struct AttachmentMediaPickerView: View {
@State private var isCameraAccessGranted = AVCaptureDevice.authorizationStatus(for: .video) == .authorized @State private var isCameraAccessGranted = AVCaptureDevice.authorizationStatus(for: .video) == .authorized
@State private var isGalleryAccessGranted = PHPhotoLibrary.authorizationStatus() == .authorized @State private var isGalleryAccessGranted = PHPhotoLibrary.authorizationStatus() == .authorized
@State private var photos = [PhotoView]() @State private var thumbnails = [ThumbnailView]()
@State private var selectedMedia = [SelectedMedia]()
let gridSize = UIScreen.main.bounds.width / 3 let gridSize = UIScreen.main.bounds.width / 3
@ -55,7 +60,7 @@ struct AttachmentMediaPickerView: View {
// For pictures // For pictures
if isGalleryAccessGranted { if isGalleryAccessGranted {
ForEach(photos) { photo in ForEach(thumbnails) { photo in
photo photo
} }
} else { } else {
@ -155,7 +160,7 @@ struct AttachmentMediaPickerView: View {
image?.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in image?.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in
if let image { if let image {
DispatchQueue.main.async { DispatchQueue.main.async {
self.photos.append(PhotoView(image: image, gridSize: gridSize)) self.thumbnails.append(ThumbnailView(image: image, gridSize: gridSize, selected: $selectedMedia))
} }
} }
}) })
@ -172,7 +177,7 @@ struct AttachmentMediaPickerView: View {
thumbnail.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in thumbnail.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in
if let image { if let image {
DispatchQueue.main.async { DispatchQueue.main.async {
self.photos.append(PhotoView(image: image, gridSize: gridSize, duration: asset.duration.minAndSec)) self.thumbnails.append(ThumbnailView(image: image, gridSize: gridSize, selected: $selectedMedia, duration: asset.duration.minAndSec))
} }
} }
}) })
@ -195,17 +200,20 @@ struct AttachmentMediaPickerView: View {
} }
} }
private struct PhotoView: Identifiable, View { private struct ThumbnailView: Identifiable, View {
let id = UUID() let id = UUID()
let gridSize: CGFloat let gridSize: CGFloat
let duration: String? let duration: String?
@State private var image: UIImage @State private var image: UIImage
@State private var ready = false @State private var ready = false
@State private var selected = false
@Binding var selectedMedia: [SelectedMedia]
init(image: UIImage, gridSize: CGFloat, duration: String? = nil) { init(image: UIImage, gridSize: CGFloat, selected: Binding<[SelectedMedia]>, duration: String? = nil) {
self.image = image self.image = image
self.gridSize = gridSize self.gridSize = gridSize
_selectedMedia = selected
self.duration = duration self.duration = duration
} }
@ -230,6 +238,38 @@ private struct PhotoView: Identifiable, View {
} }
} }
} }
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 { } else {
ZStack { ZStack {