Эх сурвалжийг харах

load videos and photos from photo library, move sending of media files to chat view controller

cyberta 5 жил өмнө
parent
commit
5ed3c4ef1a

+ 30 - 5
deltachat-ios/Controller/ChatViewController.swift

@@ -15,6 +15,14 @@ extension ChatViewController: MediaSendHandler {
 }
 }
 
 
 extension ChatViewController: MediaPickerDelegate {
 extension ChatViewController: MediaPickerDelegate {
+    func onVideoSelected(url: NSURL) {
+        sendVideo(url: url)
+    }
+
+    func onImageSelected(url: NSURL) {
+        sendImage(url: url)
+    }
+
     func onImageSelected(image: UIImage) {
     func onImageSelected(image: UIImage) {
         sendImage(image)
         sendImage(image)
     }
     }
@@ -754,6 +762,23 @@ extension ChatViewController: MessagesDataSource {
         }
         }
     }
     }
 
 
+    private func sendVideo(url: NSURL) {
+        let msg = dc_msg_new(mailboxPointer, DC_MSG_VIDEO)
+        if let path = url.relativePath?.cString(using: .utf8) { //absoluteString?.cString(using: .utf8) {
+            dc_msg_set_file(msg, path, "video/mov")
+            dc_send_msg(mailboxPointer, UInt32(chatId), msg)
+            dc_msg_unref(msg)
+        }
+    }
+
+    private func sendImage(url: NSURL) {
+        if let data = try? Data(contentsOf: url as URL) {
+            if let image = UIImage(data: data) {
+                sendImage(image)
+            }
+        }
+    }
+
     func isLastSectionVisible() -> Bool {
     func isLastSectionVisible() -> Bool {
         guard !messageList.isEmpty else { return false }
         guard !messageList.isEmpty else { return false }
 
 
@@ -892,8 +917,8 @@ extension ChatViewController: MessagesLayoutDelegate {
 
 
     private func showClipperOptions() {
     private func showClipperOptions() {
         let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
         let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
-        let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: videoButtonPressed(_:))
-        let photoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: photoButtonPressed(_:))
+        let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
+        let photoAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
 
 
         alert.addAction(photoAction)
         alert.addAction(photoAction)
         alert.addAction(galleryAction)
         alert.addAction(galleryAction)
@@ -901,12 +926,12 @@ extension ChatViewController: MessagesLayoutDelegate {
         self.present(alert, animated: true, completion: nil)
         self.present(alert, animated: true, completion: nil)
     }
     }
 
 
-    private func photoButtonPressed(_ action: UIAlertAction) {
+    private func cameraButtonPressed(_ action: UIAlertAction) {
         coordinator?.showCameraViewController(delegate: self)
         coordinator?.showCameraViewController(delegate: self)
     }
     }
 
 
-    private func videoButtonPressed(_ action: UIAlertAction) {
-        coordinator?.showVideoLibrary()
+    private func galleryButtonPressed(_ action: UIAlertAction) {
+        coordinator?.showPhotoVideoLibrary(delegate: self)
     }
     }
 
 
 }
 }

+ 5 - 53
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -240,7 +240,7 @@ class EditSettingsCoordinator: Coordinator {
     }
     }
 
 
     func showPhotoPicker(delegate: MediaPickerDelegate) {
     func showPhotoPicker(delegate: MediaPickerDelegate) {
-        mediaPicker.showGallery(delegate: delegate)
+        mediaPicker.showPhotoGallery(delegate: delegate)
     }
     }
 
 
     func showCamera(delegate: MediaPickerDelegate) {
     func showCamera(delegate: MediaPickerDelegate) {
@@ -378,8 +378,6 @@ class GroupChatDetailCoordinator: Coordinator {
         navigationController.pushViewController(contactDetailController, animated: true)
         navigationController.pushViewController(contactDetailController, animated: true)
     }
     }
 
 
-
-
 }
 }
 
 
 class ChatViewCoordinator: NSObject, Coordinator {
 class ChatViewCoordinator: NSObject, Coordinator {
@@ -439,15 +437,6 @@ class ChatViewCoordinator: NSObject, Coordinator {
         navigationController.pushViewController(chatViewController, animated: true)
         navigationController.pushViewController(chatViewController, animated: true)
     }
     }
 
 
-    private func sendVideo(url: NSURL) {
-        let msg = dc_msg_new(mailboxPointer, DC_MSG_VIDEO)
-        if let path = url.relativePath?.cString(using: .utf8) { //absoluteString?.cString(using: .utf8) {
-            dc_msg_set_file(msg, path, "video/mov")
-            dc_send_msg(mailboxPointer, UInt32(chatId), msg)
-            dc_msg_unref(msg)
-        }
-    }
-
     private func handleMediaMessageSuccess() {
     private func handleMediaMessageSuccess() {
         if let chatViewController = self.navigationController.visibleViewController as? MediaSendHandler {
         if let chatViewController = self.navigationController.visibleViewController as? MediaSendHandler {
             DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
             DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
@@ -460,45 +449,8 @@ class ChatViewCoordinator: NSObject, Coordinator {
         mediaPicker.showCamera(delegate: delegate, allowCropping: false)
         mediaPicker.showCamera(delegate: delegate, allowCropping: false)
     }
     }
 
 
-    func showVideoLibrary() {
-        if PHPhotoLibrary.authorizationStatus() != .authorized {
-            PHPhotoLibrary.requestAuthorization { status in
-                DispatchQueue.main.async {
-                    [weak self] in
-                    switch status {
-                    case  .denied, .notDetermined, .restricted:
-                        print("denied")
-                    case .authorized:
-                        self?.presentVideoLibrary()
-                    }
-                }
-            }
-        } else {
-            presentVideoLibrary()
-        }
-    }
-
-    private func presentVideoLibrary() {
-        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
-            let videoPicker = UIImagePickerController()
-            videoPicker.title = String.localized("video")
-            videoPicker.delegate = self
-            videoPicker.sourceType = .photoLibrary
-            videoPicker.mediaTypes = [kUTTypeMovie as String, kUTTypeVideo as String]
-            navigationController.present(videoPicker, animated: true, completion: nil)
-        }
-    }
-}
-
-extension ChatViewCoordinator: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
-
-    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
-        if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {
-            sendVideo(url: videoUrl)
-        }
-        navigationController.dismiss(animated: true) {
-            self.handleMediaMessageSuccess()
-        }
+    func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
+        mediaPicker.showPhotoVideoLibrary(delegate: delegate)
     }
     }
 }
 }
 
 
