Browse Source

Merge pull request #858 from deltachat/nativ_image_picker

Native image picker
bjoern 5 năm trước cách đây
mục cha
commit
a392805649

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

@@ -743,12 +743,12 @@ class ChatViewController: MessagesViewController {
         }
         }
     }
     }
 
 
-    private func showDocumentLibrary(delegate: MediaPickerDelegate) {
-        mediaPicker?.showDocumentLibrary(delegate: delegate)
+    private func showDocumentLibrary() {
+        mediaPicker?.showDocumentLibrary()
     }
     }
 
 
-    private func showVoiceMessageRecorder(delegate: MediaPickerDelegate) {
-        mediaPicker?.showVoiceRecorder(delegate: delegate)
+    private func showVoiceMessageRecorder() {
+        mediaPicker?.showVoiceRecorder()
     }
     }
 
 
     private func showCameraViewController() {
     private func showCameraViewController() {
@@ -756,7 +756,7 @@ class ChatViewController: MessagesViewController {
     }
     }
 
 
     private func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
     private func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
-        mediaPicker?.showPhotoVideoLibrary(delegate: delegate)
+        mediaPicker?.showPhotoVideoLibrary()
     }
     }
 
 
     private func showMediaGallery(currentIndex: Int, mediaUrls urls: [URL]) {
     private func showMediaGallery(currentIndex: Int, mediaUrls urls: [URL]) {
@@ -1265,11 +1265,11 @@ extension ChatViewController: MessagesLayoutDelegate {
     }
     }
 
 
     private func documentActionPressed(_ action: UIAlertAction) {
     private func documentActionPressed(_ action: UIAlertAction) {
-        showDocumentLibrary(delegate: self)
+        showDocumentLibrary()
     }
     }
 
 
     private func voiceMessageButtonPressed(_ action: UIAlertAction) {
     private func voiceMessageButtonPressed(_ action: UIAlertAction) {
-        showVoiceMessageRecorder(delegate: self)
+        showVoiceMessageRecorder()
     }
     }
 
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
     private func cameraButtonPressed(_ action: UIAlertAction) {

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

@@ -101,7 +101,7 @@ class EditGroupViewController: UITableViewController, MediaPickerDelegate {
     }
     }
 
 
     private func galleryButtonPressed(_ action: UIAlertAction) {
     private func galleryButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showPhotoGallery(delegate: self)
+        mediaPicker?.showPhotoGallery()
     }
     }
 
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
     private func cameraButtonPressed(_ action: UIAlertAction) {

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

@@ -125,7 +125,7 @@ class EditSettingsController: UITableViewController, MediaPickerDelegate {
 
 
     // MARK: - actions
     // MARK: - actions
     private func galleryButtonPressed(_ action: UIAlertAction) {
     private func galleryButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showPhotoGallery(delegate: self)
+        mediaPicker?.showPhotoGallery()
     }
     }
 
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
     private func cameraButtonPressed(_ action: UIAlertAction) {

+ 0 - 1
deltachat-ios/Controller/NewChatViewController.swift

@@ -1,4 +1,3 @@
-import KK_ALCameraViewController
 import Contacts
 import Contacts
 import UIKit
 import UIKit
 import DcCore
 import DcCore

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

@@ -346,7 +346,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
     }
 
 
     private func showPhotoPicker(delegate: MediaPickerDelegate) {
     private func showPhotoPicker(delegate: MediaPickerDelegate) {
-        mediaPicker?.showPhotoGallery(delegate: delegate)
+        mediaPicker?.showPhotoGallery()
     }
     }
 
 
     private func showCamera(delegate: MediaPickerDelegate) {
     private func showCamera(delegate: MediaPickerDelegate) {

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

@@ -123,7 +123,7 @@ class ProfileInfoViewController: UITableViewController {
     }
     }
 
 
     private func galleryButtonPressed(_ action: UIAlertAction) {
     private func galleryButtonPressed(_ action: UIAlertAction) {
-        mediaPicker?.showPhotoGallery(delegate: self)
+        mediaPicker?.showPhotoGallery()
     }
     }
 
 
     private func cameraButtonPressed(_ action: UIAlertAction) {
     private func cameraButtonPressed(_ action: UIAlertAction) {

+ 0 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -1,5 +1,4 @@
 import UIKit
 import UIKit
-import KK_ALCameraViewController
 import Photos
 import Photos
 import MobileCoreServices
 import MobileCoreServices
 import DcCore
 import DcCore

+ 38 - 47
deltachat-ios/Helper/MediaPicker.swift

@@ -1,7 +1,6 @@
 import UIKit
 import UIKit
 import Photos
 import Photos
 import MobileCoreServices
 import MobileCoreServices
-import KK_ALCameraViewController
 
 
 protocol MediaPickerDelegate: class {
 protocol MediaPickerDelegate: class {
     func onImageSelected(image: UIImage)
     func onImageSelected(image: UIImage)
@@ -26,7 +25,7 @@ extension MediaPickerDelegate {
     }
     }
 }
 }
 
 
-class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate, AudioRecorderControllerDelegate, UIDocumentPickerDelegate {
+class MediaPicker: NSObject, UINavigationControllerDelegate, AudioRecorderControllerDelegate {
 
 
     enum CameraMediaTypes {
     enum CameraMediaTypes {
         case photo
         case photo
@@ -46,8 +45,7 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         self.navigationController = navigationController
         self.navigationController = navigationController
     }
     }
 
 
-    func showVoiceRecorder(delegate: MediaPickerDelegate) {
-        self.delegate = delegate
+    func showVoiceRecorder() {
         let audioRecorderController = AudioRecorderController()
         let audioRecorderController = AudioRecorderController()
         audioRecorderController.delegate = self
         audioRecorderController.delegate = self
         //audioRecorderController.maximumRecordDuration = 1200
         //audioRecorderController.maximumRecordDuration = 1200
@@ -56,7 +54,7 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         navigationController?.present(audioRecorderNavController, animated: true, completion: nil)
         navigationController?.present(audioRecorderNavController, animated: true, completion: nil)
     }
     }
 
 
-    func showPhotoVideoLibrary(delegate: MediaPickerDelegate) {
+    func showPhotoVideoLibrary() {
         if PHPhotoLibrary.authorizationStatus() != .authorized {
         if PHPhotoLibrary.authorizationStatus() != .authorized {
             PHPhotoLibrary.requestAuthorization { status in
             PHPhotoLibrary.requestAuthorization { status in
                 DispatchQueue.main.async {
                 DispatchQueue.main.async {
@@ -65,64 +63,47 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
                     case  .denied, .notDetermined, .restricted:
                     case  .denied, .notDetermined, .restricted:
                         print("denied")
                         print("denied")
                     case .authorized:
                     case .authorized:
-                        self?.presentPhotoVideoLibrary(delegate: delegate)
+                        self?.presentPhotoVideoLibrary()
                     }
                     }
                 }
                 }
             }
             }
         } else {
         } else {
-            presentPhotoVideoLibrary(delegate: delegate)
+            presentPhotoVideoLibrary()
         }
         }
     }
     }
 
 
-    func showDocumentLibrary(delegate: MediaPickerDelegate) {
+    func showDocumentLibrary() {
         let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet, kUTTypeVCard, kUTTypeZipArchive, kUTTypeImage]
         let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet, kUTTypeVCard, kUTTypeZipArchive, kUTTypeImage]
         let documentPicker = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
         let documentPicker = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
         documentPicker.delegate = self
         documentPicker.delegate = self
         documentPicker.allowsMultipleSelection = false
         documentPicker.allowsMultipleSelection = false
         documentPicker.modalPresentationStyle = .formSheet
         documentPicker.modalPresentationStyle = .formSheet
-        self.delegate = delegate
         navigationController?.present(documentPicker, animated: true, completion: nil)
         navigationController?.present(documentPicker, animated: true, completion: nil)
     }
     }
 
 
-    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
-        let url = urls[0] as NSURL
-        self.delegate?.onDocumentSelected(url: url)
+    private func presentPhotoVideoLibrary() {
+        let mediaTypes = [
+            kUTTypeMovie as String,
+            kUTTypeVideo as String,
+            kUTTypeImage as String
+        ]
+        showPhotoLibrary(allowsCropping: false, mediaTypes: mediaTypes)
     }
     }
 
 
