Procházet zdrojové kódy

open image gallery only on tap on the image, not the whole cell

cyberta před 4 roky
rodič
revize
f06208ab76

+ 24 - 8
deltachat-ios/Chat/ChatViewControllerNew.swift

@@ -381,14 +381,10 @@ class ChatViewControllerNew: UITableViewController {
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         let messageId = messageIds[indexPath.row]
         let message = DcMsg(id: messageId)
-        if let url = message.fileURL {
-            // find all other messages with same message type
-            let previousUrls: [URL] = message.previousMediaURLs()
-            let nextUrls: [URL] = message.nextMediaURLs()
-
-            // these are the files user will be able to swipe trough
-            let mediaUrls: [URL] = previousUrls + [url] + nextUrls
-            showMediaGallery(currentIndex: previousUrls.count, mediaUrls: mediaUrls)
+        if message.type == DC_MSG_FILE ||
+            message.type == DC_MSG_AUDIO ||
+            message.type == DC_MSG_VOICE {
+            showMediaGalleryFor(message: message)
         }
         UIMenuController.shared.setMenuVisible(false, animated: true)
     }
@@ -989,12 +985,32 @@ class ChatViewControllerNew: UITableViewController {
             break
         }
     }
+
+    func showMediaGalleryFor(indexPath: IndexPath) {
+        let messageId = messageIds[indexPath.row]
+        let message = DcMsg(id: messageId)
+        showMediaGalleryFor(message: message)
+    }
+
+    func showMediaGalleryFor(message: DcMsg) {
+        if let url = message.fileURL {
+            // find all other messages with same message type
+            let previousUrls: [URL] = message.previousMediaURLs()
+            let nextUrls: [URL] = message.nextMediaURLs()
+            // these are the files user will be able to swipe trough
+            let mediaUrls: [URL] = previousUrls + [url] + nextUrls
+            showMediaGallery(currentIndex: previousUrls.count, mediaUrls: mediaUrls)
+        }
+    }
 }
 
 // MARK: - BaseMessageCellDelegate
 extension ChatViewControllerNew: BaseMessageCellDelegate {
     func linkTapped(link: String) {
     }
+    func imageTapped(indexPath: IndexPath) {
+        showMediaGalleryFor(indexPath: indexPath)
+    }
 }
 
 // MARK: - MediaPickerDelegate

+ 1 - 0
deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift

@@ -268,4 +268,5 @@ public class BaseMessageCell: UITableViewCell {
 public protocol BaseMessageCellDelegate: class {
 
     func linkTapped(link: String) // link is eg. `https://foo.bar` or `/command`
+    func imageTapped(indexPath: IndexPath)
 }

+ 10 - 0
deltachat-ios/Chat/Views/Cells/NewImageTextCell.swift

@@ -23,6 +23,7 @@ class NewImageTextCell: BaseMessageCell {
         imageView.translatesAutoresizingMaskIntoConstraints = false
         imageView.contentMode = .scaleAspectFit
         imageView.setContentHuggingPriority(.defaultHigh, for: .vertical)
+        imageView.isUserInteractionEnabled = true
         return imageView
     }()
 
@@ -32,6 +33,9 @@ class NewImageTextCell: BaseMessageCell {
         mainContentView.addArrangedSubview(messageLabel)
         contentImageView.constraintAlignLeadingMaxTo(mainContentView).isActive = true
         contentImageView.constraintAlignTrailingMaxTo(mainContentView).isActive = true
+        let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(onImageTapped))
+        gestureRecognizer.numberOfTapsRequired = 1
+        contentImageView.addGestureRecognizer(gestureRecognizer)
     }
 
     override func update(msg: DcMsg, messageStyle: UIRectCorner, isAvatarVisible: Bool) {
@@ -50,6 +54,12 @@ class NewImageTextCell: BaseMessageCell {
         super.update(msg: msg, messageStyle: messageStyle, isAvatarVisible: isAvatarVisible)
     }
 
+    @objc func onImageTapped() {
+        if let tableView = self.superview as? UITableView, let indexPath = tableView.indexPath(for: self) {
+            baseDelegate?.imageTapped(indexPath: indexPath)
+        }
+    }
+
     private func setAspectRatioFor(msg: DcMsg) {
         guard let image = msg.image else {
            return