diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d95b5b9..224ae4d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa ### Added +- **Breaking Change** Adds new `DetectorType` called `.transitInformation` to message label. +[#520](https://github.com/MessageKit/MessageKit/pull/520) by [@nosarj](https://github.com/nosarj). + - **Breaking Change** Adds `.custom(Any?)` case to `MessageData`. [#498](https://github.com/MessageKit/MessageKit/pull/498) by [@SD10](https://github.com/SD10). diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index e7b7910b..97275ade 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -305,7 +305,7 @@ extension ConversationViewController: MessagesDisplayDelegate { } func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] { - return [.url, .address, .phoneNumber, .date] + return [.url, .address, .phoneNumber, .date, .transitInformation] } // MARK: - All Messages @@ -439,6 +439,10 @@ extension ConversationViewController: MessageLabelDelegate { func didSelectURL(_ url: URL) { print("URL Selected: \(url)") } + + func didSelectTransitInformation(_ transitInformation: [String : String]) { + print("TransitInformation Selected: \(transitInformation)") + } } diff --git a/Sources/Models/DetectorType.swift b/Sources/Models/DetectorType.swift index 55c48efc..20ea43f7 100644 --- a/Sources/Models/DetectorType.swift +++ b/Sources/Models/DetectorType.swift @@ -30,6 +30,7 @@ public enum DetectorType { case date case phoneNumber case url + case transitInformation // MARK: - Not supported yet @@ -43,6 +44,7 @@ public enum DetectorType { case .date: return .date case .phoneNumber: return .phoneNumber case .url: return .link + case .transitInformation: return .transitInformation } } diff --git a/Sources/Protocols/MessageLabelDelegate.swift b/Sources/Protocols/MessageLabelDelegate.swift index 1b749d7c..38e7194b 100644 --- a/Sources/Protocols/MessageLabelDelegate.swift +++ b/Sources/Protocols/MessageLabelDelegate.swift @@ -33,6 +33,8 @@ public protocol MessageLabelDelegate: AnyObject { func didSelectPhoneNumber(_ phoneNumber: String) func didSelectURL(_ url: URL) + + func didSelectTransitInformation(_ transitInformation: [String: String]) } @@ -45,5 +47,8 @@ public extension MessageLabelDelegate { func didSelectPhoneNumber(_ phoneNumber: String) {} func didSelectURL(_ url: URL) {} + + func didSelectTransitInformation(_ transitInformation: [String: String]) {} + } diff --git a/Sources/Views/MessageLabel.swift b/Sources/Views/MessageLabel.swift index c33a20be..f572be22 100644 --- a/Sources/Views/MessageLabel.swift +++ b/Sources/Views/MessageLabel.swift @@ -130,6 +130,9 @@ open class MessageLabel: UILabel { open internal(set) var phoneNumberAttributes: [NSAttributedStringKey: Any] = defaultAttributes open internal(set) var urlAttributes: [NSAttributedStringKey: Any] = defaultAttributes + + open internal(set) var transitInformationAttributes: [NSAttributedStringKey: Any] = defaultAttributes + public func setAttributes(_ attributes: [NSAttributedStringKey: Any], detector: DetectorType) { switch detector { @@ -141,6 +144,8 @@ open class MessageLabel: UILabel { dateAttributes = attributes case .url: urlAttributes = attributes + case .transitInformation: + transitInformationAttributes = attributes } if isConfiguring { attributesNeedUpdate = true @@ -268,6 +273,8 @@ open class MessageLabel: UILabel { return phoneNumberAttributes case .url: return urlAttributes + case .transitInformation: + return transitInformationAttributes } } @@ -282,6 +289,8 @@ open class MessageLabel: UILabel { return phoneNumberAttributes case .link: return urlAttributes + case .transitInformation: + return transitInformationAttributes default: fatalError(MessageKitError.unrecognizedCheckingResult) } @@ -324,6 +333,12 @@ open class MessageLabel: UILabel { let tuple: (NSRange, MessageTextCheckingType) = (result.range, .link(result.url)) ranges.append(tuple) rangesForDetectors.updateValue(ranges, forKey: .url) + case .transitInformation: + var ranges = rangesForDetectors[.transitInformation] ?? [] + let tuple: (NSRange, MessageTextCheckingType) = (result.range, .transitInfoComponents(result.components)) + ranges.append(tuple) + rangesForDetectors.updateValue(ranges, forKey: .transitInformation) + default: fatalError("Received an unrecognized NSTextCheckingResult.CheckingType") } @@ -391,6 +406,13 @@ open class MessageLabel: UILabel { case let .link(url): guard let url = url else { return } handleURL(url) + case let .transitInfoComponents(transitInformation): + var transformedTransitInformation = [String: String]() + guard let transitInformation = transitInformation else { return } + transitInformation.forEach { (key, value) in + transformedTransitInformation[key.rawValue] = value + } + handleTransitInformation(transformedTransitInformation) } } @@ -410,6 +432,10 @@ open class MessageLabel: UILabel { delegate?.didSelectPhoneNumber(phoneNumber) } + private func handleTransitInformation(_ components: [String: String]) { + delegate?.didSelectTransitInformation(components) + } + } private enum MessageTextCheckingType { @@ -417,4 +443,5 @@ private enum MessageTextCheckingType { case date(Date?) case phoneNumber(String?) case link(URL?) + case transitInfoComponents([NSTextCheckingKey: String]?) } diff --git a/Tests/DetectorTypeSpec.swift b/Tests/DetectorTypeSpec.swift index b77aa119..60426131 100644 --- a/Tests/DetectorTypeSpec.swift +++ b/Tests/DetectorTypeSpec.swift @@ -54,6 +54,12 @@ final class DetectorTypeSpec: QuickSpec { expect(phoneNumber).to(equal(NSTextCheckingResult.CheckingType.phoneNumber)) } } + context("case .transitInformation") { + it("should equal .transitInformation") { + let transitInformation = DetectorType.transitInformation.textCheckingType + expect(transitInformation).to(equal(NSTextCheckingResult.CheckingType.transitInformation)) + } + } } } }