-    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() {
+        let mediaType = [kUTTypeImage as String]
+        showPhotoLibrary(allowsCropping: true, mediaTypes: mediaType) // used mainly for avatar-selection, allow cropping therefore
     }
     }
 
 
-    func showPhotoGallery(delegate: MediaPickerDelegate) {
-        let croppingParameters = CroppingParameters(
-            isEnabled: true,
-            allowResizing: true,
-            allowMoving: true,
-            minimumSize: CGSize(width: 70, height: 70)
-        )
-
-        let controller = CameraViewController.imagePickerViewController(
-            croppingParameters: croppingParameters,
-            completion: { [weak self] image, _ in
-                if let image = image {
-                    self?.delegate?.onImageSelected(image: image)
-                }
-                self?.navigationController?.dismiss(animated: true, completion: nil)})
-        self.delegate = delegate
-        controller.modalPresentationStyle = .fullScreen
-        navigationController?.present(controller, animated: true, completion: nil)
+    private func showPhotoLibrary(allowsCropping: Bool, mediaTypes: [String]) {
+        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
+            let imagePickerController = UIImagePickerController()
+            imagePickerController.delegate = self
+            imagePickerController.sourceType = .photoLibrary
+            imagePickerController.mediaTypes = mediaTypes
+            imagePickerController.allowsEditing = allowsCropping
+            navigationController?.present(imagePickerController, animated: true, completion: nil)
+        }
     }
     }
 
 
     func showCamera(allowCropping: Bool, supportedMediaTypes: CameraMediaTypes) {
     func showCamera(allowCropping: Bool, supportedMediaTypes: CameraMediaTypes) {
@@ -137,9 +118,7 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
             case .allAvailable:
             case .allAvailable:
                 mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera) ?? []
                 mediaTypes = UIImagePickerController.availableMediaTypes(for: .camera) ?? []
             }
             }
