Selaa lähdekoodia

Sent mechanism fix and some fixes in code

humanlikedisaster 6 vuotta sitten
vanhempi
commit
ee1d737456

+ 4 - 2
deltachat-ios/AppDelegate.swift

@@ -11,6 +11,8 @@ import AudioToolbox
 
 
 var mailboxPointer:UnsafeMutablePointer<dc_context_t>!
+let notificationChanged = Notification.Name(rawValue:"MrEventMsgsChanged")
+let 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: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:notificationIncoming,
                     object: nil,
                     userInfo: ["message":"Incoming Message!", "date":Date()])
         }

+ 5 - 5
deltachat-ios/ChatListController.swift

@@ -34,10 +34,13 @@ 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"),
                                             object:nil, queue:nil) {
@@ -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 {

+ 17 - 10
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: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: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 {
@@ -425,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)
     }
 }

+ 19 - 13
deltachat-ios/Wrapper.swift

@@ -72,21 +72,27 @@ class MRMessage {
         return String(cString: result)
     }
     
-    var image: UIImage? {
-        let file = dc_msg_get_file(messagePointer)
-        guard let cPath = file else { return nil }
-        let path = String(cString: cPath)
-        if path.count > 0 {
-            do {
-                let data = try Data(contentsOf: URL(fileURLWithPath: path))
-                let image = UIImage(data: data)
-                return image
-            } catch (_) {
-                return nil
+    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
         }
-        return nil
-    }
+    }()
     
     // MR_MSG_*
     var type: Int {