Skip to content

Commit 762f583

Browse files
committed
Refactor to see what SenderType would look like
1 parent 4b14db1 commit 762f583

13 files changed

Lines changed: 140 additions & 59 deletions

File tree

Example/ChatExample.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
383B9EB32172A1C4008AB91A /* MockUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383B9EB22172A1C4008AB91A /* MockUser.swift */; };
1011
385C2922211FF32E0010B4BA /* CustomCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 385C2920211FF32E0010B4BA /* CustomCell.swift */; };
1112
385C2923211FF32E0010B4BA /* TableViewCells.swift in Sources */ = {isa = PBXBuildFile; fileRef = 385C2921211FF32E0010B4BA /* TableViewCells.swift */; };
1213
385C2927211FF33B0010B4BA /* MockSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 385C2925211FF33A0010B4BA /* MockSocket.swift */; };
@@ -67,6 +68,7 @@
6768
/* Begin PBXFileReference section */
6869
0364943D08CDBE656E6F6DF8 /* Pods-ChatExampleTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ChatExampleTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ChatExampleTests/Pods-ChatExampleTests.debug.xcconfig"; sourceTree = "<group>"; };
6970
2AC6E3F5C11E39F57598DBE6 /* Pods_ChatExampleUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ChatExampleUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
71+
383B9EB22172A1C4008AB91A /* MockUser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUser.swift; sourceTree = "<group>"; };
7072
385C2920211FF32E0010B4BA /* CustomCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomCell.swift; sourceTree = "<group>"; };
7173
385C2921211FF32E0010B4BA /* TableViewCells.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewCells.swift; sourceTree = "<group>"; };
7274
385C2925211FF33A0010B4BA /* MockSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MockSocket.swift; sourceTree = "<group>"; };
@@ -146,6 +148,7 @@
146148
children = (
147149
385C2926211FF33B0010B4BA /* MockMessage.swift */,
148150
385C2925211FF33A0010B4BA /* MockSocket.swift */,
151+
383B9EB22172A1C4008AB91A /* MockUser.swift */,
149152
);
150153
path = Models;
151154
sourceTree = "<group>";
@@ -512,6 +515,7 @@
512515
385C2945211FF38F0010B4BA /* NavigationController.swift in Sources */,
513516
385C2948211FF38F0010B4BA /* ChatViewController.swift in Sources */,
514517
385C2944211FF38F0010B4BA /* MessageContainerController.swift in Sources */,
518+
383B9EB32172A1C4008AB91A /* MockUser.swift in Sources */,
515519
385C2943211FF38F0010B4BA /* SettingsViewController.swift in Sources */,
516520
385C2927211FF33B0010B4BA /* MockSocket.swift in Sources */,
517521
385C2947211FF38F0010B4BA /* BasicExampleViewController.swift in Sources */,

Example/Sources/Data Generation/SampleData.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ final internal class SampleData {
5151
}
5252
}
5353

54-
let system = Sender(id: "000000", displayName: "System")
55-
let nathan = Sender(id: "000001", displayName: "Nathan Tannar")
56-
let steven = Sender(id: "000002", displayName: "Steven Deutsch")
57-
let wu = Sender(id: "000003", displayName: "Wu Zhong")
54+
let system = MockUser(id: "000000", displayName: "System")
55+
let nathan = MockUser(id: "000001", displayName: "Nathan Tannar")
56+
let steven = MockUser(id: "000002", displayName: "Steven Deutsch")
57+
let wu = MockUser(id: "000003", displayName: "Wu Zhong")
5858

5959
lazy var senders = [nathan, steven, wu]
6060

