Przeglądaj źródła

Merge pull request #485 from deltachat/document_picker

Document picker
cyBerta 5 lat temu
rodzic
commit
70294c3e21

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

@@ -79,7 +79,7 @@ class AccountSetupController: UITableViewController {
 
     private func showProgressHud(title: String) {
         configProgressAlert.actions[0].isEnabled = true
-        configProgressAlert.title = title;
+        configProgressAlert.title = title
         configProgressAlert.message = String.localized("one_moment")
         present(configProgressAlert, animated: true, completion: nil)
     }

+ 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)
     }

+ 20 - 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,21 @@ 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)
+    }
+
     private func presentPhotoVideoLibrary(delegate: MediaPickerDelegate) {
         if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
             let videoPicker = UIImagePickerController()

+ 4 - 0
deltachat-ios/deltachat-ios.entitlements

@@ -6,6 +6,10 @@
 	<string>development</string>
 	<key>com.apple.developer.associated-domains</key>
 	<array/>
+	<key>com.apple.developer.icloud-container-identifiers</key>
+	<array/>
+	<key>com.apple.developer.ubiquity-kvstore-identifier</key>
+	<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
 	<key>com.apple.security.application-groups</key>
 	<array>
 		<string>group.eu.merlinux.group.chat.delta.ios</string>