another.im-ios/ConversationsClassic/View/Screens/Attachments/Media/AttachmentMediaPickerView.swift

459 lines
18 KiB
Swift
Raw Normal View History

2024-07-03 11:50:59 +00:00
import AVFoundation
2024-07-04 10:40:27 +00:00
import MobileCoreServices
2024-07-03 11:50:59 +00:00
import Photos
2024-07-02 09:56:27 +00:00
import SwiftUI
2024-07-02 10:23:27 +00:00
2024-07-04 09:17:05 +00:00
struct SelectedMedia {
2024-07-04 09:53:45 +00:00
let id: String
2024-07-04 09:17:05 +00:00
}
2024-07-02 09:56:27 +00:00
struct AttachmentMediaPickerView: View {
2024-07-03 11:50:59 +00:00
@State private var isCameraAccessGranted = AVCaptureDevice.authorizationStatus(for: .video) == .authorized
@State private var isGalleryAccessGranted = PHPhotoLibrary.authorizationStatus() == .authorized
2024-07-03 13:47:25 +00:00
2024-07-04 09:17:05 +00:00
@State private var thumbnails = [ThumbnailView]()
@State private var selectedMedia = [SelectedMedia]()
2024-07-03 11:50:59 +00:00
2024-07-04 10:40:27 +00:00
@State private var showCameraPicker = false
2024-07-03 11:50:59 +00:00
let gridSize = UIScreen.main.bounds.width / 3
2024-07-02 09:56:27 +00:00
var body: some View {
2024-07-03 11:50:59 +00:00
let columns = Array(repeating: GridItem(.flexible(), spacing: 0), count: 3)
2024-07-04 09:53:45 +00:00
VStack(spacing: 0) {
// List of media
ScrollView(showsIndicators: false) {
LazyVGrid(columns: columns, spacing: 0) {
// For camera
if isCameraAccessGranted {
2024-07-03 11:50:59 +00:00
ZStack {
2024-07-04 09:53:45 +00:00
CameraView()
.aspectRatio(1, contentMode: .fit)
.frame(maxWidth: .infinity)
Image(systemName: "camera")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 40, height: 40)
.foregroundColor(.white)
.padding(8)
.background(Color.black.opacity(0.5))
.clipShape(Circle())
.padding(8)
}
2024-07-04 10:40:27 +00:00
.onTapGesture {
showCameraPicker = true
}
2024-07-04 09:53:45 +00:00
} else {
Button {
openAppSettings()
} label: {
ZStack {
Rectangle()
.fill(Color.Material.Background.light)
.overlay {
VStack {
Image(systemName: "camera")
.foregroundColor(.Material.Elements.active)
.font(.system(size: 30))
Text("Allow camera access")
.foregroundColor(.Material.Text.main)
.font(.body3)
}
2024-07-03 11:50:59 +00:00
}
2024-07-04 09:53:45 +00:00
.frame(height: 100)
}
2024-07-03 11:50:59 +00:00
}
}
2024-07-04 09:53:45 +00:00
// For gallery
if isGalleryAccessGranted {
ForEach(thumbnails) { photo in
photo
}
} else {
Button {
openAppSettings()
} label: {
ZStack {
Rectangle()
.fill(Color.Material.Background.light)
.overlay {
VStack {
Image(systemName: "photo")
.foregroundColor(.Material.Elements.active)
.font(.system(size: 30))
Text("Allow gallery access")
.foregroundColor(.Material.Text.main)
.font(.body3)
}
2024-07-03 11:50:59 +00:00
}
2024-07-04 09:53:45 +00:00
.frame(height: 100)
}
2024-07-03 11:50:59 +00:00
}
}
2024-07-02 10:23:27 +00:00
}
2024-07-02 09:56:27 +00:00
}
2024-07-04 10:40:27 +00:00
.fullScreenCover(isPresented: $showCameraPicker) {
2024-07-10 13:00:54 +00:00
// TODO: fix it
// CameraPicker(sourceType: .camera) { data, type in
// store.dispatch(.conversationAction(.sendAttachment([.init(
// id: UUID().uuidString,
// type: type,
// data: data,
// thumbnail: Data(),
// string: ""
// )])))
// showCameraPicker = false
// store.dispatch(.conversationAction(.showAttachmentPicker(false)))
// }
// .edgesIgnoringSafeArea(.all)
2024-07-04 10:40:27 +00:00
}
2024-07-04 09:53:45 +00:00
// Send panel
Rectangle()
.foregroundColor(.Material.Shape.black)
.frame(maxWidth: .infinity)
.frame(height: self.selectedMedia.isEmpty ? 0 : 50)
.overlay {
HStack {
Text(L10n.Attachment.Send.media)
.foregroundColor(.Material.Text.white)
.font(.body1)
Image(systemName: "arrow.up.circle")
.foregroundColor(.Material.Text.white)
.font(.body1)
.padding(.leading, 8)
}
.padding()
}
.clipped()
.onTapGesture {
2024-07-09 13:09:34 +00:00
let ids = selectedMedia.map { $0.id }
sendGalleryMedia(ids: ids)
2024-07-10 14:13:47 +00:00
store.dispatch(.sharingAction(.showSharing(false)))
2024-07-04 09:53:45 +00:00
}
2024-07-03 10:47:59 +00:00
}
2024-07-03 11:50:59 +00:00
.onAppear {
2024-07-03 13:47:25 +00:00
DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 0.2) {
checkCameraAccess()
checkGalleryAccess()
}
2024-07-03 11:50:59 +00:00
}
2024-07-03 10:47:59 +00:00
}
2024-07-03 11:50:59 +00:00
private func checkCameraAccess() {
let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .authorized:
isCameraAccessGranted = true
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { granted in
DispatchQueue.main.async {
self.isCameraAccessGranted = granted
}
}
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
case .denied, .restricted:
isCameraAccessGranted = false
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
@unknown default:
isCameraAccessGranted = false
}
}
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
private func checkGalleryAccess() {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized, .limited:
isGalleryAccessGranted = true
2024-07-09 13:09:34 +00:00
fetchGallery()
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
case .notDetermined:
PHPhotoLibrary.requestAuthorization { status in
DispatchQueue.main.async {
self.isGalleryAccessGranted = status == .authorized
if self.isGalleryAccessGranted {
2024-07-09 13:09:34 +00:00
self.fetchGallery()
2024-07-03 10:47:59 +00:00
}
2024-07-03 11:50:59 +00:00
}
2024-07-03 10:47:59 +00:00
}
2024-07-03 11:50:59 +00:00
case .denied, .restricted:
isGalleryAccessGranted = false
@unknown default:
isGalleryAccessGranted = false
}
}
2024-07-09 13:09:34 +00:00
private func fetchGallery() {
2024-07-03 11:50:59 +00:00
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
2024-07-04 08:47:08 +00:00
let assets = PHAsset.fetchAssets(with: fetchOptions)
2024-07-03 11:50:59 +00:00
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
option.isSynchronous = true
2024-07-03 10:47:59 +00:00
2024-07-03 11:50:59 +00:00
assets.enumerateObjects { asset, _, _ in
2024-07-04 08:47:08 +00:00
if asset.mediaType == .image {
manager.requestImage(
for: asset,
targetSize: PHImageManagerMaximumSize,
contentMode: .aspectFill,
options: option
) { image, _ in
image?.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in
if let image {
DispatchQueue.main.async {
2024-07-04 09:53:45 +00:00
self.thumbnails.append(ThumbnailView(id: asset.localIdentifier, image: image, gridSize: gridSize, selected: $selectedMedia))
2024-07-04 08:47:08 +00:00
}
}
})
}
} else if asset.mediaType == .video {
manager.requestAVAsset(forVideo: asset, options: nil) { avAsset, _, _ in
if let avAsset {
let imageGenerator = AVAssetImageGenerator(asset: avAsset)
imageGenerator.appliesPreferredTrackTransform = true
let time = CMTimeMake(value: 1, timescale: 2)
do {
let imageRef = try imageGenerator.copyCGImage(at: time, actualTime: nil)
let thumbnail = UIImage(cgImage: imageRef)
thumbnail.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in
if let image {
DispatchQueue.main.async {
2024-07-04 09:53:45 +00:00
self.thumbnails.append(ThumbnailView(id: asset.localIdentifier, image: image, gridSize: gridSize, selected: $selectedMedia, duration: asset.duration.minAndSec))
2024-07-04 08:47:08 +00:00
}
}
})
} catch {
print("Failed to create thumbnail image")
2024-07-03 11:50:59 +00:00
}
}
2024-07-04 08:47:08 +00:00
}
2024-07-03 10:47:59 +00:00
}
}
}
2024-07-09 13:09:34 +00:00
2024-07-10 13:00:54 +00:00
private func sendGalleryMedia(ids _: [String]) {
// var media: [AttachmentItem] = []
// let dispatchGroup = DispatchGroup()
//
// let asset = PHAsset.fetchAssets(withLocalIdentifiers: ids, options: nil)
// asset.enumerateObjects { asset, _, _ in
// dispatchGroup.enter()
// if asset.mediaType == .image {
// let manager = PHImageManager.default()
// let option = PHImageRequestOptions()
// option.isSynchronous = true
//
// manager.requestImage(
// for: asset,
// targetSize: PHImageManagerMaximumSize,
// contentMode: .aspectFill,
// options: option
// ) { image, _ in
// if let image {
// let data = image.jpegData(compressionQuality: 1.0) ?? Data()
// media.append(.init(type: .image, data: data, string: ""))
// }
// dispatchGroup.leave()
// }
// } else if asset.mediaType == .video {
// let manager = PHImageManager.default()
// let option = PHVideoRequestOptions()
// option.version = .current
// option.deliveryMode = .highQualityFormat
//
// manager.requestAVAsset(forVideo: asset, options: option) { avAsset, _, _ in
// if let avAsset {
// let url = (avAsset as? AVURLAsset)?.url
// let data = try? Data(contentsOf: url ?? URL(fileURLWithPath: ""))
// media.append(.init(type: .movie, data: data ?? Data(), string: ""))
// }
// dispatchGroup.leave()
// }
// }
// }
// dispatchGroup.notify(queue: .main) {
// store.dispatch(.conversationAction(.sendAttachment(.init(
// id: UUID().uuidString,
// items: media
// ))))
// }
2024-07-09 13:09:34 +00:00
}
2024-07-03 13:47:25 +00:00
}
2024-07-04 09:17:05 +00:00
private struct ThumbnailView: Identifiable, View {
2024-07-04 09:53:45 +00:00
let id: String
2024-07-03 13:47:25 +00:00
let gridSize: CGFloat
2024-07-04 08:47:08 +00:00
let duration: String?
2024-07-03 13:47:25 +00:00
@State private var image: UIImage
@State private var ready = false
2024-07-04 09:17:05 +00:00
@State private var selected = false
@Binding var selectedMedia: [SelectedMedia]
2024-07-03 11:50:59 +00:00
2024-07-04 09:53:45 +00:00
init(id: String, image: UIImage, gridSize: CGFloat, selected: Binding<[SelectedMedia]>, duration: String? = nil) {
self.id = id
2024-07-03 13:47:25 +00:00
self.image = image
self.gridSize = gridSize
2024-07-04 09:17:05 +00:00
_selectedMedia = selected
2024-07-04 08:47:08 +00:00
self.duration = duration
2024-07-03 13:47:25 +00:00
}
var body: some View {
if ready {
2024-07-04 08:47:08 +00:00
ZStack {
Image(uiImage: image)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: gridSize, height: gridSize)
.clipped()
if let duration {
VStack {
Spacer()
HStack {
Spacer()
Text(duration)
.foregroundColor(.Material.Text.white)
.font(.sub1)
.shadow(color: .black, radius: 2)
.padding(4)
}
}
}
2024-07-04 09:17:05 +00:00
if selected {
VStack {
HStack {
Spacer()
Circle()
.frame(width: 30, height: 30)
.shadow(color: .black, radius: 2)
.foregroundColor(.Material.Shape.white)
.overlay {
Image(systemName: "checkmark")
.foregroundColor(.Material.Elements.active)
.font(.body3)
}
.padding(4)
}
Spacer()
}
}
}
.onTapGesture {
withAnimation {
selected.toggle()
if selected {
2024-07-04 09:53:45 +00:00
selectedMedia.append(SelectedMedia(id: id))
2024-07-04 09:17:05 +00:00
} else {
2024-07-04 09:53:45 +00:00
selectedMedia.removeAll { $0.id == id }
2024-07-04 09:17:05 +00:00
}
}
2024-07-04 08:47:08 +00:00
}
2024-07-03 13:47:25 +00:00
} else {
ZStack {
Rectangle()
2024-07-04 08:21:12 +00:00
.fill(Color.Material.Background.light)
2024-07-03 13:47:25 +00:00
.overlay {
ProgressView()
}
.frame(width: gridSize, height: gridSize)
}
.onAppear {
image.scaleAndCropImage(toExampleSize: CGSize(width: gridSize, height: gridSize), completion: { image in
if let image {
self.image = image
ready = true
}
})
}
}
2024-07-02 09:56:27 +00:00
}
}
2024-07-03 11:50:59 +00:00
class CameraUIView: UIView {
var previewLayer: AVCaptureVideoPreviewLayer?
override func layoutSubviews() {
super.layoutSubviews()
previewLayer?.frame = bounds
2024-07-02 09:56:27 +00:00
}
}
2024-07-03 11:50:59 +00:00
struct CameraView: UIViewRepresentable {
func makeUIView(context _: Context) -> CameraUIView {
let view = CameraUIView()
let captureSession = AVCaptureSession()
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return view }
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return view }
captureSession.addInput(input)
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.videoGravity = .resizeAspectFill
view.layer.addSublayer(previewLayer)
view.previewLayer = previewLayer
captureSession.startRunning()
return view
}
func updateUIView(_ uiView: CameraUIView, context _: Context) {
uiView.previewLayer?.frame = uiView.bounds
}
}
2024-07-04 10:40:27 +00:00
struct CameraPicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType
2024-07-10 13:00:54 +00:00
var completionHandler: (Data, Data, AttachmentType) -> Void
2024-07-04 10:40:27 +00:00
func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
picker.sourceType = sourceType
picker.delegate = context.coordinator
picker.mediaTypes = [UTType.movie.identifier, UTType.image.identifier]
picker.videoQuality = .typeHigh
picker.videoMaximumDuration = 60 // 60 sec Limit
picker.view.backgroundColor = .clear
return picker
}
func updateUIViewController(_: UIImagePickerController, context _: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
let parent: CameraPicker
init(_ parent: CameraPicker) {
self.parent = parent
}
func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
2024-07-09 12:43:38 +00:00
// swiftlint:disable:next force_cast
let mediaType = info[.mediaType] as! String
2024-07-09 12:37:51 +00:00
2024-07-10 13:00:54 +00:00
// TODO: fix it
// if mediaType == UTType.image.identifier {
// if let image = info[.originalImage] as? UIImage {
// let data = image.jpegData(compressionQuality: 1.0) ?? Data()
// parent.completionHandler(data, .image)
// }
// } else if mediaType == UTType.movie.identifier {
// if let url = info[.mediaURL] as? URL {
// let data = try? Data(contentsOf: url)
// parent.completionHandler(data ?? Data(), .movie)
// }
// }
2024-07-04 10:40:27 +00:00
}
}
}