Explorar o código

mediapicker uses delegate now

nayooti %!s(int64=5) %!d(string=hai) anos
pai
achega
3603706f69

+ 7 - 17
deltachat-ios/Controller/ChatViewController.swift

@@ -103,7 +103,9 @@ class ChatViewController: MessagesViewController, UINavigationControllerDelegate
     var showCustomNavBar = true
 
     private lazy var mediaPicker: MediaPicker? = {
-        return MediaPicker(navigationController: navigationController)
+        let mediaPicker = MediaPicker(navigationController: navigationController)
+        mediaPicker.delegate = self 
+        return mediaPicker
     }()
 
     var emptyStateView: EmptyStateLabel = {
@@ -723,21 +725,7 @@ class ChatViewController: MessagesViewController, UINavigationControllerDelegate
     }
 
     private func showCameraViewController() {
-        if UIImagePickerController.isSourceTypeAvailable(.camera) {
-            let imagePickerController = UIImagePickerController()
-            imagePickerController.sourceType = .camera
-            imagePickerController.delegate = self
-            let mediaTypes: [String] = UIImagePickerController.availableMediaTypes(for: .camera) ?? []
-            imagePickerController.mediaTypes = mediaTypes
-            imagePickerController.setEditing(true, animated: true)
-            present(imagePickerController, animated: true, completion: nil)
-        } else {
-            let alert = UIAlertController(title: String.localized("chat_camera_unavailable"), message: nil, preferredStyle: .alert)
-            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
-                self.navigationController?.dismiss(animated: true, completion: nil)
-            }))
-            present(alert, animated: true, completion: nil)
-        }
+       mediaPicker?.showCamera(delegate: self)
     }
 
     private func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
@@ -1525,8 +1513,9 @@ extension MessageCollectionViewCell {
     }
 }
 
+/*
 // MARK: - UIImagePickerControllerDelegate
-extension ChatViewController: UIImagePickerControllerDelegate {
+extension MediaPicker: UIImagePickerControllerDelegate {
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
 
         enum PickerMediaType: String {
@@ -1553,3 +1542,4 @@ extension ChatViewController: UIImagePickerControllerDelegate {
     }
 
 }
+*/

+ 4 - 2
deltachat-ios/Controller/EditGroupViewController.swift

