瀏覽代碼

Improve webxdc preview (#1575)

* show webxdc preview image when sharing webxdc files

* rounded corners in webxdc share previews
cyBerta 3 年之前
父節點
當前提交
d2dcf6a0b8

+ 5 - 1
DcCore/DcCore/Extensions/UIImage+Extensions.swift

@@ -28,9 +28,13 @@ public extension UIImage {
         return CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
     }
 
-    func scaleDownImage(toMax: CGFloat) -> UIImage? {
+    func scaleDownImage(toMax: CGFloat, cornerRadius: CGFloat? = nil) -> UIImage? {
         let rect = getResizedRectangle(toMax: Float(toMax))
         UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)
+        if let cornerRadius = cornerRadius {
+            UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).addClip()
+        }
+
         draw(in: rect)
         let newImage = UIGraphicsGetImageFromCurrentImageContext()
         UIGraphicsEndImageContext()

+ 14 - 3
DcShare/Helper/ShareAttachment.swift

@@ -134,7 +134,7 @@ class ShareAttachment {
         item.loadItem(forTypeIdentifier: kUTTypeMovie as String, options: nil) { data, error in
             switch data {
             case let url as URL:
-                self.addDcMsg(url: url, viewType: DC_MSG_VIDEO)
+                _ = self.addDcMsg(url: url, viewType: DC_MSG_VIDEO)
                 self.delegate?.onAttachmentChanged()
                 if self.imageThumbnail == nil {
                     DispatchQueue.global(qos: .background).async {
@@ -166,7 +166,17 @@ class ShareAttachment {
         item.loadItem(forTypeIdentifier: typeIdentifier as String, options: nil) { data, error in
             switch data {
             case let url as URL:
-                self.addDcMsg(url: url, viewType: viewType)
+                if url.pathExtension == "xdc" {
+                    let webxdcMsg = self.addDcMsg(url: url, viewType: DC_MSG_WEBXDC)
+                    if self.imageThumbnail == nil {
+                        self.imageThumbnail = webxdcMsg.getWebxdcPreviewImage()?
+                            .scaleDownImage(toMax: self.thumbnailSize,
+                                            cornerRadius: 10)
+                        self.delegate?.onThumbnailChanged()
+                    }
+                } else {
+                    _ = self.addDcMsg(url: url, viewType: viewType)
+                }
                 self.delegate?.onAttachmentChanged()
                 if self.imageThumbnail == nil {
                     self.generateThumbnailRepresentations(url: url)
@@ -180,10 +190,11 @@ class ShareAttachment {
         }
     }
 
-    private func addDcMsg(url: URL, viewType: Int32) {
+    private func addDcMsg(url: URL, viewType: Int32) -> DcMsg {
         let msg = dcContext.newMessage(viewType: viewType)
         msg.setFile(filepath: url.relativePath)
         self.messages.append(msg)
+        return msg
     }
 
     private func generateThumbnailRepresentations(url: URL) {

+ 2 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -91,6 +91,7 @@
 		3080A037277DE30100E74565 /* UITextView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3080A032277DE30000E74565 /* UITextView+Extensions.swift */; };
 		3080A038277DE30100E74565 /* UIView+AutoLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3080A033277DE30100E74565 /* UIView+AutoLayout.swift */; };
 		30860EE926F49E64002651A6 /* DownloadOnDemandViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30860EE826F49E64002651A6 /* DownloadOnDemandViewController.swift */; };
+		308850A0282A914F00204623 /* DcMsg+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 304219D82440734A00516852 /* DcMsg+Extension.swift */; };
 		3095A351237DD1F700AB07F7 /* MediaPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3095A350237DD1F700AB07F7 /* MediaPicker.swift */; };
 		30A4149724F6EFBE00EC91EB /* InfoMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30A4149624F6EFBE00EC91EB /* InfoMessageCell.swift */; };
 		30AAD71B2762869600DE3DC1 /* SelectableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30AAD71A2762869600DE3DC1 /* SelectableCell.swift */; };
@@ -1376,6 +1377,7 @@
 				302589FF2452FA280086C1CD /* ShareAttachment.swift in Sources */,
 				3057029F24C6445000D84EFC /* EmptyStateLabel.swift in Sources */,
 				305702A224C6455400D84EFC /* TypeAlias.swift in Sources */,
+				308850A0282A914F00204623 /* DcMsg+Extension.swift in Sources */,
 				30152C9D25A5D95400377714 /* MessageLabelDelegate.swift in Sources */,
 				30E8F2442449C64100CE2C90 /* ChatListCell.swift in Sources */,
 				30E8F2132447285600CE2C90 /* ShareViewController.swift in Sources */,

+ 1 - 8
deltachat-ios/Controller/DocumentGalleryController.swift

@@ -207,14 +207,7 @@ extension DocumentGalleryController {
         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 previewImage = message.getWebxdcPreviewImage()
             let previewText = dict["name"] as? String ?? fileURL.lastPathComponent
             objectsToShare = [WebxdcItemSource(title: previewText,
                                                previewImage: previewImage,

+ 11 - 0
deltachat-ios/DC/DcMsg+Extension.swift

@@ -20,4 +20,15 @@ extension DcMsg {
            }
         return [:]
     }
+
+    public func getWebxdcPreviewImage() -> UIImage? {
+        let dict = self.getWebxdcInfoDict()
+        if let iconfilePath = dict["icon"] as? String {
+            let blob = self.getWebxdcBlob(filename: iconfilePath)
+            if !blob.isEmpty {
+                return UIImage(data: blob)
+            }
+        }
+        return nil
+    }
 }