ソースを参照

implement deletion of single messages (#181)

cyberta 5 年 前
コミット
245ae01e4b

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

@@ -127,6 +127,9 @@ class ChatViewController: MessagesViewController {
                 } else if let id = ui["message_id"] as? Int {
                     if id > 0 {
                         self.updateMessage(id)
+                    } else {
+                        // change might be a deletion
+                        self.refreshMessages()
                     }
                 }
             }
@@ -264,6 +267,7 @@ class ChatViewController: MessagesViewController {
             // Configures the UIMenu which is shown when selecting a message
             menuItems = [
                 UIMenuItem(title: String.localized("info"), action: #selector(MessageCollectionViewCell.messageInfo(_:))),
+                UIMenuItem(title: String.localized("delete"), action: #selector(MessageCollectionViewCell.messageDelete(_:)))
             ]
         }
 
@@ -430,6 +434,7 @@ class ChatViewController: MessagesViewController {
 
     override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
         if action == NSSelectorFromString("messageInfo:") ||
+            action == NSSelectorFromString("messageDelete:") ||
             action == NSSelectorFromString("messageBlock:") ||
             action == NSSelectorFromString("messageDismiss:") ||
             action == NSSelectorFromString("messageStartChat:") {
@@ -449,6 +454,10 @@ class ChatViewController: MessagesViewController {
             if let ctrl = navigationController {
                 ctrl.pushViewController(msgViewController, animated: true)
             }
+        case NSSelectorFromString("messageDelete:"):
+            let msg = messageList[indexPath.section]
+            logger.info("message: delete \(msg.messageId)")
+            askToDeleteMessage(id: msg.id)
         case NSSelectorFromString("messageStartChat:"):
             let msg = messageList[indexPath.section]
             logger.info("message: Start Chat \(msg.messageId)")
@@ -491,6 +500,20 @@ class ChatViewController: MessagesViewController {
         }))
         present(alert, animated: true, completion: nil)
     }
+
+    private func askToDeleteMessage(id: Int) {
+        let alert = UIAlertController(title: String.localized("delete_message_ask"),
+                                         message: nil,
+                                         preferredStyle: .actionSheet)
+        alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { _ in
+            self.dcContext.deleteMessage(msgId: id)
+            self.dismiss(animated: true, completion: nil)
+        }))
+        alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in
+            self.dismiss(animated: true, completion: nil)
+        }))
+        present(alert, animated: true, completion: nil)
+    }
 }
 
 // MARK: - MessagesDataSource
@@ -1090,6 +1113,21 @@ extension ChatViewController: InputBarAccessoryViewDelegate {
 
 // MARK: - MessageCollectionViewCell
 extension MessageCollectionViewCell {
+
+
+    @objc func messageDelete(_ sender: Any?) {
+        // Get the collectionView
+        if let collectionView = self.superview as? UICollectionView {
+            // Get indexPath
+            if let indexPath = collectionView.indexPath(for: self) {
+                // Trigger action
+                collectionView.delegate?.collectionView?(collectionView,
+                    performAction: #selector(MessageCollectionViewCell.messageDelete(_:)),
+                    forItemAt: indexPath, withSender: sender)
+            }
+        }
+    }
+
     @objc func messageInfo(_ sender: Any?) {
         // Get the collectionView
         if let collectionView = self.superview as? UICollectionView {

+ 4 - 1
deltachat-ios/DC/Wrapper.swift

@@ -78,6 +78,10 @@ class DcContext {
         return "ErrGetMsgInfo"
     }
 
+    func deleteMessage(msgId: Int) {
+        dc_delete_msgs(contextPointer, [UInt32(msgId)], 1)
+    }
+
     func initiateKeyTransfer() -> String? {
         if let cString = dc_initiate_key_transfer(self.contextPointer) {
             let swiftString = String(cString: cString)
@@ -533,7 +537,6 @@ class DcMsg: MessageType {
                 NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12),
                 NSAttributedString.Key.foregroundColor: DcColors.grayTextColor,
                 ])
-            print("show info: ", text)
             return MessageKind.attributedText(text)
         } else if isSetupMessage {
             return MessageKind.text(String.localized("autocrypt_asm_click_body"))

+ 1 - 0
deltachat-ios/en.lproj/Localizable.strings

@@ -625,3 +625,4 @@
 "complete" = "Complete";
 "info" = "Info";
 "qr_code_title" = "QR scan/show";
+"delete_message_ask" = "Do you really want to delete this message from your phone?";

+ 1 - 0
tools/untranslated.xml

@@ -23,6 +23,7 @@
     <string name="complete">Complete</string>
     <string name="info">Info</string>
     <string name="qr_code_title">QR scan/show</string>
+    <string name="delete_message_ask">Do you really want to delete this message from your phone?</string>
     <!-- info plist keys need to pre prefixed with INFOPLIST., spaces need to be replaced with dots and dashes need to replaced with underscores -->
     <string name="INFOPLIST.Privacy._.Camera.Usage.Description">Allowing access to the camera lets you take photos and videos.</string>
     <string name="INFOPLIST.Privacy._.Contacts.Usage.Description">Allowing access to your address book lets you chat with contacts from your device.</string>