-            if allowCropping {
-                imagePickerController.allowsEditing = true
-            }
+            imagePickerController.allowsEditing = allowCropping
             imagePickerController.mediaTypes = mediaTypes
             imagePickerController.mediaTypes = mediaTypes
             imagePickerController.setEditing(true, animated: true)
             imagePickerController.setEditing(true, animated: true)
             navigationController?.present(imagePickerController, animated: true, completion: nil)
             navigationController?.present(imagePickerController, animated: true, completion: nil)
@@ -160,6 +139,11 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         showCamera(allowCropping: false, supportedMediaTypes: .allAvailable)
         showCamera(allowCropping: false, supportedMediaTypes: .allAvailable)
     }
     }
 
 
+}
+
+// MARK: - UIImagePickerControllerDelegate
+extension MediaPicker: UIImagePickerControllerDelegate {
+
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
 
 
         if let type = info[.mediaType] as? String, let mediaType = PickerMediaType(rawValue: type) {
         if let type = info[.mediaType] as? String, let mediaType = PickerMediaType(rawValue: type) {
@@ -201,3 +185,10 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
     }
     }
 
 
 }
 }
+
+extension MediaPicker: UIDocumentPickerDelegate {
+    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+        let url = urls[0] as NSURL
+        self.delegate?.onDocumentSelected(url: url)
+    }
+}