Skip to content

Commit

Permalink
detect long tap on link (I finally found out how to do it 🎉 )
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Laux committed Apr 17, 2024
1 parent d6b3416 commit 906d4d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
11 changes: 10 additions & 1 deletion deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1993,7 +1993,16 @@ extension ChatViewController {
if tableView.isEditing || messageId == DC_MSG_ID_MARKER1 || messageId == DC_MSG_ID_DAYMARKER {
return nil
}

// Check if the long tap is on a link (or other message text element with custom long tap behavior)
if let msgcell = tableView.cellForRow(at: indexPath) as? BaseMessageCell {
let label = msgcell.messageLabel.label
let localTouchLocation = tableView.convert(point, to: label)
let handled = label.handleGesture(localTouchLocation, longTap: true)
if handled {
return nil
}
}

return UIContextMenuConfiguration(
identifier: NSString(string: "\(messageId)"),
previewProvider: nil,
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ public class BaseMessageCell: UITableViewCell {
open func handleTapGesture(_ gesture: UIGestureRecognizer) {
guard gesture.state == .ended else { return }
let touchLocation = gesture.location(in: messageLabel)
let isHandled = messageLabel.label.handleGesture(touchLocation)
let isHandled = messageLabel.label.handleGesture(touchLocation, longTap: false)
if !isHandled, let tableView = self.superview as? UITableView, let indexPath = tableView.indexPath(for: self) {
self.baseDelegate?.textTapped(indexPath: indexPath)
}
Expand Down
46 changes: 44 additions & 2 deletions deltachat-ios/Chat/Views/MessageLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,62 @@ open class MessageLabel: UILabel {

}

open func handleGesture(_ touchLocation: CGPoint) -> Bool {
open func handleGesture(_ touchLocation: CGPoint, longTap: Bool) -> Bool {

guard let index = stringIndex(at: touchLocation) else { return false }

for (detectorType, ranges) in rangesForDetectors {
for (range, value) in ranges {
if range.contains(index) {
handleGesture(for: detectorType, value: value)
if longTap {
return handleLongTapGesture(for: detectorType, value: value)
} else {
handleGesture(for: detectorType, value: value)
}
return true
}
}
}
return false
}

/// @returns whether item was handled
private func handleLongTapGesture(for detectorType: DetectorType, value: NewMessageTextCheckingType) -> Bool {
switch value {
// case let .addressComponents(addressComponents):
// var transformedAddressComponents = [String: String]()
// guard let addressComponents = addressComponents else { return false }
// addressComponents.forEach { (key, value) in
// transformedAddressComponents[key.rawValue] = value
// }
// handleAddress(transformedAddressComponents)
case let .phoneNumber(phoneNumber):
guard let phoneNumber = phoneNumber else { return false }
handlePhoneNumber(phoneNumber)
// case let .date(date):
// guard let date = date else { return false }
// handleDate(date)
case let .link(url):
guard let url = url else { return false }
handleURL(url)

// case let .custom(pattern, match):
// guard let match = match else { return false }
// switch detectorType {
// case .hashtag:
// handleHashtag(match)
// case .mention:
// handleMention(match)
// case .command:
// handleCommand(match)
// default:
// handleCustom(pattern, match: match)
// }
default:
return false
}
return false
}

/// swiftlint:disable cyclomatic_complexity
private func handleGesture(for detectorType: DetectorType, value: NewMessageTextCheckingType) {
Expand Down
2 changes: 1 addition & 1 deletion deltachat-ios/View/MultilineLabelCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class MultilineLabelCell: UITableViewCell {
open func handleTapGesture(_ gesture: UIGestureRecognizer) {
guard gesture.state == .ended else { return }
let touchLocation = gesture.location(in: label)
let isHandled = label.handleGesture(touchLocation)
let isHandled = label.handleGesture(touchLocation, longTap: false)
if !isHandled {
logger.info("status: tapped outside urls or phone numbers")
}
Expand Down

0 comments on commit 906d4d8

Please sign in to comment.