Skip to content

Commit 07c0e58

Browse files
SD10zhongwuzw
authored andcommitted
Remove the limitation of 1 cell to 1 section (MessageKit#601)
* Add numberOfSections delegate method * Remove numberOfMessages(in) and add other two substitution methods * Fix tests failure
1 parent 58704a2 commit 07c0e58

6 files changed

Lines changed: 27 additions & 11 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa
88

99
### Added
1010

11+
- **Breaking Change** Added new `MessagesDataSource` delegate methods, `numberOfSections(in)` and `numberOfItems(inSection:in)`.
12+
[#601](https://github.com/MessageKit/MessageKit/pull/601) by [@SD10](https://github.com/sd10), [@zhongwuzw](https://github.com/zhongwuzw).
13+
1114
- **Breaking Change** Added new protocol `MediaItem` as the associated value for the
1215
`MessageData.video` and `MessageData.photo` cases.
1316
[#587](https://github.com/MessageKit/MessageKit/pull/587) by [@SD10](https://github.com/sd10).
@@ -71,6 +74,9 @@ You must now set this font explicitly through the `emojiMessageSizeCalculator` o
7174

7275
### Removed
7376

77+
- **Breaking Change** Removed `numberOfMessages(in)` method of `MessagesDataSource`, instead please use `numberOfSections(in)` and `numberOfItems(inSection:in)`.
78+
[#601](https://github.com/MessageKit/MessageKit/pull/601) by [@SD10](https://github.com/sd10), [@zhongwuzw](https://github.com/zhongwuzw).
79+
7480
- **Breaking Change** Removed the `messageLabelFont` property from `MessagesCollectionViewFlowLayout`.
7581
You can now set this property through `textMessageSizeCalculator` property.
7682
[#579](https://github.com/MessageKit/MessageKit/pull/579) by [@SD10](https://github.com/sd10).

Example/Sources/ConversationViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ extension ConversationViewController: MessagesDataSource {
260260
func currentSender() -> Sender {
261261
return SampleData.shared.currentSender
262262
}
263-
264-
func numberOfMessages(in messagesCollectionView: MessagesCollectionView) -> Int {
263+
264+
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
265265
return messageList.count
266266
}
267267

Sources/Controllers/MessagesViewController.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,14 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
160160
guard let collectionView = collectionView as? MessagesCollectionView else {
161161
fatalError(MessageKitError.notMessagesCollectionView)
162162
}
163-
// Each message is its own section
164-
return collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
163+
return collectionView.messagesDataSource?.numberOfSections(in: collectionView) ?? 0
165164
}
166165

167166
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
168167
guard let collectionView = collectionView as? MessagesCollectionView else {
169168
fatalError(MessageKitError.notMessagesCollectionView)
170169
}
171-
let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
172-
// There will only ever be 1 message per section
173-
return messageCount > 0 ? 1 : 0
170+
return collectionView.messagesDataSource?.numberOfItems(inSection: section, in: collectionView) ?? 0
174171
}
175172

176173
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

Sources/Protocols/MessagesDataSource.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ public protocol MessagesDataSource: AnyObject {
4545
/// - messagesCollectionView: The `MessagesCollectionView` in which the message will be displayed.
4646
func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType
4747

48-
/// The number of messages to be displayed in the `MessagesCollectionView`.
48+
/// The number of sections to be displayed in the `MessagesCollectionView`.
4949
///
5050
/// - Parameters:
5151
/// - messagesCollectionView: The `MessagesCollectionView` in which the messages will be displayed.
52-
func numberOfMessages(in messagesCollectionView: MessagesCollectionView) -> Int
52+
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int
53+
54+
/// The number of cells to be displayed in the `MessagesCollectionView`.
55+
///
56+
/// - Parameters:
57+
/// - section: The number of the section in which the cells will be displayed.
58+
/// - messagesCollectionView: The `MessagesCollectionView` in which the messages will be displayed.
59+
/// - Note:
60+
/// The default implementation of this method returns 1. Putting each message in its own section.
61+
func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int
5362

5463
/// The attributed text to be used for cell's top label.
5564
///
@@ -79,6 +88,10 @@ public extension MessagesDataSource {
7988
return message.sender == currentSender()
8089
}
8190

91+
func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int {
92+
return 1
93+
}
94+
8295
func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
8396
return nil
8497
}

Tests/ControllersTest/MessagesViewControllerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class MessagesViewControllerTests: XCTestCase {
8787
sut.messagesCollectionView.reloadData()
8888

8989
let count = sut.messagesCollectionView.numberOfSections
90-
let expectedCount = messagesDataSource.numberOfMessages(in: sut.messagesCollectionView)
90+
let expectedCount = messagesDataSource.numberOfSections(in: sut.messagesCollectionView)
9191

9292
XCTAssertEqual(count, expectedCount)
9393
}

Tests/Mocks/MockMessagesDataSource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class MockMessagesDataSource: MessagesDataSource {
3535
return senders[0]
3636
}
3737

38-
func numberOfMessages(in messagesCollectionView: MessagesCollectionView) -> Int {
38+
func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
3939
return messages.count
4040
}
4141

0 commit comments

Comments
 (0)