Skip to content

Commit

Permalink
Native picker prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Aug 9, 2023
1 parent 5c34354 commit 9cc482b
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions WordPress/Classes/ViewRelated/Media/MediaLibraryPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,84 @@ 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?
private var blog: Blog?

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..<result.count {
assets.append(result[index])
}
let stub = WPMediaPickerViewController()
self.delegate?.mediaPickerController(stub, didFinishPicking: assets)

// dismiss(animated: true)
//
// let existingSelection = self.selection
// var newSelection = [String: PHPickerResult]()
// for result in results {
// let identifier = result.assetIdentifier!
// newSelection[identifier] = existingSelection[identifier] ?? result
// }
//
// // Track the selection in case the user deselects it later.
// selection = newSelection
// selectedAssetIdentifiers = results.map(\.assetIdentifier!)
// selectedAssetIdentifierIterator = selectedAssetIdentifiers.makeIterator()
//
// if selection.isEmpty {
// displayEmptyImage()
// } else {
// displayNext()
// }
}
}

Expand Down

0 comments on commit 9cc482b

Please sign in to comment.