소스 검색

inital document picker implementation (#376)

cyberta 5 년 전
부모
커밋
e3da94d3c2
3개의 변경된 파일47개의 추가작업 그리고 1개의 파일을 삭제
  1. 19 0
      deltachat-ios/Controller/ChatViewController.swift
  2. 4 0
      deltachat-ios/Coordinator/AppCoordinator.swift
  3. 24 1
      deltachat-ios/Helper/MediaPicker.swift

+ 19 - 0
deltachat-ios/Controller/ChatViewController.swift

@@ -30,6 +30,11 @@ extension ChatViewController: MediaPickerDelegate {
     func onVoiceMessageRecorded(url: NSURL) {
         sendVoiceMessage(url: url)
     }
+
+    func onDocumentSelected(url: NSURL) {
+        sendDocumentMessage(url: url)
+    }
+
 }
 
 class ChatViewController: MessagesViewController {
@@ -883,6 +888,14 @@ extension ChatViewController: MessagesDataSource {
         }
     }
 
+    private func sendDocumentMessage(url: NSURL) {
+        DispatchQueue.global().async {
+            let msg = DcMsg(viewType: DC_MSG_FILE)
+            msg.setFile(filepath: url.relativePath, mimeType: nil)
+            msg.sendInChat(id: self.chatId)
+        }
+    }
+
     private func sendVoiceMessage(url: NSURL) {
         DispatchQueue.global().async {
             let msg = DcMsg(viewType: DC_MSG_VOICE)
@@ -1047,15 +1060,21 @@ extension ChatViewController: MessagesLayoutDelegate {
         let alert = UIAlertController(title: nil, message: nil, preferredStyle: .safeActionSheet)
         let galleryAction = PhotoPickerAlertAction(title: String.localized("gallery"), style: .default, handler: galleryButtonPressed(_:))
         let cameraAction = PhotoPickerAlertAction(title: String.localized("camera"), style: .default, handler: cameraButtonPressed(_:))
+        let documentAction = UIAlertAction(title: String.localized("documents"), style: .default, handler: documentActionPressed(_:))
         let voiceMessageAction = UIAlertAction(title: String.localized("voice_message"), style: .default, handler: voiceMessageButtonPressed(_:))
 
         alert.addAction(cameraAction)
         alert.addAction(galleryAction)
+        alert.addAction(documentAction)
         alert.addAction(voiceMessageAction)
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
 
+    private func documentActionPressed(_ action: UIAlertAction) {
+        coordinator?.showDocumentLibrary(delegate: self)
+    }
+
     private func voiceMessageButtonPressed(_ action: UIAlertAction) {
         coordinator?.showVoiceMessageRecorder(delegate: self)
     }

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

@@ -441,6 +441,10 @@ class ChatViewCoordinator: NSObject, Coordinator {
         navigationController.pushViewController(chatViewController, animated: true)
     }
 
+    func showDocumentLibrary(delegate: MediaPickerDelegate) {
+        mediaPicker.showDocumentLibrary(delegate: delegate)
+    }
+
     func showVoiceMessageRecorder(delegate: MediaPickerDelegate) {
         mediaPicker.showVoiceRecorder(delegate: delegate)
     }

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

@@ -8,6 +8,7 @@ protocol MediaPickerDelegate: class {
     func onImageSelected(url: NSURL)
     func onVideoSelected(url: NSURL)
     func onVoiceMessageRecorded(url: NSURL)
+    func onDocumentSelected(url: NSURL)
 }
 
 extension MediaPickerDelegate {
@@ -20,9 +21,12 @@ extension MediaPickerDelegate {
     func onVoiceMessageRecorded(url: NSURL) {
         logger.debug("voice message recorded: \(url)")
     }
+    func onDocumentSelected(url: NSURL) {
+        logger.debug("document selected: \(url)")
+    }
 }
 
-class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate, AudioRecorderControllerDelegate {
+class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate, AudioRecorderControllerDelegate, UIDocumentPickerDelegate {
 
     private let navigationController: UINavigationController
     private weak var delegate: MediaPickerDelegate?
@@ -60,6 +64,25 @@ class MediaPicker: NSObject, UINavigationControllerDelegate, UIImagePickerContro
         }
     }
 
+    func showDocumentLibrary(delegate: MediaPickerDelegate) {
+        let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet, kUTTypeVCard, kUTTypeZipArchive]
+        let documentPicker = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
+        documentPicker.delegate = self
+        documentPicker.allowsMultipleSelection = false
+        documentPicker.modalPresentationStyle = .formSheet
+        self.delegate = delegate
+        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)
+    }
+
+    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
+        logger.debug("documentPicker was cancelled")
+    }
+
     private func presentPhotoVideoLibrary(delegate: MediaPickerDelegate) {
         if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
             let videoPicker = UIImagePickerController()