浏览代码

added context menu logic to documentGalleryController

nayooti 4 年之前
父节点
当前提交
6bbb9db53d

+ 1 - 1
deltachat-ios/Controller/ContactDetailViewController.swift

@@ -390,7 +390,7 @@ class ContactDetailViewController: UITableViewController {
 
     private func showDocuments() {
         let messageIds: [Int] = viewModel.documentItemMessageIds.reversed()
-        let fileGalleryController = DocumentGalleryController(fileMessageIds: messageIds)
+        let fileGalleryController = DocumentGalleryController(context: viewModel.context, fileMessageIds: messageIds)
         navigationController?.pushViewController(fileGalleryController, animated: true)
     }
 

+ 71 - 9
deltachat-ios/Controller/DocumentGalleryController.swift

@@ -3,9 +3,10 @@ import DcCore
 
 class DocumentGalleryController: UIViewController {
 
-    private let fileMessageIds: [Int]
+    private var fileMessageIds: [Int]
+    private let dcContext: DcContext
 
-    private lazy var tableViews: UITableView = {
+    private lazy var tableView: UITableView = {
         let table = UITableView(frame: .zero, style: .grouped)
         table.register(DocumentGalleryFileCell.self, forCellReuseIdentifier: DocumentGalleryFileCell.reuseIdentifier)
         table.dataSource = self
@@ -21,8 +22,33 @@ class DocumentGalleryController: UIViewController {
         return label
     }()
 
+    private lazy var contextMenuConfiguration: ContextMenuConfiguration = {
+        let deleteItem = ContextMenuConfiguration.ContextMenuItem(
+            title: String.localized("delete"),
+            imageNames: ("trash", nil),
+            option: .delete,
+            action: #selector(GalleryCell.itemDelete(_:)),
+            onPerform: { [weak self] indexPath in
+                self?.askToDeleteItem(at: indexPath)
+            }
+        )
+        let showInChatItem = ContextMenuConfiguration.ContextMenuItem(
+            title: String.localized("show_in_chat"),
+            imageNames: ("doc.text.magnifyingglass", nil),
+            option: .showInChat,
+            action: #selector(GalleryCell.showInChat(_:)),
+            onPerform: { [weak self] indexPath in
+                self?.redirectToMessage(of: indexPath)
+            }
+        )
 
-    init(fileMessageIds: [Int]) {
+        let config = ContextMenuConfiguration()
+        config.setMenu([showInChatItem, deleteItem])
+        return config
+    }()
+
+    init(context: DcContext, fileMessageIds: [Int]) {
+        self.dcContext = context
         self.fileMessageIds = fileMessageIds
         super.init(nibName: nil, bundle: nil)
         self.title = String.localized("files")
@@ -43,12 +69,12 @@ class DocumentGalleryController: UIViewController {
 
     // MARK: - layout
     private func setupSubviews() {
-        view.addSubview(tableViews)
-        tableViews.translatesAutoresizingMaskIntoConstraints = false
-        tableViews.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
-        tableViews.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
-        tableViews.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
-        tableViews.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
+        view.addSubview(tableView)
+        tableView.translatesAutoresizingMaskIntoConstraints = false
+        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
+        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
+        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
+        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
 
         view.addSubview(emptyStateView)
         emptyStateView.translatesAutoresizingMaskIntoConstraints = false
@@ -57,6 +83,25 @@ class DocumentGalleryController: UIViewController {
         emptyStateView.centerYAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerYAnchor).isActive = true
         emptyStateView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
     }
+
+    // MARK: - actions
+    private func askToDeleteItem(at indexPath: IndexPath) {
+        let title = String.localized(stringID: "ask_delete_messages", count: 1)
+        let alertController =  UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)
+        let okAction = UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { [weak self] _ in
+            self?.deleteItem(at: indexPath)
+        })
+        let cancelAction = UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil)
+        alertController.addAction(okAction)
+        alertController.addAction(cancelAction)
+        present(alertController, animated: true, completion: nil)
+    }
+
+    private func deleteItem(at indexPath: IndexPath) {
+        let msgId = fileMessageIds.remove(at: indexPath.row)
+        self.dcContext.deleteMessage(msgId: msgId)
+        self.tableView.deleteRows(at: [indexPath], with: .automatic)
+    }
 }
 
 // MARK: - UITableViewDelegate, UITableViewDataSource
@@ -91,4 +136,21 @@ extension DocumentGalleryController {
         let previewController = PreviewController(type: .multi(fileMessageIds, index))
         present(previewController, animated: true, completion: nil)
     }
+
+    func redirectToMessage(of indexPath: IndexPath) {
+        let msgId = fileMessageIds[indexPath.row]
+
+        guard
+            let chatViewController = navigationController?.viewControllers.filter ({ $0 is ChatViewController}).first as? ChatViewController,
+            let chatListController = navigationController?.viewControllers.filter({ $0 is ChatListController}).first as? ChatListController
+        else {
+            safe_fatalError("failt to retrieve chatViewController, chatListController in navigation stack")
+            return
+        }
+        self.navigationController?.viewControllers.remove(at: 1)
+
+        self.navigationController?.pushViewController(chatViewController, animated: true)
+        self.navigationController?.setViewControllers([chatListController, chatViewController], animated: false)
+        chatViewController.scrollToMessage(msgId: msgId)
+    }
 }

+ 1 - 0
deltachat-ios/Controller/GalleryViewController.swift

@@ -137,6 +137,7 @@ class GalleryViewController: UIViewController {
         }
     }
 
+    // MARK: - actions
     private func askToDeleteItem(at indexPath: IndexPath) {
         let title = String.localized(stringID: "ask_delete_messages", count: 1)
         let alertController =  UIAlertController(title: title, message: nil, preferredStyle: .safeActionSheet)

+ 1 - 1
deltachat-ios/Controller/GroupChatDetailViewController.swift

@@ -304,7 +304,7 @@ class GroupChatDetailViewController: UIViewController {
 
     private func showDocuments() {
         let messageIds: [Int] = documentItemMessageIds.reversed()
-        let fileGalleryController = DocumentGalleryController(fileMessageIds: messageIds)
+        let fileGalleryController = DocumentGalleryController(context: dcContext, fileMessageIds: messageIds)
         navigationController?.pushViewController(fileGalleryController, animated: true)    }
 
     private func showGallery() {