瀏覽代碼

refactor sending messages with attached images

cyberta 5 年之前
父節點
當前提交
e3998efdd4
共有 2 個文件被更改,包括 31 次插入8 次删除
  1. 4 8
      deltachat-ios/Controller/ChatViewController.swift
  2. 27 0
      deltachat-ios/DC/Wrapper.swift

+ 4 - 8
deltachat-ios/Controller/ChatViewController.swift

@@ -749,15 +749,11 @@ extension ChatViewController: MessagesDataSource {
             if let compressedImage = image.dcCompress() {
                 // at this point image is compressed by 85% by default
                 let pixelSize = compressedImage.imageSizeInPixel()
-                let width = Int32(exactly: pixelSize.width)!
-                let height =  Int32(exactly: pixelSize.height)!
                 let path = Utils.saveImage(image: compressedImage)
-                let msg = dc_msg_new(mailboxPointer, DC_MSG_IMAGE)
-                dc_msg_set_file(msg, path, "image/jpeg")
-                dc_msg_set_dimension(msg, width, height)
-                dc_send_msg(mailboxPointer, UInt32(self.chatId), msg)
-                // cleanup
-                dc_msg_unref(msg)
+                let msg = DcMsg(viewType: DC_MSG_IMAGE)
+                msg.setFile(filepath: path, mimeType: "image/jpeg")
+                msg.setDimension(width: pixelSize.width, height: pixelSize.height)
+                msg.sendInChat(id: self.chatId)
             }
         }
     }

+ 27 - 0
deltachat-ios/DC/Wrapper.swift

@@ -498,6 +498,21 @@ class DcArray {
 class DcMsg: MessageType {
     private var messagePointer: OpaquePointer?
 
+    /**
+        viewType: one of
+            DC_MSG_TEXT,
+            DC_MSG_IMAGE,
+            DC_MSG_GIF,
+            DC_MSG_STICKER,
+            DC_MSG_AUDIO,
+            DC_MSG_VOICE,
+            DC_MSG_VIDEO,
+            DC_MSG_FILE
+     */
+    init(viewType: Int32) {
+        messagePointer = dc_msg_new(mailboxPointer, viewType)
+    }
+
     init(id: Int) {
         messagePointer = dc_get_msg(mailboxPointer, UInt32(id))
     }
@@ -717,6 +732,14 @@ class DcMsg: MessageType {
         return nil
     }
 
+    func setFile(filepath: String?, mimeType: String?) {
+        dc_msg_set_file(messagePointer, filepath, mimeType)
+    }
+
+    func setDimension(width: CGFloat, height: CGFloat) {
+        dc_msg_set_dimension(messagePointer, Int32(exactly: width)!, Int32(exactly: height)!)
+    }
+
     var filesize: Int {
         return Int(dc_msg_get_filebytes(messagePointer))
     }
@@ -761,6 +784,10 @@ class DcMsg: MessageType {
         let chatId = dc_create_chat_by_msg_id(mailboxPointer, UInt32(id))
         return DcChat(id: Int(chatId))
     }
+
+    func sendInChat(id: Int) {
+        dc_send_msg(mailboxPointer, UInt32(id), messagePointer)
+    }
 }
 
 class DcContact {