Browse Source

Merge pull request #1573 from deltachat/webxdc_gallery

adapt Document Gallery for webxdcs
cyBerta 3 năm trước cách đây
mục cha
commit
e9dc704c0a

+ 82 - 2
deltachat-ios/Controller/DocumentGalleryController.swift

@@ -1,5 +1,6 @@
 import UIKit
 import DcCore
+import LinkPresentation
 
 class DocumentGalleryController: UIViewController {
 
@@ -41,9 +42,16 @@ class DocumentGalleryController: UIViewController {
                 self?.redirectToMessage(of: indexPath)
             }
         )
+        let shareItem = ContextMenuProvider.ContextMenuItem(
+            title: String.localized("menu_share"),
+            imageName: "square.and.arrow.up",
+            action: #selector(DocumentGalleryFileCell.share(_:)), onPerform: { [weak self] indexPath in
+                self?.shareAttachment(of: indexPath)
+            }
+        )
 
         let menu = ContextMenuProvider()
-        menu.setMenu([showInChatItem, deleteItem])
+        menu.setMenu([showInChatItem, shareItem, deleteItem])
         return menu
     }()
 
@@ -110,6 +118,11 @@ class DocumentGalleryController: UIViewController {
         self.dcContext.deleteMessage(msgId: msgId)
         self.tableView.deleteRows(at: [indexPath], with: .automatic)
     }
+
+    func showWebxdcViewFor(message: DcMsg) {
+        let webxdcViewController = WebxdcViewController(dcContext: dcContext, messageId: message.id)
+        navigationController?.pushViewController(webxdcViewController, animated: true)
+    }
 }
 
 // MARK: - UITableViewDelegate, UITableViewDataSource
@@ -130,7 +143,12 @@ extension DocumentGalleryController: UITableViewDelegate, UITableViewDataSource
 
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let msgId = fileMessageIds[indexPath.row]
-        showPreview(msgId: msgId)
+        let message = dcContext.getMessage(id: msgId)
+        if message.type == DC_MSG_WEBXDC {
+            showWebxdcViewFor(message: message)
+        } else {
+            showPreview(msgId: msgId)
+        }
         tableView.deselectRow(at: indexPath, animated: false)
     }
 
@@ -180,4 +198,66 @@ extension DocumentGalleryController {
             appDelegate.appCoordinator.showChat(chatId: chatId, msgId: msgId, animated: false, clearViewControllerStack: true)
         }
     }
+
+    func shareAttachment(of indexPath: IndexPath) {
+        let msgId = fileMessageIds[indexPath.row]
+        let message = dcContext.getMessage(id: msgId)
+        let activityVC: UIActivityViewController
+        guard let fileURL = message.fileURL else { return }
+        let objectsToShare: [Any]
+        if message.type == DC_MSG_WEBXDC {
+            let dict = message.getWebxdcInfoDict()
+            var previewImage: UIImage?
+            if let iconfilePath = dict["icon"] as? String {
+                let blob = message.getWebxdcBlob(filename: iconfilePath)
+                if !blob.isEmpty {
+                    previewImage = UIImage(data: blob)
+                }
+            }
+
+            let previewText = dict["name"] as? String ?? fileURL.lastPathComponent
+            objectsToShare = [WebxdcItemSource(title: previewText,
+                                               previewImage: previewImage,
+                                               url: fileURL)]
+        } else {
+            objectsToShare = [fileURL]
+        }
+
+        activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
+        activityVC.excludedActivityTypes = [.copyToPasteboard]
+        activityVC.popoverPresentationController?.sourceView = self.view
+        self.present(activityVC, animated: true, completion: nil)
+    }
+}
+
+class WebxdcItemSource: NSObject, UIActivityItemSource {
+    var title: String
+    var url: URL
+    var previewImage: UIImage?
+
+    init(title: String, previewImage: UIImage?, url: URL) {
+        self.title = title
+        self.url = url
+        self.previewImage = previewImage
+        super.init()
+    }
+
+    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
+        return title
+    }
+
+    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
+        return url
+    }
+
+    @available(iOS 13.0, *)
+    func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
+        let metadata = LPLinkMetadata()
+        metadata.title = title
+        if let previewImage = previewImage {
+            metadata.iconProvider = NSItemProvider(object: previewImage)
+        }
+        metadata.originalURL = url
+        return metadata
+    }
 }

+ 30 - 1
deltachat-ios/View/Cell/DocumentGalleryFileCell.swift

@@ -67,6 +67,14 @@ class DocumentGalleryFileCell: UITableViewCell {
 
     // MARK: - update
     func update(msg: DcMsg) {
+        if msg.type == DC_MSG_WEBXDC {
+            updateWebxdcMsg(msg: msg)
+        } else {
+            updateFileMsg(msg: msg)
+        }
+    }
+
+    private func updateFileMsg(msg: DcMsg) {
         if let fileUrl = msg.fileURL {
             generateThumbnailFor(url: fileUrl, placeholder: UIImage(named: "ic_attach_file_36pt")?.maskWithColor(color: DcColors.grayTextColor))
         }
@@ -74,6 +82,23 @@ class DocumentGalleryFileCell: UITableViewCell {
         subtitle.text = msg.getPrettyFileSize()
     }
 
+    private func updateWebxdcMsg(msg: DcMsg) {
+        let dict = msg.getWebxdcInfoDict()
+        if let iconfilePath = dict["icon"] as? String {
+            let blob = msg.getWebxdcBlob(filename: iconfilePath)
+            if !blob.isEmpty {
+                fileImageView.image = UIImage(data: blob)?.sd_resizedImage(with: CGSize(width: 50, height: 50), scaleMode: .aspectFill)
+            }
+        }
+
+        title.text = dict["name"] as? String
+        guard let summary = dict["summary"] as? String, !summary.isEmpty else {
+            subtitle.text = "Webxdc"
+            return
+        }
+        subtitle.text = summary
+    }
+
     private func generateThumbnailFor(url: URL, placeholder: UIImage?) {
         if let thumbnail = ThumbnailCache.shared.restoreImage(key: url.absoluteString) {
             fileImageView.image = thumbnail
@@ -86,7 +111,7 @@ class DocumentGalleryFileCell: UITableViewCell {
         }
     }
 
-    // needed for iOS 12 context men
+    // needed for iOS 12 context menu
     @objc func itemDelete(_ sender: Any) {
         self.performAction(#selector(DocumentGalleryFileCell.itemDelete(_:)), with: sender)
     }
@@ -95,6 +120,10 @@ class DocumentGalleryFileCell: UITableViewCell {
         self.performAction(#selector(DocumentGalleryFileCell.showInChat(_:)), with: sender)
     }
 
+    @objc func share(_ sender: Any) {
+        self.performAction(#selector(DocumentGalleryFileCell.share(_:)), 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?(