From 2276565c1c459875dc015ca0cfbf87eea1a619f4 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Wed, 26 Apr 2023 11:02:43 +0200 Subject: [PATCH] Add email, phone number and url link in MultilineMessageCell --- .../ChatConversationTableViewSwift.swift | 1 + .../Chat/Views/MultilineMessageCell.swift | 61 ++++++++++++++++--- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift index 9e478655e..5ccdab993 100644 --- a/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift +++ b/Classes/Swift/Chat/Views/ChatConversationTableViewSwift.swift @@ -101,6 +101,7 @@ class ChatConversationTableViewSwift: UIViewController, UICollectionViewDataSour if ChatConversationTableViewModel.sharedModel.getNBMessages() > 0 { scrollToBottom(animated: false) } + collectionView.reloadData() } func scrollToMessage(message: ChatMessage){ diff --git a/Classes/Swift/Chat/Views/MultilineMessageCell.swift b/Classes/Swift/Chat/Views/MultilineMessageCell.swift index 5bbb1647c..4789fca03 100644 --- a/Classes/Swift/Chat/Views/MultilineMessageCell.swift +++ b/Classes/Swift/Chat/Views/MultilineMessageCell.swift @@ -815,7 +815,8 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI - checkIfIsLink(content: content.utf8Text.trimmingCharacters(in: .whitespacesAndNewlines)) + checkIfIsLinkOrPhoneNumber(content: content.utf8Text) + NSLayoutConstraint.deactivate(labelHiddenConstraints) @@ -996,11 +997,23 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI } } - func checkIfIsLink(content: String){ + func checkIfIsLinkOrPhoneNumber(content: String){ let input = content - let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) - matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) + let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.phoneNumber.rawValue | NSTextCheckingResult.CheckingType.link.rawValue) + + let regex = try! NSRegularExpression(pattern: "sips:(\\S+)") + + + //matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) + //let matchesSips = regex.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) + + let matchesSips = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) + matches = regex.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) + + for matcheSips in matchesSips { + matches.append(matcheSips) + } let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineSpacing = 1 @@ -1021,18 +1034,48 @@ class MultilineMessageCell: UICollectionViewCell, UICollectionViewDataSource, UI label.addGestureRecognizer(tap) } - label.attributedText = attributedString; + label.attributedText = attributedString } @objc func handleTapOnLabel(_ sender: UITapGestureRecognizer) { matches.forEach { match in if sender.didTapAttributedTextInLabel(label: label, inRange: match.range) { + + let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" + let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) + if let url = match.url { - if #available(iOS 10.0, *) { - UIApplication.shared.open(url, options: [:], completionHandler: nil) - } else { - UIApplication.shared.openURL(url) + + if url.absoluteString.hasPrefix("sip:") || url.absoluteString.hasPrefix("sips:"){ + let view: DialerView = self.VIEW(DialerView.compositeViewDescription()) + CallManager.instance().nextCallIsTransfer = true + PhoneMainView.instance().changeCurrentView(view.compositeViewDescription()) + view.addressField.text = url.absoluteString + }else if emailTest.evaluate(with: url.absoluteString){ + if let urlWithMailTo = URL(string: "mailto:\(url.absoluteString)") { + if #available(iOS 10.0, *) { + UIApplication.shared.open(urlWithMailTo, options: [:], completionHandler: nil) + } else { + UIApplication.shared.openURL(urlWithMailTo) + } + } + }else{ + if #available(iOS 10.0, *) { + UIApplication.shared.open(url, options: [:], completionHandler: nil) + } else { + UIApplication.shared.openURL(url) + } } + }else if let phoneNumber = match.phoneNumber { + let view: DialerView = self.VIEW(DialerView.compositeViewDescription()) + CallManager.instance().nextCallIsTransfer = true + PhoneMainView.instance().changeCurrentView(view.compositeViewDescription()) + view.addressField.text = phoneNumber + }else if let sips = label.attributedText { + let view: DialerView = self.VIEW(DialerView.compositeViewDescription()) + CallManager.instance().nextCallIsTransfer = true + PhoneMainView.instance().changeCurrentView(view.compositeViewDescription()) + view.addressField.text = (sips.string as NSString).substring(with: match.range) } } }