소스 검색

add editing options for drafted images and videos

cyberta 4 년 전
부모
커밋
c480583230

+ 18 - 0
deltachat-ios/Chat/ChatViewController.swift

@@ -1222,9 +1222,27 @@ extension ChatViewController: DraftPreviewDelegate {
         if let attachmentPath = draft.draftAttachment {
             let attachmentURL = URL(fileURLWithPath: attachmentPath, isDirectory: false)
             let previewController = PreviewController(type: .single(attachmentURL))
+            if #available(iOS 13.0, *), draft.draftViewType == DC_MSG_IMAGE || draft.draftViewType == DC_MSG_VIDEO {
+                previewController.setEditing(true, animated: true)
+                previewController.delegate = self
+            }
             let nav = UINavigationController(rootViewController: previewController)
             nav.modalPresentationStyle = .fullScreen
             navigationController?.present(nav, animated: true)
         }
     }
 }
+
+extension ChatViewController: QLPreviewControllerDelegate {
+    @available(iOS 13.0, *)
+    func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
+        return .updateContents
+    }
+
+    func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
+        DispatchQueue.main.async { [weak self] in
+            guard let self = self else { return }
+            self.draftArea.reload(draft: self.draft)
+        }
+    }
+}

+ 6 - 0
deltachat-ios/Chat/Views/DraftArea.swift

@@ -76,6 +76,12 @@ public class DraftArea: UIView, InputItem {
         chatInputBar.configure(draft: draft)
     }
 
+    /// reload cleans caches containing the drafted attachment so that the UI will update correctly
+    public func reload(draft: DraftModel) {
+        mediaPreview.reload(draft: draft)
+        ///TODO: add document reloading when document editing was added
+    }
+
     public func cancel() {
         quotePreview.cancel()
         mediaPreview.cancel()

+ 14 - 0
deltachat-ios/Chat/Views/MediaPreview.swift

@@ -82,4 +82,18 @@ class MediaPreview: DraftPreview {
     @objc func imageTapped() {
         delegate?.onAttachmentTapped()
     }
+
+    func reload(draft: DraftModel) {
+        guard let attachment = draft.draftAttachment else { return }
+        let url = URL(fileURLWithPath: attachment, isDirectory: false)
+        // there are editing options for DC_MSG_GIF, so that can be ignored
+        if draft.draftViewType == DC_MSG_IMAGE {
+            SDImageCache.shared.removeImage(forKey: url.absoluteString, withCompletion: { [weak self] in
+                self?.configure(draft: draft)
+            })
+        } else if draft.draftViewType == DC_MSG_VIDEO {
+            ThumbnailCache.shared.deleteImage(key: attachment)
+            self.configure(draft: draft)
+        }
+    }
 }

+ 4 - 0
deltachat-ios/Helper/ThumbnailCache.swift

@@ -18,4 +18,8 @@ class ThumbnailCache {
     func restoreImage(key: String) -> UIImage? {
         return cache.object(forKey: NSString(string: key))
     }
+
+    func deleteImage(key: String) {
+        cache.removeObject(forKey: NSString(string: key))
+    }
 }