ソースを参照

context menu in document gallery now works

nayooti 4 年 前
コミット
e3762a38ee

+ 25 - 11
deltachat-ios/Controller/DocumentGalleryController.swift

@@ -22,29 +22,29 @@ class DocumentGalleryController: UIViewController {
         return label
     }()
 
-    private lazy var contextMenuConfiguration: ContextMenuConfiguration = {
-        let deleteItem = ContextMenuConfiguration.ContextMenuItem(
+    private lazy var contextMenu: ContextMenuProvider = {
+        let deleteItem = ContextMenuProvider.ContextMenuItem(
             title: String.localized("delete"),
             imageNames: ("trash", nil),
             option: .delete,
-            action: #selector(GalleryCell.itemDelete(_:)),
+            action: #selector(DocumentGalleryFileCell.itemDelete(_:)),
             onPerform: { [weak self] indexPath in
                 self?.askToDeleteItem(at: indexPath)
             }
         )
-        let showInChatItem = ContextMenuConfiguration.ContextMenuItem(
+        let showInChatItem = ContextMenuProvider.ContextMenuItem(
             title: String.localized("show_in_chat"),
             imageNames: ("doc.text.magnifyingglass", nil),
             option: .showInChat,
-            action: #selector(GalleryCell.showInChat(_:)),
+            action: #selector(DocumentGalleryFileCell.showInChat(_:)),
             onPerform: { [weak self] indexPath in
                 self?.redirectToMessage(of: indexPath)
             }
         )
 
-        let config = ContextMenuConfiguration()
-        config.setMenu([showInChatItem, deleteItem])
-        return config
+        let menu = ContextMenuProvider()
+        menu.setMenu([showInChatItem, deleteItem])
+        return menu
     }()
 
     init(context: DcContext, fileMessageIds: [Int]) {
@@ -67,6 +67,10 @@ class DocumentGalleryController: UIViewController {
         }
     }
 
+    override func viewWillAppear(_ animated: Bool) {
+        setupContextMenuIfNeeded()
+    }
+
     // MARK: - layout
     private func setupSubviews() {
         view.addSubview(tableView)
@@ -84,6 +88,11 @@ class DocumentGalleryController: UIViewController {
         emptyStateView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
     }
 
+    private func setupContextMenuIfNeeded() {
+        UIMenuController.shared.menuItems = contextMenu.menuItems
+        UIMenuController.shared.update()
+    }
+
     // MARK: - actions
     private func askToDeleteItem(at indexPath: IndexPath) {
         let title = String.localized(stringID: "ask_delete_messages", count: 1)
@@ -128,12 +137,17 @@ extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource
 
     // MARK: - context menu
     // context menu for iOS 11, 12
+    func tableView(_ tableView: UITableView, shouldShowMenuForRowAt indexPath: IndexPath) -> Bool {
+        return true
+    }
+
     func tableView(_ tableView: UITableView, canPerformAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
-        return contextMenuConfiguration.canPerformAction(action: action)
+        let action = contextMenu.canPerformAction(action: action)
+        return action
     }
 
     func tableView(_ tableView: UITableView, performAction action: Selector, forRowAt indexPath: IndexPath, withSender sender: Any?) {
-        contextMenuConfiguration.performAction(action: action, indexPath: indexPath)
+        contextMenu.performAction(action: action, indexPath: indexPath)
     }
 
     // context menu for iOS 13+
@@ -143,7 +157,7 @@ extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource
             identifier: nil,
             previewProvider: nil,
             actionProvider: { [weak self] _ in
-                self?.contextMenuConfiguration.actionProvider(indexPath: indexPath)
+                self?.contextMenu.actionProvider(indexPath: indexPath)
             }
         )
     }

+ 12 - 12
deltachat-ios/Controller/GalleryViewController.swift

@@ -46,8 +46,8 @@ class GalleryViewController: UIViewController {
         return label
     }()
 
-    private lazy var contextMenuConfiguration: ContextMenuConfiguration = {
-        let deleteItem = ContextMenuConfiguration.ContextMenuItem(
+    private lazy var contextMenu: ContextMenuProvider = {
+        let deleteItem = ContextMenuProvider.ContextMenuItem(
             title: String.localized("delete"),
             imageNames: ("trash", nil),
             option: .delete,
@@ -56,7 +56,7 @@ class GalleryViewController: UIViewController {
                 self?.askToDeleteItem(at: indexPath)
             }
         )
-        let showInChatItem = ContextMenuConfiguration.ContextMenuItem(
+        let showInChatItem = ContextMenuProvider.ContextMenuItem(
             title: String.localized("show_in_chat"),
             imageNames: ("doc.text.magnifyingglass", nil),
             option: .showInChat,
@@ -66,7 +66,7 @@ class GalleryViewController: UIViewController {
             }
         )
 
-        let config = ContextMenuConfiguration()
+        let config = ContextMenuProvider()
         config.setMenu([showInChatItem, deleteItem])
         return config
     }()
@@ -124,7 +124,7 @@ class GalleryViewController: UIViewController {
     }
 
     private func setupContextMenuIfNeeded() {
-        UIMenuController.shared.menuItems = contextMenuConfiguration.menuItems
+        UIMenuController.shared.menuItems = contextMenu.menuItems
         UIMenuController.shared.update()
     }
 
@@ -218,19 +218,19 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
         timeLabel.hide(animated: true)
     }
 
+    // MARK: - context menu
+    // context menu for iOS 11, 12
     func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
         return true
     }
 
-    // MARK: - context menu
-    // context menu for iOS 11, 12
     func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
-        return contextMenuConfiguration.canPerformAction(action: action)
+        return contextMenu.canPerformAction(action: action)
     }
 
     func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
 
-        contextMenuConfiguration.performAction(action: action, indexPath: indexPath)
+        contextMenu.performAction(action: action, indexPath: indexPath)
     }
 
     // context menu for iOS 13+
@@ -250,7 +250,7 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
                 return contextMenuController
             },
             actionProvider: { [weak self] _ in
-                self?.contextMenuConfiguration.actionProvider(indexPath: indexPath)
+                self?.contextMenu.actionProvider(indexPath: indexPath)
             }
         )
     }