61-
var currentSender: Sender {
61+
var currentSender: MockUser {
6262
return nathan
6363
}
6464

@@ -150,7 +150,7 @@ final internal class SampleData {
150150
let randomNumberSender = Int(arc4random_uniform(UInt32(allowedSenders.count)))
151151

152152
let uniqueID = NSUUID().uuidString
153-
let sender = allowedSenders[randomNumberSender]
153+
let user = allowedSenders[randomNumberSender]
154154
let date = dateAddingRandomTime()
155155

156156
switch randomMessageType() {
@@ -206,7 +206,7 @@ final internal class SampleData {
206206
completion(messages)
207207
}
208208

209-
func getMessages(count: Int, allowedSenders: [Sender], completion: ([MockMessage]) -> Void) {
209+
func getMessages(count: Int, allowedSenders: [MockUser], completion: ([MockMessage]) -> Void) {
210210
var messages: [MockMessage] = []
211211
// Disable Custom Messages
212212
UserDefaults.standard.set(false, forKey: "Custom Messages")
@@ -217,18 +217,18 @@ final internal class SampleData {
217217
completion(messages)
218218
}
219219

220-
func getAvatarFor(sender: Sender) -> Avatar {
220+
func getAvatarFor(sender: SenderType) -> Avatar {
221221
let firstName = sender.displayName.components(separatedBy: " ").first
222222
let lastName = sender.displayName.components(separatedBy: " ").first
223223
let initials = "\(firstName?.first ?? "A")\(lastName?.first ?? "A")"
224-
switch sender {
225-
case nathan:
224+
switch sender.id {
225+
case "000001":
226226
return Avatar(image: #imageLiteral(resourceName: "Nathan-Tannar"), initials: initials)
227-
case steven:
227+
case "000002":
228228
return Avatar(image: #imageLiteral(resourceName: "Steven-Deutsch"), initials: initials)
229-
case wu:
229+
case "000003":
230230
return Avatar(image: #imageLiteral(resourceName: "Wu-Zhong"), initials: initials)
231-
case system:
231+
case "000000":
232232
return Avatar(image: nil, initials: "SS")
233233
default:
234234
return Avatar(image: nil, initials: initials)

Example/Sources/Models/MockMessage.swift

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,50 @@ private struct ImageMediaItem: MediaItem {
5656
internal struct MockMessage: MessageType {
5757

5858
var messageId: String
59-
var sender: Sender
59+
var sender: SenderType {
60+
return user
61+
}
6062
var sentDate: Date
6163
var kind: MessageKind
6264

63-
private init(kind: MessageKind, sender: Sender, messageId: String, date: Date) {
65+
var user: MockUser
66+
67+
private init(kind: MessageKind, user: MockUser, messageId: String, date: Date) {
6468
self.kind = kind
65-
self.sender = sender
69+
self.user = user
6670
self.messageId = messageId
6771
self.sentDate = date
6872
}
6973

70-
init(custom: Any?, sender: Sender, messageId: String, date: Date) {
71-
self.init(kind: .custom(custom), sender: sender, messageId: messageId, date: date)
74+
init(custom: Any?, user: MockUser, messageId: String, date: Date) {
75+
self.init(kind: .custom(custom), user: user, messageId: messageId, date: date)
7276
}
7377

74-
init(text: String, sender: Sender, messageId: String, date: Date) {
75-
self.init(kind: .text(text), sender: sender, messageId: messageId, date: date)
78+
init(text: String, user: MockUser, messageId: String, date: Date) {
79+
self.init(kind: .text(text), user: user, messageId: messageId, date: date)
7680
}
7781

78-
init(attributedText: NSAttributedString, sender: Sender, messageId: String, date: Date) {
79-
self.init(kind: .attributedText(attributedText), sender: sender, messageId: messageId, date: date)
82+
init(attributedText: NSAttributedString, user: MockUser, messageId: String, date: Date) {
83+
self.init(kind: .attributedText(attributedText), user: user, messageId: messageId, date: date)
8084
}
8185

82-
init(image: UIImage, sender: Sender, messageId: String, date: Date) {
86+
init(image: UIImage, user: MockUser, messageId: String, date: Date) {
8387
let mediaItem = ImageMediaItem(image: image)
84-
self.init(kind: .photo(mediaItem), sender: sender, messageId: messageId, date: date)
88+
self.init(kind: .photo(mediaItem), user: user, messageId: messageId, date: date)
8589
}
8690

87-
init(thumbnail: UIImage, sender: Sender, messageId: String, date: Date) {
91+
init(thumbnail: UIImage, user: MockUser, messageId: String, date: Date) {
8892
let mediaItem = ImageMediaItem(image: thumbnail)
89-
self.init(kind: .video(mediaItem), sender: sender, messageId: messageId, date: date)
93+
self.init(kind: .video(mediaItem), user: user, messageId: messageId, date: date)
9094
}
9195

92-
init(location: CLLocation, sender: Sender, messageId: String, date: Date) {
96+
init(location: CLLocation, user: MockUser, messageId: String, date: Date) {
9397
let locationItem = CoordinateItem(location: location)
94-
self.init(kind: .location(locationItem), sender: sender, messageId: messageId, date: date)
98+
self.init(kind: .location(locationItem), user: user, messageId: messageId, date: date)
9599
}
96100

97-
init(emoji: String, sender: Sender, messageId: String, date: Date) {
98-
self.init(kind: .emoji(emoji), sender: sender, messageId: messageId, date: date)
101+
init(emoji: String, user: MockUser, messageId: String, date: Date) {
102+
self.init(kind: .emoji(emoji), user: user, messageId: messageId, date: date)
99103
}
100104

101105
}

Example/Sources/Models/MockSocket.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ final class MockSocket {
3737

3838
private var onTypingStatusCode: (() -> Void)?
3939

40-
private var connectedUsers: [Sender] = []
40+
private var connectedUsers: [MockUser] = []
4141

4242
private init() {}
4343

4444
@discardableResult
45-
func connect(with senders: [Sender]) -> Self {
45+
func connect(with senders: [MockUser]) -> Self {
4646
disconnect()
4747
connectedUsers = senders
4848
timer = Timer.scheduledTimer(timeInterval: 2.5, target: self, selector: #selector(handleTimer), userInfo: nil, repeats: true)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017-2018 MessageKit
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
import Foundation
26+
import MessageKit
27+
28+
struct MockUser: SenderType, Equatable {
29+
var id: String
30+
var displayName: String
31+
}

Example/Sources/View Controllers/AdvancedExampleViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ final class AdvancedExampleViewController: ChatViewController {
176176

177177
func isPreviousMessageSameSender(at indexPath: IndexPath) -> Bool {
178178
guard indexPath.section - 1 >= 0 else { return false }
179-
return messageList[indexPath.section].sender == messageList[indexPath.section - 1].sender
179+
return messageList[indexPath.section].user == messageList[indexPath.section - 1].user
180180
}
181181

182182
func isNextMessageSameSender(at indexPath: IndexPath) -> Bool {
183183
guard indexPath.section + 1 < messageList.count else { return false }
184-
return messageList[indexPath.section].sender == messageList[indexPath.section + 1].sender
184+
return messageList[indexPath.section].user == messageList[indexPath.section + 1].user
185185
}
186186

187187
func setTypingIndicatorHidden(_ isHidden: Bool, performUpdates updates: (() -> Void)? = nil) {

Example/Sources/View Controllers/ChatViewController.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class ChatViewController: MessagesViewController, MessagesDataSource {
138138

139139
// MARK: - MessagesDataSource
140140

141-
func currentSender() -> Sender {
141+
func currentSender() -> SenderType {
142142
return SampleData.shared.currentSender
143143
}
144144

@@ -233,12 +233,13 @@ extension ChatViewController: MessageInputBarDelegate {
233233
func messageInputBar(_ inputBar: MessageInputBar, didPressSendButtonWith text: String) {
234234

235235
for component in inputBar.inputTextView.components {
236-
236+
237+
let user = SampleData.shared.currentSender
237238
if let str = component as? String {
238-
let message = MockMessage(text: str, sender: currentSender(), messageId: UUID().uuidString, date: Date())
239+
let message = MockMessage(text: str, user: user, messageId: UUID().uuidString, date: Date())
239240
insertMessage(message)
240241
} else if let img = component as? UIImage {
241-
let message = MockMessage(image: img, sender: currentSender(), messageId: UUID().uuidString, date: Date())
242+
let message = MockMessage(image: img, user: user, messageId: UUID().uuidString, date: Date())
242243
insertMessage(message)
243244
}
244245

MessageKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
1FF377AA20087D78004FD648 /* MessagesViewController+Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FF377A920087D78004FD648 /* MessagesViewController+Menu.swift */; };
3636
1FF377AC20087DA2004FD648 /* MessagesViewController+Keyboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FF377AB20087DA2004FD648 /* MessagesViewController+Keyboard.swift */; };
3737
382C794221705D2000F4FAF5 /* HorizontalEdgeInsets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 382C794121705D2000F4FAF5 /* HorizontalEdgeInsets.swift */; };
38+
383B9EB121728BAD008AB91A /* SenderType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 383B9EB021728BAD008AB91A /* SenderType.swift */; };
3839
38C2AE7C20D4878D00F8079E /* MessageInputBar.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 38C2AE7B20D4878D00F8079E /* MessageInputBar.framework */; };
3940
88916B2D1CF0DF2F00469F91 /* MessageKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 88916B221CF0DF2F00469F91 /* MessageKit.framework */; };
4041
8962AC8A1F87AB7D0030B058 /* MessagesCollectionViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8962AC831F87AB230030B058 /* MessagesCollectionViewTests.swift */; };
@@ -130,6 +131,7 @@
130131
1FF377A920087D78004FD648 /* MessagesViewController+Menu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessagesViewController+Menu.swift"; sourceTree = "<group>"; };
131132
1FF377AB20087DA2004FD648 /* MessagesViewController+Keyboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessagesViewController+Keyboard.swift"; sourceTree = "<group>"; };
132133
382C794121705D2000F4FAF5 /* HorizontalEdgeInsets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HorizontalEdgeInsets.swift; sourceTree = "<group>"; };
134+
383B9EB021728BAD008AB91A /* SenderType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SenderType.swift; sourceTree = "<group>"; };
133135
38C2AE7B20D4878D00F8079E /* MessageInputBar.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MessageInputBar.framework; path = Carthage/Build/iOS/MessageInputBar.framework; sourceTree = "<group>"; };
134136
88916B221CF0DF2F00469F91 /* MessageKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = MessageKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
135137
88916B2C1CF0DF2F00469F91 /* MessageKitTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MessageKitTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -403,6 +405,7 @@
403405
B7A03F561F8669C9006AEF79 /* MessagesDisplayDelegate.swift */,
404406
B7A03F541F8669C9006AEF79 /* MessagesLayoutDelegate.swift */,
405407
B7A03F511F8669C9006AEF79 /* MessageType.swift */,
408+
383B9EB021728BAD008AB91A /* SenderType.swift */,
406409
1FD5895F2064E08A004B5081 /* MediaItem.swift */,
407410
1FD5896320660C1C004B5081 /* LocationItem.swift */,
408411
);
@@ -583,6 +586,7 @@
583586
B7A03F4B1F86694F006AEF79 /* MessageContainerView.swift in Sources */,
584587
B7A03F281F866895006AEF79 /* LocationMessageSnapshotOptions.swift in Sources */,
585588
B7A03F6C1F8669EB006AEF79 /* UIView+Extensions.swift in Sources */,
589+
383B9EB121728BAD008AB91A /* SenderType.swift in Sources */,
586590
B7A03F3A1F866946006AEF79 /* TextMessageCell.swift in Sources */,
587591
B7A03F191F86682C006AEF79 /* MessagesCollectionViewLayoutAttributes.swift in Sources */,
588592
B7A03F461F86694F006AEF79 /* AvatarView.swift in Sources */,

Sources/Models/Sender.swift

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import Foundation
2626

2727
/// An object that groups the metadata of a messages sender.
28-
public struct Sender {
28+
@available(*, deprecated: 2.0, message: "`Sender` has been replaced with the `SenderType` protocol in 2.0")
29+
public struct Sender: SenderType {
2930

3031
/// MARK: - Properties
3132

@@ -44,14 +45,3 @@ public struct Sender {
4445
self.displayName = displayName
4546
}
4647
}
47-
48-
// MARK: - Equatable Conformance
49-
50-
extension Sender: Equatable {
51-
52-
/// Two senders are considered equal if they have the same id.
53-
public static func == (left: Sender, right: Sender) -> Bool {
54-
return left.id == right.id
55-
}
56-
57-
}

Sources/Protocols/MessageType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Foundation
2929
public protocol MessageType {
3030

3131
/// The sender of the message.
32-
var sender: Sender { get }
32+
var sender: SenderType { get }
3333

3434
/// The unique identifier for the message.
3535
var messageId: String { get }

0 commit comments

Comments
 (0)