Skip to content

Commit

Permalink
Add MediaPickerMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Aug 21, 2023
1 parent b93ece7 commit 0b1ef40
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import WordPressShared
import SwiftUI
import SVProgressHUD
import Gridicons
import PhotosUI

extension SitePickerViewController {

Expand Down Expand Up @@ -35,24 +36,23 @@ extension SitePickerViewController {
}

private func makeUpdateSiteIconActions() -> [UIAction] {
let presenter = makeSiteIconPresenter()
let mediaMenu = MediaPickerMenu(viewConroller: self, filter: .images)
var actions: [UIAction] = []
if FeatureFlag.nativePhotoPicker.enabled {
actions += [
MediaPickerMenu.makePickFromPhotosAction { [weak self] in
self?.updateSiteIcon(source: .photosLibrary)
},
MediaPickerMenu.makeTakePhotoAction { [weak self] in
self?.updateSiteIcon(source: .camera)
},
MediaPickerMenu.makePickFromMediaAction { [weak self] in
self?.updateSiteIcon(source: .mediaLibrary)
}
mediaMenu.makePhotosAction(delegate: presenter),
mediaMenu.makeCameraAction(delegate: presenter),
mediaMenu.makeMediaAction(blog: blog, delegate: presenter)
]
} else {
actions.append(UIAction(
title: SiteIconAlertStrings.Actions.changeSiteIcon,
image: UIImage(systemName: "photo.on.rectangle"),
handler: { [weak self] _ in self?.updateSiteIcon(source: .combined) }
handler: { [weak self] _ in
guard let self else { return }
presenter.presentPickerFrom(self)
}
))
}
if FeatureFlag.siteIconCreator.enabled {
Expand All @@ -73,16 +73,9 @@ extension SitePickerViewController {
return actions
}

enum SiteIconSource {
case photosLibrary
case camera
case mediaLibrary
case combined // legacy option
}

func updateSiteIcon(source: SiteIconSource = .combined) {
siteIconPickerPresenter = SiteIconPickerPresenter(blog: blog)
siteIconPickerPresenter?.onCompletion = { [ weak self] media, error in
private func makeSiteIconPresenter() -> SiteIconPickerPresenter {
let presenter = SiteIconPickerPresenter(blog: blog)
presenter.onCompletion = { [ weak self] media, error in
if error != nil {
self?.showErrorForSiteIconUpdate()
} else if let media = media {
Expand All @@ -95,22 +88,12 @@ extension SitePickerViewController {
self?.siteIconPickerPresenter = nil
self?.startAlertTimer()
}

siteIconPickerPresenter?.onIconSelection = { [weak self] in
presenter.onIconSelection = { [weak self] in
self?.blogDetailHeaderView.updatingIcon = true
self?.dismiss(animated: true)
}

switch source {
case .photosLibrary:
siteIconPickerPresenter?.presentPhotosPicker(from: self)
case .camera:
siteIconPickerPresenter?.presentCamera(from: self)
case .mediaLibrary:
siteIconPickerPresenter?.presentMediaLibraryPicker(from: self)
case .combined:
siteIconPickerPresenter?.presentPickerFrom(self)
}
self.siteIconPickerPresenter = presenter
return presenter
}

func showEmojiPicker() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,6 @@ final class SiteIconPickerPresenter: NSObject {
unregisterChangeObserver()
}

func presentPhotosPicker(from presentingViewController: UIViewController) {
var configuration = PHPickerConfiguration()
configuration.filter = .images

let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
presentingViewController.present(picker, animated: true)
}

func presentCamera(from presentingViewController: UIViewController) {
let picker = WPMediaCapturePresenter(presenting: presentingViewController)
picker.completionBlock = { [weak self] info in
if let image = info?[UIImagePickerController.InfoKey.originalImage] as? UIImage {
self?.showImageCropViewController(image, presentingViewController: presentingViewController)
}
}
picker.mediaType = .image
picker.presentCapture()
mediaCapturePresenter = picker // Retain
}

func presentMediaLibraryPicker(from presentingViewController: UIViewController) {
let options = WPMediaPickerOptions()
options.showMostRecentFirst = true
options.filter = [.image]
options.allowMultipleSelection = false
options.showSearchBar = true
options.badgedUTTypes = [UTType.gif.identifier]
options.preferredStatusBarStyle = WPStyleGuide.preferredStatusBarStyle
options.allowCaptureOfMedia = false

let pickerViewController = WPNavigationMediaPickerViewController(options: options)

let dataSource = MediaLibraryPickerDataSource(blog: blog)
dataSource.ignoreSyncErrors = true
self.dataSource = dataSource

pickerViewController.showGroupSelector = false
pickerViewController.dataSource = dataSource
pickerViewController.delegate = self
pickerViewController.modalPresentationStyle = .formSheet

presentingViewController.present(pickerViewController, animated: true)
}

/// Presents a new WPMediaPickerViewController instance.
///
@objc func presentPickerFrom(_ viewController: UIViewController) {
Expand All @@ -136,7 +91,7 @@ final class SiteIconPickerPresenter: NSObject {

/// Shows a new ImageCropViewController for the given image.
///
fileprivate func showImageCropViewController(_ image: UIImage, presentingViewController: UIViewController? = nil) {
func showImageCropViewController(_ image: UIImage, presentingViewController: UIViewController? = nil) {
DispatchQueue.main.async {
SVProgressHUD.dismiss()
let imageCropViewController = ImageCropViewController(image: image)
Expand Down Expand Up @@ -241,17 +196,29 @@ extension SiteIconPickerPresenter: PHPickerViewControllerDelegate {
WPAnalytics.track(.siteSettingsSiteIconGalleryPicked)
self.showLoadingMessage()
self.originalMedia = nil
result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] image, _ in
if let image = image as? UIImage {
MediaPickerMenu.loadImage(for: result) { [weak self] image, error in
if let image {
self?.showImageCropViewController(image, presentingViewController: picker)
} else {
DDLogError("Failed to load image: \(String(describing: error))")
self?.showErrorLoadingImageMessage()
}
}
}
}

extension SiteIconPickerPresenter: UINavigationControllerDelegate {}
extension SiteIconPickerPresenter: ImagePickerControllerDelegate {
func imagePicker(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let presentingViewController = picker.presentingViewController else {
return
}
presentingViewController.dismiss(animated: true) {
if let image = info[.originalImage] as? UIImage {
self.showImageCropViewController(image, presentingViewController: presentingViewController)
}
}
}
}

extension SiteIconPickerPresenter: WPMediaPickerViewControllerDelegate {

Expand Down
37 changes: 0 additions & 37 deletions WordPress/Classes/ViewRelated/Media/MediaPicker.swift

This file was deleted.

Loading

0 comments on commit 0b1ef40

Please sign in to comment.