Browse Source

add basic context menu, implement copy

B. Petersen 4 years ago
parent
commit
3b3f608c93

+ 33 - 14
deltachat-ios/Chat/ChatViewControllerNew.swift

@@ -141,6 +141,12 @@ class ChatViewControllerNew: UITableViewController {
                                        object: nil)
         notificationCenter.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
         notificationCenter.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
+        UIMenuController.shared.menuItems = [
+            UIMenuItem(title: String.localized("info"), action: #selector(BaseMessageCell.messageInfo)),
+            UIMenuItem(title: String.localized("delete"), action: #selector(BaseMessageCell.messageDelete)),
+            UIMenuItem(title: String.localized("forward"), action: #selector(BaseMessageCell.messageForward))
+        ]
+        UIMenuController.shared.update()
     }
 
     @objc func keyboardWillShow(_ notification: Notification) {
@@ -194,8 +200,6 @@ class ChatViewControllerNew: UITableViewController {
             updateTitle(chat: dcContext.getChat(chatId: chatId))
         }
 
-        configureMessageMenu()
-
         let nc = NotificationCenter.default
         msgChangedObserver = nc.addObserver(
             forName: dcNotificationChanged,
@@ -543,18 +547,6 @@ class ChatViewControllerNew: UITableViewController {
         }
     }
 
-    private func configureMessageMenu() {
-        var menuItems: [UIMenuItem]
-
-        menuItems = [
-            UIMenuItem(title: String.localized("info"), action: #selector(MessageCollectionViewCell.messageInfo(_:))),
-            UIMenuItem(title: String.localized("delete"), action: #selector(MessageCollectionViewCell.messageDelete(_:))),
-            UIMenuItem(title: String.localized("forward"), action: #selector(MessageCollectionViewCell.messageForward(_:)))
-        ]
-
-        UIMenuController.shared.menuItems = menuItems
-    }
-
     private func configureMessageInputBar() {
         messageInputBar.delegate = self
         messageInputBar.inputTextView.tintColor = DcColors.primary
@@ -914,6 +906,33 @@ class ChatViewControllerNew: UITableViewController {
             sendImage(image)
         }
     }
+
+    // MARK: - Context menu
+    override func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
+        return true
+    }
+
+    override func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
+        return action == #selector(UIResponderStandardEditActions.copy(_:))
+            || action == #selector(BaseMessageCell.messageInfo)
+            || action == #selector(BaseMessageCell.messageDelete)
+            || action == #selector(BaseMessageCell.messageForward)
+    }
+
+    override func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
+        // handle standard actions here, but custom actions never trigger this. it still needs to be present for the menu to display, though.
+        if action == #selector(copy(_:)) {
+            let id = messageIds[indexPath.row]
+            let msg = DcMsg(id: id)
+
+            let pasteboard = UIPasteboard.general
+            if msg.type == DC_MSG_TEXT {
+                pasteboard.string = msg.text
+            } else {
+                pasteboard.string = msg.summary(chars: 10000000)
+            }
+        }
+    }
 }
 
 /*extension ChatViewControllerNew: MediaSendHandler {

+ 13 - 1
deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift

@@ -113,7 +113,6 @@ public class BaseMessageCell: UITableViewCell {
 
     // update classes inheriting BaseMessageCell first before calling super.update(...)
     func update(msg: DcMsg, messageStyle: UIRectCorner, isAvatarVisible: Bool) {
-
         if msg.isFromCurrentSender {
             topLabel.text = nil
             leadingConstraintCurrentSender?.isActive = true
@@ -233,4 +232,17 @@ public class BaseMessageCell: UITableViewCell {
         bottomLabel.text = nil
         bottomLabel.attributedText = nil
     }
+
+    // MARK: - Context menu
+    @objc func messageInfo(sender: AnyObject?) {
+
+    }
+
+    @objc func messageDelete(sender: AnyObject?) {
+
+    }
+
+    @objc func messageForward(sender: AnyObject?) {
+
+    }
 }