@@ -12,7 +12,9 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     var avatarSelectionCell: AvatarSelectionCell
 
     private lazy var mediaPicker: MediaPicker? = {
-        return MediaPicker(navigationController: navigationController)
+        let mediaPicker = MediaPicker(navigationController: navigationController)
+        mediaPicker.delegate = self 
+        return mediaPicker
     }()
 
     lazy var groupNameCell: TextFieldCell = {
@@ -103,7 +105,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showCamera(delegate: self)
+        mediaPicker?.showCamera(delegate: self, allowCropping: true, supportedMediaTypes: .photo)
     }
 
     func onImageSelected(image: UIImage) {

+ 1 - 1
deltachat-ios/Controller/EditSettingsController.swift

@@ -127,7 +127,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showCamera(delegate: self)
+        mediaPicker?.showCamera(delegate: self, allowCropping: true, supportedMediaTypes: .photo)
     }
 
     private func deleteProfileIconPressed(_ action: UIAlertAction) {

+ 1 - 1
deltachat-ios/Controller/NewGroupController.swift

@@ -348,7 +348,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
 
     private func showCamera(delegate: MediaPickerDelegate) {
-        mediaPicker?.showCamera(delegate: delegate)
+        mediaPicker?.showCamera(delegate: self, allowCropping: true, supportedMediaTypes: .photo)
     }
 
     private func showQrCodeInvite(chatId: Int) {

+ 62 - 39
deltachat-ios/Helper/MediaPicker.swift

@@ -28,8 +28,18 @@ extension MediaPickerDelegate {
 
 class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate, AudioRecorderControllerDelegate, UIDocumentPickerDelegate {
 
+    enum CameraMediaTypes {
+        case photo
+        case allAvailable
+    }
+
+    enum PickerMediaType: String {
+        case image = "public.image"
+        case video = "public.movie"
+     }
+
     private weak var navigationController: UINavigationController?
-    private weak var delegate: MediaPickerDelegate?
+    weak var delegate: MediaPickerDelegate?
 
     init(navigationController: UINavigationController?) {
         // it does not make sense to give nil here, but it makes construction easier
@@ -115,32 +125,30 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         navigationController?.present(controller, animated: true, completion: nil)
     }
 
-    func showCamera(delegate: MediaPickerDelegate, allowCropping: Bool) {
+    func showCamera(delegate: MediaPickerDelegate, allowCropping: Bool, supportedMediaTypes: CameraMediaTypes) {
         if UIImagePickerController.isSourceTypeAvailable(.camera) {
-            var croppingParameters: CroppingParameters = CroppingParameters()
+            let imagePickerController = UIImagePickerController()
+            imagePickerController.sourceType = .camera
+            imagePickerController.delegate = self
+            let mediaTypes: [String]
+            switch supportedMediaTypes {
+            case .photo:
+                mediaTypes = [PickerMediaType.image.rawValue]
+            case .allAvailable:
+                mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera) ?? []
+            }
             if allowCropping {
-                croppingParameters = CroppingParameters(isEnabled: true,
-                allowResizing: true,
-                allowMoving: true,
-                minimumSize: CGSize(width: 70, height: 70))
+                imagePickerController.allowsEditing = true
             }
-
-            let cameraViewController = CameraViewController(
-                croppingParameters: croppingParameters,
-                allowsLibraryAccess: false,
-                allowsSwapCameraOrientation: true,
-                allowVolumeButtonCapture: false,
-                completion: { [weak self] image, _ in
-                    if let image = image {
-                        self?.delegate?.onImageSelected(image: image)
-                    }
-                    self?.navigationController?.dismiss(animated: true, completion: nil)}
-            )
-            self.delegate = delegate
-            cameraViewController.modalPresentationStyle = .fullScreen
-            navigationController?.present(cameraViewController, animated: true, completion: nil)
+            imagePickerController.mediaTypes = mediaTypes
+            imagePickerController.setEditing(true, animated: true)
+            navigationController?.present(imagePickerController, animated: true, completion: nil)
         } else {
-            let alert = UIAlertController(title: String.localized("chat_camera_unavailable"), message: nil, preferredStyle: .alert)
+            let alert = UIAlertController(
+                title: String.localized("chat_camera_unavailable"),
+                message: nil,
+                preferredStyle: .alert
+            )
             alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
                 self.navigationController?.dismiss(animated: true, completion: nil)
             }))
@@ -149,27 +157,42 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
     }
 
     func showCamera(delegate: MediaPickerDelegate) {
-        showCamera(delegate: delegate, allowCropping: true)
+        showCamera(delegate: delegate, allowCropping: false, supportedMediaTypes: .allAvailable)
     }
 
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
-        if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
-            videoUrl.convertToMp4(completionHandler: { url, error in
-                if let url = url {
-                    self.delegate?.onVideoSelected(url: (url as NSURL))
-                } else if let error = error {
-                    logger.error(error.localizedDescription)
-                    let alert = UIAlertController(title: String.localized("error"), message: nil, preferredStyle: .alert)
-                    alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
-                        self.navigationController?.dismiss(animated: true, completion: nil)
-                    }))
-                    self.navigationController?.present(alert, animated: true, completion: nil)
+
+        if let type = info[.mediaType] as? String, let mediaType = PickerMediaType(rawValue: type) {
+
+            switch mediaType {
+            case .video:
+                if let videoUrl = info[.mediaURL] as? URL {
+                    handleVideoUrl(url: videoUrl)
+                }
+            case .image:
+                if let image = info[.editedImage] as? UIImage {
+                    self.delegate?.onImageSelected(image: image)
+                } else if let image = info[.originalImage] as? UIImage {
+                    self.delegate?.onImageSelected(image: image)
                 }
-            })
-        } else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? NSURL {
-            self.delegate?.onImageSelected(url: imageUrl)
+            }
         }
-        navigationController?.dismiss(animated: true, completion: nil)
+        picker.dismiss(animated: true, completion: nil)
+    }
+
+    func handleVideoUrl(url: URL) {
+        url.convertToMp4(completionHandler: { url, error in
+            if let url = url {
+                self.delegate?.onVideoSelected(url: (url as NSURL))
+            } else if let error = error {
+                logger.error(error.localizedDescription)
+            let alert = UIAlertController(title: String.localized("error"), message: nil, preferredStyle: .alert)
+            alert.addAction(UIAlertAction(title: String.localized("ok"), style: .cancel, handler: { _ in
+                self.navigationController?.dismiss(animated: true, completion: nil)
+            }))
+                self.navigationController?.present(alert, animated: true, completion: nil)
+            }
+        })
     }
 
     func didFinishAudioAtPath(path: String) {