Edit attachments gallery for non-drawings

This commit is contained in:
Shadowfacts 2024-12-07 12:54:41 -05:00
parent c564bb4112
commit ad5f45c620
4 changed files with 50 additions and 8 deletions

View File

@ -7,6 +7,8 @@
import UIKit import UIKit
import GalleryVC import GalleryVC
import TuskerComponents
import Photos
struct AttachmentsGalleryDataSource: GalleryDataSource { struct AttachmentsGalleryDataSource: GalleryDataSource {
let collectionView: UICollectionView let collectionView: UICollectionView
@ -24,15 +26,33 @@ struct AttachmentsGalleryDataSource: GalleryDataSource {
case .editing(_, _, _): case .editing(_, _, _):
fatalError("TODO") fatalError("TODO")
case .asset(_): case .asset(let id):
fatalError("TODO") content = LoadingGalleryContentViewController(caption: nil) {
if let (image, gifData) = await fetchImageAndGIFData(assetID: id) {
let gifController = gifData.map(GIFController.init)
return ImageGalleryContentViewController(image: image, caption: nil, gifController: gifController)
} else {
return nil
}
}
case .drawing(let drawing): case .drawing(let drawing):
let image = drawing.imageInLightMode(from: drawing.bounds) let image = drawing.imageInLightMode(from: drawing.bounds)
content = ImageGalleryContentViewController(image: image, caption: nil, gifController: nil) content = ImageGalleryContentViewController(image: image, caption: nil, gifController: nil)
case .file(_, _): case .file(let url, let type):
fatalError("TODO") if type.conforms(to: .movie) {
content = VideoGalleryContentViewController(url: url, caption: nil)
} else if type.conforms(to: .image),
let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
let gifController = type == .gif ? GIFController(gifData: data) : nil
content = ImageGalleryContentViewController(image: image, caption: nil, gifController: gifController)
} else {
return LoadingGalleryContentViewController(caption: nil) {
nil
}
}
case .none: case .none:
return LoadingGalleryContentViewController(caption: nil) { return LoadingGalleryContentViewController(caption: nil) {
@ -47,4 +67,24 @@ struct AttachmentsGalleryDataSource: GalleryDataSource {
collectionView.cellForItem(at: IndexPath(item: index, section: 0)) collectionView.cellForItem(at: IndexPath(item: index, section: 0))
} }
private func fetchImageAndGIFData(assetID id: String) async -> (UIImage, Data?)? {
guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [id], options: nil).firstObject else {
return nil
}
let (type, data) = await withCheckedContinuation { continuation in
PHImageManager.default().requestImageDataAndOrientation(for: asset, options: nil) { data, typeIdentifier, orientation, info in
continuation.resume(returning: (typeIdentifier, data))
}
}
guard let data,
let image = UIImage(data: data) else {
return nil
}
if type == UTType.gif.identifier {
return (image, data)
} else {
return (image, nil)
}
}
} }

View File

@ -21,7 +21,8 @@ struct AttachmentsSection: View {
) )
// Impose a minimum height, because otherwise it defaults to zero which prevents the collection // Impose a minimum height, because otherwise it defaults to zero which prevents the collection
// view from laying out, and leaving the intrinsic content size at zero too. // view from laying out, and leaving the intrinsic content size at zero too.
.frame(minHeight: 100) // Add 4 to the minItemSize because otherwise drag-and-drop while reordering can alter the contentOffset by that much.
.frame(minHeight: 104)
} }
} }

View File

@ -36,8 +36,8 @@ class EditAttachmentWrapperGalleryContentViewController: UIViewController, Galle
false false
} }
var canAnimateFromSourceView: Bool { var presentationAnimation: GalleryContentPresentationAnimation {
wrapped.canAnimateFromSourceView wrapped.presentationAnimation
} }
var hideControlsOnZoom: Bool { var hideControlsOnZoom: Bool {

View File

@ -14,7 +14,8 @@ extension UIView {
while let superview = view.superview { while let superview = view.superview {
if superview.layer.masksToBounds { if superview.layer.masksToBounds {
return superview return superview
} else if superview is UIScrollView { } else if let scrollView = superview as? UIScrollView,
scrollView.isScrollEnabled {
return self return self
} else { } else {
view = superview view = superview