diff --git a/WordPress/Classes/ViewRelated/Media/MediaLibraryPicker.swift b/WordPress/Classes/ViewRelated/Media/MediaLibraryPicker.swift index c6723c5b6115..48f7757538f0 100644 --- a/WordPress/Classes/ViewRelated/Media/MediaLibraryPicker.swift +++ b/WordPress/Classes/ViewRelated/Media/MediaLibraryPicker.swift @@ -3,9 +3,10 @@ import MobileCoreServices import CoreGraphics import Photos import UniformTypeIdentifiers +import PhotosUI /// Encapsulates launching and customization of a media picker to import media from the Photos Library -final class MediaLibraryPicker: NSObject { +final class MediaLibraryPicker: NSObject, PHPickerViewControllerDelegate { private let dataSource = WPPHAssetDataSource() weak var delegate: WPMediaPickerViewControllerDelegate? @@ -13,23 +14,73 @@ final class MediaLibraryPicker: NSObject { func presentPicker(origin: UIViewController, blog: Blog) { self.blog = blog - let options = WPMediaPickerOptions() - options.showMostRecentFirst = true - options.filter = [.all] - options.allowCaptureOfMedia = false - options.badgedUTTypes = [UTType.gif.identifier] - options.preferredStatusBarStyle = WPStyleGuide.preferredStatusBarStyle - - let picker = WPNavigationMediaPickerViewController(options: options) - picker.dataSource = dataSource - picker.delegate = delegate - picker.mediaPicker.registerClass(forReusableCellOverlayViews: DisabledVideoOverlay.self) - - if FeatureFlag.mediaPickerPermissionsNotice.enabled { - picker.mediaPicker.registerClass(forCustomHeaderView: DeviceMediaPermissionsHeader.self) - } + var configuration = PHPickerConfiguration(photoLibrary: .shared()) + + // Set the filter type according to the user’s selection. +// configuration.filter = filter + // Set the mode to avoid transcoding, if possible, if your app supports arbitrary image/video encodings. + configuration.preferredAssetRepresentationMode = .compatible + // Set the selection behavior to respect the user’s selection order. + configuration.selection = .ordered + // TODO: (kean) what should be the selection limit? + configuration.selectionLimit = 20 + // Set the preselected asset identifiers with the identifiers that the app tracks. + // configuration.preselectedAssetIdentifiers = selectedAssetIdentifiers + + let picker = PHPickerViewController(configuration: configuration) + picker.delegate = self origin.present(picker, animated: true) + + +// let options = WPMediaPickerOptions() +// options.showMostRecentFirst = true +// options.filter = [.all] +// options.allowCaptureOfMedia = false +// // TODO: (kean) how do we handle GIFs? +// options.badgedUTTypes = [UTType.gif.identifier] +// options.preferredStatusBarStyle = WPStyleGuide.preferredStatusBarStyle +// +// let picker = WPNavigationMediaPickerViewController(options: options) +// picker.dataSource = dataSource +// picker.delegate = delegate +// picker.mediaPicker.registerClass(forReusableCellOverlayViews: DisabledVideoOverlay.self) +// +// if FeatureFlag.mediaPickerPermissionsNotice.enabled { +// picker.mediaPicker.registerClass(forCustomHeaderView: DeviceMediaPermissionsHeader.self) +// } +// +// origin.present(picker, animated: true) + } + + func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { + let result = PHAsset.fetchAssets(withLocalIdentifiers: results.compactMap(\.assetIdentifier), options: nil) + var assets: [PHAsset] = [] + for index in 0..