Merge pull request #520 from nosarj/enhancement/transitInfoDetection

Enhancement/transit info detection
This commit is contained in:
Steven Deutsch
2018-03-04 06:54:00 -06:00
committed by GitHub
6 changed files with 48 additions and 1 deletions
+3
View File
@@ -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).
@@ -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)")
}
}
+2
View File
@@ -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
}
}
@@ -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]) {}
}
+27
View File
@@ -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]?)
}
+6
View File
@@ -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))
}
}
}
}
}