瀏覽代碼

Merge pull request #853 from deltachat/video_recording

Video recording
cyBerta 5 年之前
父節點
當前提交
46e4f15c2d

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

@@ -114,7 +114,9 @@ class ChatViewController: MessagesViewController {
     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 = {
@@ -749,8 +751,8 @@ class ChatViewController: MessagesViewController {
         mediaPicker?.showVoiceRecorder(delegate: delegate)
     }
 
-    private func showCameraViewController(delegate: MediaPickerDelegate) {
-        mediaPicker?.showCamera(delegate: delegate, allowCropping: false)
+    private func showCameraViewController() {
+       mediaPicker?.showCamera()
     }
 
     private func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
@@ -1271,7 +1273,7 @@ extension ChatViewController: MessagesLayoutDelegate {
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        showCameraViewController(delegate: self)
+        showCameraViewController()
     }
 
     private func galleryButtonPressed(_ action: UIAlertAction) {
@@ -1410,7 +1412,6 @@ extension ChatViewController: MessageCellDelegate {
         }
     }
 
-
     func didStartAudio(in cell: AudioMessageCell) {
         print("audio started")
     }
@@ -1423,7 +1424,6 @@ extension ChatViewController: MessageCellDelegate {
         print("audio paused")
     }
 
-
     @objc func didTapBackground(in cell: MessageCollectionViewCell) {
         print("background of message tapped")
     }
@@ -1513,7 +1513,6 @@ extension MessageCollectionViewCell {
         }
     }
 
-
     @objc func messageDelete(_ sender: Any?) {
         // Get the collectionView
         if let collectionView = self.superview as? UICollectionView {

+ 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(allowCropping: true, supportedMediaTypes: .photo)
     }
 
     func onImageSelected(image: UIImage) {

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

@@ -21,7 +21,9 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     private let tagAccountSettingsCell = 1
 
     private lazy var mediaPicker: MediaPicker? = {
-        return MediaPicker(navigationController: navigationController)
+        let mediaPicker = MediaPicker(navigationController: navigationController)
+        mediaPicker.delegate = self
+        return mediaPicker
     }()
 
     private lazy var statusCell: MultilineTextFieldCell = {
@@ -127,7 +129,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showCamera(delegate: self)
+        mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo)
     }
 
     private func deleteProfileIconPressed(_ action: UIAlertAction) {

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

@@ -26,7 +26,9 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     private let sectionGroupMembers = 2
 
     private lazy var mediaPicker: MediaPicker? = {
-        return MediaPicker(navigationController: navigationController)
+        let mediaPicker = MediaPicker(navigationController: navigationController)
+        mediaPicker.delegate = self
+        return mediaPicker
     }()
 
     lazy var groupNameCell: TextFieldCell = {
@@ -348,7 +350,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
 
     private func showCamera(delegate: MediaPickerDelegate) {
-        mediaPicker?.showCamera(delegate: delegate)
+        mediaPicker?.showCamera(allowCropping: true, supportedMediaTypes: .photo)
     }
 
     private func showQrCodeInvite(chatId: Int) {

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

@@ -7,7 +7,9 @@ class ProfileInfoViewController: UITableViewController {
     private var displayName: String?
 
     private lazy var mediaPicker: MediaPicker? = {
-        return MediaPicker(navigationController: navigationController)
+        let mediaPicker = MediaPicker(navigationController: navigationController)
+        mediaPicker.delegate = self
+        return mediaPicker
     }()
 
     private lazy var doneButtonItem: UIBarButtonItem = {
@@ -131,7 +133,7 @@ class ProfileInfoViewController: UITableViewController {
     }
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showCamera(delegate: self)
+        mediaPicker?.showCamera()
     }
 }
 

+ 63 - 40
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(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)
             }))
@@ -148,28 +156,43 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         }
     }
 
-    func showCamera(delegate: MediaPickerDelegate) {
-        showCamera(delegate: delegate, allowCropping: true)
+    func showCamera() {
+        showCamera(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) {