Sfoglia il codice sorgente

Merge pull request #17 from deltachat/view_images

Display images
Jonas Reinsch 6 anni fa
parent
commit
1bdaa14a47

+ 2 - 2
deltachat-ios.xcodeproj/project.pbxproj

@@ -813,7 +813,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				CLANG_ENABLE_MODULES = YES;
 				CODE_SIGN_STYLE = Automatic;
-				DEVELOPMENT_TEAM = S595XJ38G3;
+				DEVELOPMENT_TEAM = 9M4M75R59J;
 				HEADER_SEARCH_PATHS = "deltachat-ios/libraries/deltachat-core/libs/netpgp/include";
 				INFOPLIST_FILE = "deltachat-ios/Info.plist";
 				IPHONEOS_DEPLOYMENT_TARGET = 11.0;
@@ -839,7 +839,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				CLANG_ENABLE_MODULES = YES;
 				CODE_SIGN_STYLE = Automatic;
-				DEVELOPMENT_TEAM = S595XJ38G3;
+				DEVELOPMENT_TEAM = 9M4M75R59J;
 				HEADER_SEARCH_PATHS = "deltachat-ios/libraries/deltachat-core/libs/netpgp/include";
 				INFOPLIST_FILE = "deltachat-ios/Info.plist";
 				IPHONEOS_DEPLOYMENT_TARGET = 11.0;

+ 4 - 2
deltachat-ios/AppDelegate.swift

@@ -11,6 +11,8 @@ import AudioToolbox
 
 
 var mailboxPointer:UnsafeMutablePointer<dc_context_t>!
+let dc_notificationChanged = Notification.Name(rawValue:"MrEventMsgsChanged")
+let dc_notificationIncoming = Notification.Name(rawValue:"MrEventIncomingMsg")
 
 @_silgen_name("callbackSwift")
 