@@ -330,7 +330,7 @@ private extension GalleryViewController {
     }
 }
 
-class ContextMenuConfiguration {
+class ContextMenuProvider {
 
     var menu: [ContextMenuItem] = []
 
@@ -394,7 +394,7 @@ class ContextMenuConfiguration {
     }
 }
 
-extension ContextMenuConfiguration {
+extension ContextMenuProvider {
     typealias ImageSystemName = String
     struct ContextMenuItem {
         var title: String

+ 19 - 0
deltachat-ios/View/Cell/DocumentGalleryFileCell.swift

@@ -84,6 +84,25 @@ class DocumentGalleryFileCell: UITableViewCell {
             let controller = UIDocumentInteractionController(url: url)
             fileImageView.image = controller.icons.first ?? placeholder
         }
+    }
+
+    // needed for iOS 12 context men
+    @objc func itemDelete(_ sender: Any) {
+        self.performAction(#selector(DocumentGalleryFileCell.itemDelete(_:)), with: sender)
+    }
 
+    @objc func showInChat(_ sender: Any) {
+        self.performAction(#selector(DocumentGalleryFileCell.showInChat(_:)), with: sender)
+    }
+
+    func performAction(_ action: Selector, with sender: Any?) {
+        if let tableView = self.superview as? UITableView, let indexPath = tableView.indexPath(for: self) {
+            tableView.delegate?.tableView?(
+                tableView,
+                performAction: action,
+                forRowAt: indexPath,
+                withSender: sender
+            )
+        }
     }
 }