Skip to content

Commit e80c6f0

Browse files
Added time interval config for grouping messages
1 parent fdeed04 commit e80c6f0

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

Sources/StreamChatSwiftUI/ChatChannel/ChatChannelViewModel.swift

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,23 +345,34 @@ open class ChatChannelViewModel: ObservableObject, MessagesDataSource {
345345

346346
private func groupMessages() {
347347
var temp = [String: [String]]()
348+
let primary = "primary"
348349
for (index, message) in messages.enumerated() {
349-
let dateString = messagesDateFormatter.string(from: message.createdAt)
350-
let prefix = messageCachingUtils.authorId(for: message)
351-
let key = "\(prefix)-\(dateString)"
352-
if temp[key] == nil {
353-
temp[key] = [message.id]
354-
} else {
355-
// check if the previous message is not sent by the same user.
356-
let previousIndex = index - 1
357-
if previousIndex >= 0 {
358-
let previous = messages[previousIndex]
359-
let previousAuthorId = messageCachingUtils.authorId(for: previous)
360-
let shouldAddKey = prefix != previousAuthorId
361-
if shouldAddKey {
362-
temp[key]?.append(message.id)
363-
}
364-
}
350+
let date = message.createdAt
351+
if index == 0 {
352+
temp[message.id] = [primary]
353+
continue
354+
}
355+
356+
let previous = index - 1
357+
let previousMessage = messages[previous]
358+
let currentAuthorId = messageCachingUtils.authorId(for: message)
359+
let previousAuthorId = messageCachingUtils.authorId(for: previousMessage)
360+
361+
if currentAuthorId != previousAuthorId {
362+
temp[message.id] = [primary]
363+
}
364+
365+
if previousMessage.type == .error
366+
|| previousMessage.type == .ephemeral
367+
|| previousMessage.type == .system {
368+
temp[message.id] = [primary]
369+
continue
370+
}
371+
372+
let delay = previousMessage.createdAt.timeIntervalSince(date)
373+
374+
if delay > utils.messageListConfig.maxTimeIntervalBetweenMessagesInGroup {
375+
temp[message.id] = [primary]
365376
}
366377
}
367378

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public struct MessageListConfig {
1919
messagePopoverEnabled: Bool = true,
2020
doubleTapOverlayEnabled: Bool = false,
2121
becomesFirstResponderOnOpen: Bool = false,
22-
updateChannelsFromMessageList: Bool = false
22+
updateChannelsFromMessageList: Bool = false,
23+
maxTimeIntervalBetweenMessagesInGroup: TimeInterval = 60
2324
) {
2425
self.messageListType = messageListType
2526
self.typingIndicatorPlacement = typingIndicatorPlacement
@@ -32,6 +33,7 @@ public struct MessageListConfig {
3233
self.doubleTapOverlayEnabled = doubleTapOverlayEnabled
3334
self.becomesFirstResponderOnOpen = becomesFirstResponderOnOpen
3435
self.updateChannelsFromMessageList = updateChannelsFromMessageList
36+
self.maxTimeIntervalBetweenMessagesInGroup = maxTimeIntervalBetweenMessagesInGroup
3537
}
3638

3739
let messageListType: MessageListType
@@ -45,6 +47,7 @@ public struct MessageListConfig {
4547
let doubleTapOverlayEnabled: Bool
4648
let becomesFirstResponderOnOpen: Bool
4749
let updateChannelsFromMessageList: Bool
50+
let maxTimeIntervalBetweenMessagesInGroup: TimeInterval
4851
}
4952

5053
/// Contains information about the message paddings.

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListView.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,7 @@ public struct MessageListView<Factory: ViewFactory>: View, KeyboardReadable {
231231
if !messageListConfig.groupMessages {
232232
return true
233233
}
234-
let dateString = dateFormatter.string(from: message.createdAt)
235-
let prefix = utils.messageCachingUtils.authorId(for: message)
236-
let key = "\(prefix)-\(dateString)"
237-
let inMessagingGroup = messagesGroupingInfo[key]?.contains(message.id) ?? false
238-
return inMessagingGroup
234+
return messagesGroupingInfo[message.id] != nil
239235
}
240236

241237
private func handleLongPress(messageDisplayInfo: MessageDisplayInfo) {

StreamChatSwiftUITests/Tests/ChatChannel/ChatChannelViewModel_Tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ChatChannelViewModel_Tests: StreamChatTestCase {
4747
let messagesGroupingInfo = viewModel.messagesGroupingInfo
4848

4949
// Then
50-
XCTAssert(messagesGroupingInfo.count == 8)
50+
XCTAssert(messagesGroupingInfo.count == 1)
5151
for (_, groupingInfo) in messagesGroupingInfo {
5252
XCTAssert(groupingInfo.count == 1)
5353
}

docusaurus/docs/iOS/swiftui/components/message-list.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ The parameters that you can use in this method are:
191191
- `onLongPress`: called when the message is long pressed.
192192
- `isLast`: whether it is the last message (e.g. to apply extra padding).
193193

194+
## Grouping Messages
195+
196+
The messages are grouped based on the `maxTimeIntervalBetweenMessagesInGroup` value in the `MessageListConfig`. The default value of this property is 60 seconds, which means messages that are 60 seconds (or less) apart, will be grouped together. You can change this value when you initialize the `MessageListConfig`.
197+
194198
## System Messages
195199

196200
If you are using the default implementation of the message container view, you can customize the system messages. These are messages that are not sent by the participants, but they represent some system events (like people added to the channel and similar). In order to change the UI of the system messages, you need to implement the `makeSystemMessageView` in the `ViewFactory` protocol.

0 commit comments

Comments
 (0)