@@ -567,7 +519,7 @@ class GroupNameCoordinator: Coordinator {
     }
     }
 
 
     func showPhotoPicker(delegate: MediaPickerDelegate) {
     func showPhotoPicker(delegate: MediaPickerDelegate) {
-          mediaPicker.showGallery(delegate: delegate)
+          mediaPicker.showPhotoGallery(delegate: delegate)
       }
       }
 
 
       func showCamera(delegate: MediaPickerDelegate) {
       func showCamera(delegate: MediaPickerDelegate) {
@@ -617,7 +569,7 @@ class EditGroupCoordinator: Coordinator {
     }
     }
 
 
     func showPhotoPicker(delegate: MediaPickerDelegate) {
     func showPhotoPicker(delegate: MediaPickerDelegate) {
-        mediaPicker.showGallery(delegate: delegate)
+        mediaPicker.showPhotoGallery(delegate: delegate)
     }
     }
 
 
     func showCamera(delegate: MediaPickerDelegate) {
     func showCamera(delegate: MediaPickerDelegate) {

+ 54 - 1
deltachat-ios/Helper/MediaPicker.swift

@@ -5,6 +5,17 @@ import ALCameraViewController
 
 
 protocol MediaPickerDelegate: class {
 protocol MediaPickerDelegate: class {
     func onImageSelected(image: UIImage)
     func onImageSelected(image: UIImage)
+    func onImageSelected(url: NSURL)
+    func onVideoSelected(url: NSURL)
+}
+
+extension MediaPickerDelegate {
+    func onImageSelected(url: NSURL) {
+        logger.debug("image selected: ", url.path ?? "unknown")
+    }
+    func onVideoSelected(url: NSURL) {
+        logger.debug("video selected: ", url.path ?? "unknown")
+    }
 }
 }
 
 
 class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
 class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@@ -15,7 +26,40 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         self.navigationController = navigationController
         self.navigationController = navigationController
     }
     }
 
 
-    func showGallery(delegate: MediaPickerDelegate) {
+
+    func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
+        if PHPhotoLibrary.authorizationStatus() != .authorized {
+            PHPhotoLibrary.requestAuthorization { status in
+                DispatchQueue.main.async {
+                    [weak self] in
+                    switch status {
+                    case  .denied, .notDetermined, .restricted:
+                        print("denied")
+                    case .authorized:
+                        self?.presentPhotoVideoLibrary(delegate: delegate)
+                    }
+                }
+            }
+        } else {
+            presentPhotoVideoLibrary(delegate: delegate)
+        }
+    }
+
+    private func presentPhotoVideoLibrary(delegate: MediaPickerDelegate) {
+        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
+            let videoPicker = UIImagePickerController()
+            videoPicker.title = String.localized("gallery")
+            videoPicker.delegate = self
+            videoPicker.sourceType = .photoLibrary
+            videoPicker.mediaTypes = [kUTTypeMovie as String,
+                                      kUTTypeVideo as String,
+                                      kUTTypeImage as String]
+            self.delegate = delegate
+            navigationController.present(videoPicker, animated: true, completion: nil)
+        }
+    }
+
+    func showPhotoGallery(delegate: MediaPickerDelegate) {
         let croppingParameters = CroppingParameters(isEnabled: true,
         let croppingParameters = CroppingParameters(isEnabled: true,
                                                     allowResizing: true,
                                                     allowResizing: true,
                                                     allowMoving: true,
                                                     allowMoving: true,
@@ -65,4 +109,13 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         showCamera(delegate: delegate, allowCropping: true)
         showCamera(delegate: delegate, allowCropping: true)
     }
     }
 
 
+    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
+        if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {
+            self.delegate?.onVideoSelected(url: videoUrl)
+        } else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? NSURL {
+            self.delegate?.onImageSelected(url: imageUrl)
+        }
+        navigationController.dismiss(animated: true, completion: nil)
+    }
+
 }
 }