wip
This commit is contained in:
parent
8da928e237
commit
944f38d301
|
@ -128,8 +128,9 @@ struct AttachmentMediaPickerView: View {
|
||||||
}
|
}
|
||||||
.clipped()
|
.clipped()
|
||||||
.onTapGesture {
|
.onTapGesture {
|
||||||
// TODO: Send selected media
|
let ids = selectedMedia.map { $0.id }
|
||||||
print("Send media files")
|
sendGalleryMedia(ids: ids)
|
||||||
|
store.dispatch(.conversationAction(.showAttachmentPicker(false)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.onAppear {
|
.onAppear {
|
||||||
|
@ -166,14 +167,14 @@ struct AttachmentMediaPickerView: View {
|
||||||
switch status {
|
switch status {
|
||||||
case .authorized, .limited:
|
case .authorized, .limited:
|
||||||
isGalleryAccessGranted = true
|
isGalleryAccessGranted = true
|
||||||
fetchImages()
|
fetchGallery()
|
||||||
|
|
||||||
case .notDetermined:
|
case .notDetermined:
|
||||||
PHPhotoLibrary.requestAuthorization { status in
|
PHPhotoLibrary.requestAuthorization { status in
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.isGalleryAccessGranted = status == .authorized
|
self.isGalleryAccessGranted = status == .authorized
|
||||||
if self.isGalleryAccessGranted {
|
if self.isGalleryAccessGranted {
|
||||||
self.fetchImages()
|
self.fetchGallery()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,7 +187,7 @@ struct AttachmentMediaPickerView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func fetchImages() {
|
private func fetchGallery() {
|
||||||
let fetchOptions = PHFetchOptions()
|
let fetchOptions = PHFetchOptions()
|
||||||
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
|
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
|
||||||
let assets = PHAsset.fetchAssets(with: fetchOptions)
|
let assets = PHAsset.fetchAssets(with: fetchOptions)
|
||||||
|
@ -235,6 +236,58 @@ struct AttachmentMediaPickerView: View {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func sendGalleryMedia(ids: [String]) {
|
||||||
|
var mediaData: [Data] = []
|
||||||
|
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()
|
||||||
|
mediaData.append(data)
|
||||||
|
}
|
||||||
|
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: ""))
|
||||||
|
mediaData.append(data ?? Data())
|
||||||
|
}
|
||||||
|
dispatchGroup.leave()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dispatchGroup.notify(queue: .main) {
|
||||||
|
store.dispatch(.conversationAction(.sendAttachment(.init(
|
||||||
|
id: UUID().uuidString,
|
||||||
|
type: .image,
|
||||||
|
url: nil,
|
||||||
|
data: mediaData,
|
||||||
|
str: nil,
|
||||||
|
localPath: nil
|
||||||
|
))))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ThumbnailView: Identifiable, View {
|
private struct ThumbnailView: Identifiable, View {
|
||||||
|
|
Loading…
Reference in a new issue