@@ -79,7 +81,7 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
         let nc = NotificationCenter.default
         
         DispatchQueue.main.async {
-            nc.post(name:Notification.Name(rawValue:"MrEventMsgsChanged"),
+            nc.post(name:dc_notificationChanged,
                     object: nil,
                     userInfo: ["message":"Messages Changed!", "date":Date()])
         }
@@ -89,7 +91,7 @@ public func callbackSwift(event: CInt, data1: CUnsignedLong, data2: CUnsignedLon
         // mrmailbox_get_fresh_msgs
         let nc = NotificationCenter.default
         DispatchQueue.main.async {
-            nc.post(name:Notification.Name(rawValue:"MrEventIncomingMsg"),
+            nc.post(name:dc_notificationIncoming,
                     object: nil,
                     userInfo: ["message":"Incoming Message!", "date":Date()])
         }

+ 7 - 7
deltachat-ios/ChatListController.swift

@@ -34,19 +34,22 @@ class ChatListController: UIViewController {
     }
     
     override func viewWillAppear(_ animated: Bool) {
+        super.viewWillAppear(animated)
+        
         getChatList()
     }
     
     override func viewDidAppear(_ animated: Bool) {
+        super.viewDidAppear(animated)
         let nc = NotificationCenter.default
-        msgChangedObserver = nc.addObserver(forName:Notification.Name(rawValue:"MrEventMsgsChanged"),
+        msgChangedObserver = nc.addObserver(forName:dc_notificationChanged,
                                             object:nil, queue:nil) {
                                                 notification in
                                                 print("----------- MrEventMsgsChanged notification received --------")
                                                 self.getChatList()
         }
         
-        incomingMsgObserver = nc.addObserver(forName:Notification.Name(rawValue:"MrEventIncomingMsg"),
+        incomingMsgObserver = nc.addObserver(forName:dc_notificationIncoming,
                                              object:nil, queue:nil) {
                                                 notification in
                                                 print("----------- MrEventIncomingMsg received --------")
@@ -55,6 +58,8 @@ class ChatListController: UIViewController {
     }
     
     override func viewWillDisappear(_ animated: Bool) {
+        super.viewWillDisappear(animated)
+        
         let nc = NotificationCenter.default
         if let msgChangedObserver = self.msgChangedObserver {
             nc.removeObserver(msgChangedObserver)
@@ -129,11 +134,6 @@ actionSheet.addAction(UIAlertAction(title: "Scan QR code",
         let nav = UINavigationController(rootViewController: ncv)
         present(nav, animated: true, completion: nil)
     }
-    
-    override func didReceiveMemoryWarning() {
-        super.didReceiveMemoryWarning()
-        // Dispose of any resources that can be recreated.
-    }
 }
 
 extension ChatListController: ChatPresenter {

+ 29 - 16
deltachat-ios/ChatViewController.swift

@@ -59,10 +59,12 @@ class ChatViewController: MessagesViewController {
         }
     }
     
-    override func viewDidAppear(_ animated: Bool) {
+    override func viewWillAppear(_ animated: Bool) {
+        super.viewWillAppear(animated)
+        
         let nc = NotificationCenter.default
-        msgChangedObserver = nc.addObserver(forName:Notification.Name(rawValue:"MrEventMsgsChanged"),
-                                            object:nil, queue:nil) {
+        msgChangedObserver = nc.addObserver(forName:dc_notificationChanged,
+                                            object:nil, queue: OperationQueue.main) {
                                                 notification in
                                                 print("----------- MrEventMsgsChanged notification received --------")
                                                 self.getMessageIds()
@@ -70,8 +72,8 @@ class ChatViewController: MessagesViewController {
                                                 self.messagesCollectionView.scrollToBottom()
         }
         
-        incomingMsgObserver = nc.addObserver(forName:Notification.Name(rawValue:"MrEventIncomingMsg"),
-                                             object:nil, queue:nil) {
+        incomingMsgObserver = nc.addObserver(forName:dc_notificationIncoming,
+                                             object:nil, queue: OperationQueue.main) {
                                                 notification in
                                                 print("----------- MrEventIncomingMsg received --------")
                                                 self.getMessageIds()
@@ -87,6 +89,8 @@ class ChatViewController: MessagesViewController {
     }
     
     override func viewWillDisappear(_ animated: Bool) {
+        super.viewDidDisappear(animated)
+        
         setTextDraft()
         let nc = NotificationCenter.default
         if let msgChangedObserver = self.msgChangedObserver {
@@ -288,13 +292,17 @@ extension ChatViewController: MessagesDataSource {
     }
     
     func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
-        let section = indexPath.section
-        let messageId = messageIds[section]
+        let row = indexPath.row
+        let messageId = messageIds[row]
         let message = MRMessage(id: messageId)
         let contact = MRContact(id: message.fromContactId)
         
         let sender = Sender(id: "\(contact.id)", displayName: contact.name)
-        return Message(text: message.text ?? "- empty -", sender: sender, messageId: "\(messageId)", date: Date(timeIntervalSince1970: Double(message.timestamp)))
+        if let image = message.image {
+            return Message(image: image, sender: sender, messageId: "\(messageId)", date: Date(timeIntervalSince1970: Double(message.timestamp)))
+        } else {
+            return Message(text: message.text ?? "- empty -", sender: sender, messageId: "\(messageId)", date: Date(timeIntervalSince1970: Double(message.timestamp)))
+        }
     }
     
     func avatar(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> Avatar {
@@ -408,7 +416,8 @@ extension ChatViewController: MessagesLayoutDelegate {
         }
         
         do {
-            let path = directory.appendingPathComponent("attachment.jpg")
+            let timestamp = Int(Date().timeIntervalSince1970) 
+            let path = directory.appendingPathComponent("\(chatId)_\(timestamp).jpg")
             try data.write(to: path!)
             return path?.relativePath
         } catch {
@@ -420,12 +429,15 @@ extension ChatViewController: MessagesLayoutDelegate {
 
 extension ChatViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
     func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
-        if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage,
-            let width = Int32(exactly: pickedImage.size.width),
-            let height = Int32(exactly: pickedImage.size.height),
-            let path = saveImage(image: pickedImage) {
-            dc_send_image_msg(mailboxPointer, UInt32(self.chatId), path, "image/jpeg", width, height)
+        DispatchQueue.global().async {
+            if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage,
+                let width = Int32(exactly: pickedImage.size.width),
+                let height = Int32(exactly: pickedImage.size.height),
+                let path = self.saveImage(image: pickedImage) {
+                dc_send_image_msg(mailboxPointer, UInt32(self.chatId), path, "image/jpeg", width, height)
+            }
         }
+        
         dismiss(animated: true, completion: nil)
     }
 }
@@ -507,8 +519,9 @@ extension ChatViewController: MessageInputBarDelegate {
     
     func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
 //        messageList.append(Message(text: text, sender: currentSender(), messageId: UUID().uuidString, date: Date()))
-        
-        dc_send_text_msg(mailboxPointer, UInt32(self.chatId), text)
+        DispatchQueue.global().async {
+            dc_send_text_msg(mailboxPointer, UInt32(self.chatId), text)
+        }
         print(text)
         inputBar.inputTextView.text = String()
 //        messagesCollectionView.reloadData()

+ 10 - 3
deltachat-ios/GroupNameController.swift

@@ -65,9 +65,16 @@ class GroupNameController: UIViewController {
                 fatalError("failed to add \(contactId) to group \(groupName)")
             }
         }
-
-        let chatVC = ChatViewController(chatId: Int(groupChatId))
-        navigationController?.pushViewController(chatVC, animated: true)
+        groupNameTextField.resignFirstResponder()
+        let root = navigationController?.presentingViewController
+        navigationController?.dismiss(animated: true) {
+            let chatVC = ChatViewController(chatId: Int(groupChatId))
+            if let navigationRoot = root as? UINavigationController {
+                navigationRoot.pushViewController(chatVC, animated: true)
+            }
+        }
+        
+        
         
     }
 

+ 28 - 1
deltachat-ios/Wrapper.swift

@@ -7,7 +7,7 @@
 //
 
 import Foundation
-
+import UIKit
 
 class MRContact {
     private var contactPointer: UnsafeMutablePointer<dc_contact_t>
@@ -67,6 +67,33 @@ class MRMessage {
         return String(cString: messagePointer.pointee.text)
     }
     
+    var mimeType: String? {
+        guard let result = dc_msg_get_filemime(messagePointer) else { return nil }
+        return String(cString: result)
+    }
+    
+    lazy var image: UIImage? = { [unowned self] in
+        guard let documents = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) else { return nil }
+        let filetype = dc_msg_get_type(messagePointer)
+        let file = dc_msg_get_filename(messagePointer)
+        if let cFile = file, filetype == DC_MSG_IMAGE {
+            let filename = String(cString: cFile)
+            let path: URL = documents.appendingPathComponent(filename)
+            if path.isFileURL {
+                do {
+                    let data = try Data(contentsOf: path)
+                    let image = UIImage(data: data)
+                    return image
+                } catch (_) {
+                    return nil
+                }
+            }
+            return nil
+        } else {
+            return nil
+        }
+    }()
+    
     // MR_MSG_*
     var type: Int {
         return Int(messagePointer.pointee.type)