Skip to content

Commit

Permalink
add an action sheet
Browse files Browse the repository at this point in the history
I also needed a weird workaround against false triggers of the normal tap handler
  • Loading branch information
Simon-Laux committed Jun 6, 2024
1 parent b6e0ba7 commit 74d77ac
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
40 changes: 40 additions & 0 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
private var keepKeyboard: Bool = false
private var wasInputBarFirstResponder = false
private var reactionMessageId: Int?

/// time based workaround to not also open the link on long press
private var linkLongPressTS: Double = 0

private lazy var isGroupChat: Bool = {
return dcContext.getChat(chatId: chatId).isGroup
Expand Down Expand Up @@ -2004,11 +2007,14 @@ extension ChatViewController {
if tableView.isEditing || messageId == DC_MSG_ID_MARKER1 || messageId == DC_MSG_ID_DAYMARKER {
return nil
}

print("url: 1")
// 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)
print("url: handled", handled)
if handled {
return nil
}
Expand Down Expand Up @@ -2323,6 +2329,14 @@ extension ChatViewController: BaseMessageCellDelegate {
}

@objc func urlTapped(url: URL, indexPath: IndexPath) {
/// this is somtimes also called on long pressing by accident, this is a time based workround to not open the link when it was long pressed before
let secondsSinceLastLongPress = NSDate().timeIntervalSince1970 - self.linkLongPressTS
print("urlTapped: secondsSinceLastLongPress", secondsSinceLastLongPress, self.linkLongPressTS)
if secondsSinceLastLongPress < 1 {
print("timeSinceLastLongPress prevented opening")
return
}

if handleUIMenu() || handleSelection(indexPath: indexPath) {
return
}
Expand All @@ -2333,6 +2347,32 @@ extension ChatViewController: BaseMessageCellDelegate {
UIApplication.shared.open(url)
}
}

@objc func urlLongTapped(url: URL, indexPath: IndexPath) {
self.linkLongPressTS = NSDate().timeIntervalSince1970
print("urlLongTapped")
let isEmail = Utils.isEmail(url: url)

let alert = UIAlertController(title: isEmail ? "Email" : "Link", message: url.absoluteString, preferredStyle: .actionSheet)
if isEmail {
let email = Utils.getEmailFrom(url)
alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
self.askToChatWith(email: email)
}))
} else {
// TODO: translation string does not exist yet
alert.addAction(UIAlertAction(title: String.localized("menu_open_link"), style: .default, handler: { _ in
UIApplication.shared.open(url)
}))
}

alert.addAction(UIAlertAction(title: String.localized(isEmail ? "menu_copy_email_to_clipboard" : "menu_copy_link_to_clipboard"), style: .default, handler: { _ in
UIPasteboard.general.string = url.absoluteString
}))

alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}

@objc func imageTapped(indexPath: IndexPath, previewError: Bool) {
if handleUIMenu() || handleSelection(indexPath: indexPath) {
Expand Down
8 changes: 8 additions & 0 deletions deltachat-ios/Chat/Views/Cells/BaseMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,7 @@ public class BaseMessageCell: UITableViewCell {

// MARK: - MessageLabelDelegate
extension BaseMessageCell: MessageLabelDelegate {

public func didSelectAddress(_ addressComponents: [String: String]) {}

public func didSelectDate(_ date: Date) {}
Expand All @@ -613,6 +614,12 @@ extension BaseMessageCell: MessageLabelDelegate {
baseDelegate?.urlTapped(url: url, indexPath: indexPath)
}
}

public func didLongPressURL(_ url: URL) {
if let tableView = self.superview as? UITableView, let indexPath = tableView.indexPath(for: self) {
baseDelegate?.urlLongTapped(url: url, indexPath: indexPath)
}
}

public func didSelectTransitInformation(_ transitInformation: [String: String]) {}

Expand Down Expand Up @@ -641,6 +648,7 @@ public protocol BaseMessageCellDelegate: AnyObject {
func commandTapped(command: String, indexPath: IndexPath) // `/command`
func phoneNumberTapped(number: String, indexPath: IndexPath)
func urlTapped(url: URL, indexPath: IndexPath) // url is eg. `https://foo.bar`
func urlLongTapped(url: URL, indexPath: IndexPath) // url is eg. `https://foo.bar` or email address
func imageTapped(indexPath: IndexPath, previewError: Bool)
func avatarTapped(indexPath: IndexPath)
func textTapped(indexPath: IndexPath)
Expand Down
14 changes: 9 additions & 5 deletions deltachat-ios/Chat/Views/MessageLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,16 @@ open class MessageLabel: UILabel {
// transformedAddressComponents[key.rawValue] = value
// }
// handleAddress(transformedAddressComponents)
case let .phoneNumber(phoneNumber):
guard let phoneNumber = phoneNumber else { return false }
handlePhoneNumber(phoneNumber)
// 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)
handleLongPressURL(url)
return true

// case let .custom(pattern, match):
// guard let match = match else { return false }
Expand All @@ -506,7 +507,6 @@ open class MessageLabel: UILabel {
default:
return false
}
return false
}

/// swiftlint:disable cyclomatic_complexity
Expand Down Expand Up @@ -561,6 +561,10 @@ open class MessageLabel: UILabel {
private func handleURL(_ url: URL) {
delegate?.didSelectURL(url)
}

private func handleLongPressURL(_ url: URL) {
delegate?.didLongPressURL(url)
}

private func handlePhoneNumber(_ phoneNumber: String) {
delegate?.didSelectPhoneNumber(phoneNumber)
Expand Down
3 changes: 3 additions & 0 deletions deltachat-ios/Chat/Views/MessageLabelDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public protocol MessageLabelDelegate: AnyObject {
/// - Parameters:
/// - url: The selected URL.
func didSelectURL(_ url: URL)

/// Triggerd when a long press occurs on a detected URL
func didLongPressURL(_ url: URL)

/// Triggered when a tap occurs on detected transit information.
///
Expand Down
6 changes: 6 additions & 0 deletions deltachat-ios/Controller/ContactDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ class ContactDetailViewController: UITableViewController {
}

extension ContactDetailViewController: MultilineLabelCellDelegate {

func phoneNumberTapped(number: String) {
let sanitizedNumber = number.filter("0123456789".contains)
if let phoneURL = URL(string: "tel://\(sanitizedNumber)") {
Expand All @@ -608,4 +609,9 @@ extension ContactDetailViewController: MultilineLabelCellDelegate {
UIApplication.shared.open(url)
}
}

func urlLongTapped(url: URL) {
// TODO: custom behaviour, also make sure it gets called, currently it's not implemented in the contacts detail table view
self.urlTapped(url: url)
}
}
5 changes: 5 additions & 0 deletions deltachat-ios/View/MultilineLabelCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ extension MultilineLabelCell: MessageLabelDelegate {
public func didSelectURL(_ url: URL) {
multilineDelegate?.urlTapped(url: url)
}

public func didLongPressURL(_ url: URL) {
multilineDelegate?.urlLongTapped(url: url)
}

public func didSelectTransitInformation(_ transitInformation: [String: String]) {}

Expand All @@ -96,4 +100,5 @@ extension MultilineLabelCell: MessageLabelDelegate {
public protocol MultilineLabelCellDelegate: AnyObject {
func phoneNumberTapped(number: String)
func urlTapped(url: URL)
func urlLongTapped(url: URL)
}

0 comments on commit 74d77ac

Please sign in to comment.