Skip to content

Commit

Permalink
Merge branch '3.0.0-beta' into typingIndicatorView
Browse files Browse the repository at this point in the history
  • Loading branch information
nathantannar4 committed Apr 3, 2019
2 parents d19e7df + a058885 commit 3f93702
Show file tree
Hide file tree
Showing 18 changed files with 339 additions and 69 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa

### Added

- **Breaking Change** Added `.hashtag`, .`mention` to detect theses pattern inside the `messageLabel`. We also add `.custom(pattern: YOUR_PATTERN)` to `DetectorType` to manage and deal with your own regular expression.
[#913](https://github.com/MessageKit/MessageKit/pull/913) by [@JulienKode](https://github.com/julienkode).

- Added support for detection and handling of `NSLink`s inside of messages.
[#815](https://github.com/MessageKit/MessageKit/pull/815) by [@jnic](https://github.com/jnic)

Expand Down
54 changes: 23 additions & 31 deletions Example/Sources/Data Generation/SampleData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,24 @@ final internal class SampleData {
static let shared = SampleData()

private init() {}

enum MessageTypes: UInt32, CaseIterable {
case Text = 0
case AttributedText = 1
case Photo = 2
case Video = 3
case Audio = 4
case Emoji = 5
case Location = 6
case Url = 7
case Phone = 8
case Custom = 9

static func random() -> MessageTypes {
// Update as new enumerations are added
let maxValue = Custom.rawValue

let rand = arc4random_uniform(maxValue+1)
return MessageTypes(rawValue: rand)!
}

enum MessageTypes: String, CaseIterable {
case Text
case AttributedText
case Photo
case Video
case Audio
case Emoji
case Location
case Url
case Phone
case Custom
}

let system = MockUser(id: "000000", displayName: "System")
let nathan = MockUser(id: "000001", displayName: "Nathan Tannar")
let steven = MockUser(id: "000002", displayName: "Steven Deutsch")
let wu = MockUser(id: "000003", displayName: "Wu Zhong")
let system = MockUser(senderId: "000000", displayName: "System")
let nathan = MockUser(senderId: "000001", displayName: "Nathan Tannar")
let steven = MockUser(senderId: "000002", displayName: "Steven Deutsch")
let wu = MockUser(senderId: "000003", displayName: "Wu Zhong")

lazy var senders = [nathan, steven, wu]

Expand Down Expand Up @@ -141,13 +133,13 @@ final internal class SampleData {
}

func randomMessageType() -> MessageTypes {
let messageType = MessageTypes.random()

if !UserDefaults.standard.bool(forKey: "\(messageType)" + " Messages") {
return randomMessageType()
var messageTypes = [MessageTypes]()
for type in MessageTypes.allCases {
if UserDefaults.standard.bool(forKey: "\(type.rawValue)" + " Messages") {
messageTypes.append(type)
}
}

return messageType
return messageTypes.random()!
}

func randomMessage(allowedSenders: [MockUser]) -> MockMessage {
Expand Down Expand Up @@ -230,7 +222,7 @@ final internal class SampleData {
let firstName = sender.displayName.components(separatedBy: " ").first
let lastName = sender.displayName.components(separatedBy: " ").first
let initials = "\(firstName?.first ?? "A")\(lastName?.first ?? "A")"
switch sender.id {
switch sender.senderId {
case "000001":
return Avatar(image: #imageLiteral(resourceName: "Nathan-Tannar"), initials: initials)
case "000002":
Expand Down
2 changes: 1 addition & 1 deletion Example/Sources/Models/MockUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ import Foundation
import MessageKit

struct MockUser: SenderType, Equatable {
var id: String
var senderId: String
var displayName: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,14 @@ extension AdvancedExampleViewController: MessagesDisplayDelegate {
}

func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedString.Key: Any] {
return MessageLabel.defaultAttributes
switch detector {
case .hashtag, .mention: return [.foregroundColor: UIColor.blue]
default: return MessageLabel.defaultAttributes
}
}

func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] {
return [.url, .address, .phoneNumber, .date, .transitInformation]
return [.url, .address, .phoneNumber, .date, .transitInformation, .mention, .hashtag]
}

// MARK: - All Messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ extension BasicExampleViewController: MessagesDisplayDelegate {
}

func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedString.Key: Any] {
return MessageLabel.defaultAttributes
switch detector {
case .hashtag, .mention: return [.foregroundColor: UIColor.blue]
default: return MessageLabel.defaultAttributes
}
}

func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] {
return [.url, .address, .phoneNumber, .date, .transitInformation]
return [.url, .address, .phoneNumber, .date, .transitInformation, .mention, .hashtag]
}

// MARK: - All Messages
Expand Down
14 changes: 13 additions & 1 deletion Example/Sources/View Controllers/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,19 @@ extension ChatViewController: MessageLabelDelegate {
func didSelectTransitInformation(_ transitInformation: [String: String]) {
print("TransitInformation Selected: \(transitInformation)")
}


func didSelectHashtag(_ hashtag: String) {
print("Hashtag selected: \(hashtag)")
}

func didSelectMention(_ mention: String) {
print("Mention selected: \(mention)")
}

func didSelectCustom(_ pattern: String, match: String?) {
print("Custom data detector patter selected: \(pattern)")
}

}

// MARK: - MessageInputBarDelegate
Expand Down
9 changes: 6 additions & 3 deletions MessageKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
38F8063521740DAD00CDB9DB /* TypingBubble.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38F8063421740DAD00CDB9DB /* TypingBubble.swift */; };
38F8063721740DD500CDB9DB /* TypingIndicatorCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38F8063621740DD500CDB9DB /* TypingIndicatorCell.swift */; };
4C508649221C0BBA0043943C /* AccessoryPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C508648221C0BBA0043943C /* AccessoryPosition.swift */; };
4C508649221C0BBA0043943C /* AccessoryPosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C508648221C0BBA0043943C /* AccessoryPosition.swift */; };
5073C1152175BE750040EAD5 /* AudioItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5073C1142175BE750040EAD5 /* AudioItem.swift */; };
5073C1192175BE960040EAD5 /* AudioMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5073C1182175BE950040EAD5 /* AudioMessageCell.swift */; };
5073C11D2175BEC60040EAD5 /* AudioMessageSizeCalculator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5073C11C2175BEC60040EAD5 /* AudioMessageSizeCalculator.swift */; };
Expand Down Expand Up @@ -154,6 +155,8 @@
38F8063421740DAD00CDB9DB /* TypingBubble.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypingBubble.swift; sourceTree = "<group>"; };
38F8063621740DD500CDB9DB /* TypingIndicatorCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypingIndicatorCell.swift; sourceTree = "<group>"; };
4C508648221C0BBA0043943C /* AccessoryPosition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccessoryPosition.swift; sourceTree = "<group>"; };
38F8062D2173CD4300CDB9DB /* MockUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUser.swift; sourceTree = "<group>"; };
4C508648221C0BBA0043943C /* AccessoryPosition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccessoryPosition.swift; sourceTree = "<group>"; };
5073C1142175BE750040EAD5 /* AudioItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AudioItem.swift; sourceTree = "<group>"; };
5073C1182175BE950040EAD5 /* AudioMessageCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AudioMessageCell.swift; sourceTree = "<group>"; };
5073C11C2175BEC60040EAD5 /* AudioMessageSizeCalculator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AudioMessageSizeCalculator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -255,12 +258,12 @@
path = Cells;
sourceTree = "<group>";
};
2EB618F11F846899007FBA0E /* Headers & Footers */ = {
2EB618F11F846899007FBA0E /* HeadersFooters */ = {
isa = PBXGroup;
children = (
1F6C040D206A2AF4007BDE44 /* MessageReusableView.swift */,
);
path = "Headers & Footers";
path = HeadersFooters;
sourceTree = "<group>";
};
5073C1212175C1580040EAD5 /* Resources */ = {
Expand Down Expand Up @@ -424,7 +427,7 @@
isa = PBXGroup;
children = (
2EB618F01F84676A007FBA0E /* Cells */,
2EB618F11F846899007FBA0E /* Headers & Footers */,
2EB618F11F846899007FBA0E /* HeadersFooters */,
B7A03F3E1F86694F006AEF79 /* AvatarView.swift */,
38F8063121740D9D00CDB9DB /* BubbleCircle.swift */,
38F8063421740DAD00CDB9DB /* TypingBubble.swift */,
Expand Down
38 changes: 32 additions & 6 deletions Sources/Models/DetectorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@

import Foundation

public enum DetectorType {
public enum DetectorType: Hashable {

case address
case date
case phoneNumber
case url
case transitInformation
case custom(NSRegularExpression)

// MARK: - Not supported yet

//case mention
//case hashtag
//case custom
// swiftlint:disable force_try
public static var hashtag = DetectorType.custom(try! NSRegularExpression(pattern: "#[a-zA-Z0-9]{4,}", options: []))
public static var mention = DetectorType.custom(try! NSRegularExpression(pattern: "@[a-zA-Z0-9]{4,}", options: []))
// swiftlint:enable force_try

internal var textCheckingType: NSTextCheckingResult.CheckingType {
switch self {
Expand All @@ -45,6 +45,32 @@ public enum DetectorType {
case .phoneNumber: return .phoneNumber
case .url: return .link
case .transitInformation: return .transitInformation
case .custom: return .regularExpression
}
}

/// Simply check if the detector type is a .custom
public var isCustom: Bool {
switch self {
case .custom: return true
default: return false
}
}

///The hashValue of the `DetectorType` so we can conform to `Hashable` and be sorted.
public var hashValue: Int {
return self.toInt()
}

/// Return an 'Int' value for each `DetectorType` type so `DetectorType` can conform to `Hashable`
private func toInt() -> Int {
switch self {
case .address: return 0
case .date: return 1
case .phoneNumber: return 2
case .url: return 3
case .transitInformation: return 4
case .custom(let regex): return regex.hashValue
}
}

Expand Down
16 changes: 13 additions & 3 deletions Sources/Models/Sender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,25 @@ public struct Sender: SenderType {
/// The unique String identifier for the sender.
///
/// Note: This value must be unique across all senders.
public let id: String
public let senderId: String

@available(*, deprecated: 3.0.0, message: "`id` has been renamed `senderId` as defined in the `SenderType` protocol")
public var id: String {
return senderId
}

/// The display name of a sender.
public let displayName: String

// MARK: - Intializers

public init(id: String, displayName: String) {
self.id = id
public init(senderId: String, displayName: String) {
self.senderId = senderId
self.displayName = displayName
}

@available(*, deprecated: 3.0.0, message: "`id` has been renamed `senderId` as defined in the `SenderType` protocol")
public init(id: String, displayName: String) {
self.init(senderId: id, displayName: displayName)
}
}
25 changes: 25 additions & 0 deletions Sources/Protocols/MessageLabelDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ public protocol MessageLabelDelegate: AnyObject {
/// - Parameters:
/// - transitInformation: The selected transit information.
func didSelectTransitInformation(_ transitInformation: [String: String])

/// Triggered when a tap occurs on a mention
///
/// - Parameters:
/// - mention: The selected mention
func didSelectMention(_ mention: String)

/// Triggered when a tap occurs on a hashtag
///
/// - Parameters:
/// - mention: The selected hashtag
func didSelectHashtag(_ hashtag: String)

/// Triggered when a tap occurs on a custom regular expression
///
/// - Parameters:
/// - pattern: the pattern of the regular expression
/// - match: part that match with the regular expression
func didSelectCustom(_ pattern: String, match: String?)

}

Expand All @@ -71,4 +90,10 @@ public extension MessageLabelDelegate {

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

func didSelectMention(_ mention: String) {}

func didSelectHashtag(_ hashtag: String) {}

func didSelectCustom(_ pattern: String, match: String?) {}

}
2 changes: 1 addition & 1 deletion Sources/Protocols/MessagesDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public protocol MessagesDataSource: AnyObject {
public extension MessagesDataSource {

func isFromCurrentSender(message: MessageType) -> Bool {
return message.sender.id == currentSender().id
return message.sender.senderId == currentSender().senderId
}

func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Protocols/SenderType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public protocol SenderType {
/// The unique String identifier for the sender.
///
/// Note: This value must be unique across all senders.
var id: String { get }
var senderId: String { get }

/// The display name of a sender.
var displayName: String { get }
Expand Down
Loading

0 comments on commit 3f93702

Please sign in to comment.