Skip to content

Commit 2ec67cb

Browse files
Option to customize the date separation logic in the message list
1 parent aad69ee commit 2ec67cb

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

66
### ✅ Added
77
- Added factory method for customizing the message list container's modifier
8+
- Option to customize the date separation logic in the message list
89

910
# [4.33.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.33.0)
1011
_June 09, 2023_

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public struct MessageDisplayOptions {
101101
public let messageLinkDisplayResolver: (ChatMessage) -> [NSAttributedString.Key: Any]
102102
public let spacerWidth: (CGFloat) -> CGFloat
103103
public let reactionsTopPadding: (ChatMessage) -> CGFloat
104+
public let dateSeparator: (ChatMessage, ChatMessage) -> Date?
104105

105106
public init(
106107
showAvatars: Bool = true,
@@ -118,7 +119,8 @@ public struct MessageDisplayOptions {
118119
messageLinkDisplayResolver: @escaping (ChatMessage) -> [NSAttributedString.Key: Any] = MessageDisplayOptions
119120
.defaultLinkDisplay,
120121
spacerWidth: @escaping (CGFloat) -> CGFloat = MessageDisplayOptions.defaultSpacerWidth,
121-
reactionsTopPadding: @escaping (ChatMessage) -> CGFloat = MessageDisplayOptions.defaultReactionsTopPadding
122+
reactionsTopPadding: @escaping (ChatMessage) -> CGFloat = MessageDisplayOptions.defaultReactionsTopPadding,
123+
dateSeparator: @escaping (ChatMessage, ChatMessage) -> Date? = MessageDisplayOptions.defaultDateSeparator
122124
) {
123125
self.showAvatars = showAvatars
124126
self.showAuthorName = showAuthorName
@@ -135,11 +137,25 @@ public struct MessageDisplayOptions {
135137
self.showAvatarsInGroups = showAvatarsInGroups ?? showAvatars
136138
self.reactionsTopPadding = reactionsTopPadding
137139
self.newMessagesSeparatorSize = newMessagesSeparatorSize
140+
self.dateSeparator = dateSeparator
138141
}
139142

140143
public func showAvatars(for channel: ChatChannel) -> Bool {
141144
channel.isDirectMessageChannel ? showAvatars : showAvatarsInGroups
142145
}
146+
147+
public static func defaultDateSeparator(message: ChatMessage, previous: ChatMessage) -> Date? {
148+
let isDifferentDay = !Calendar.current.isDate(
149+
message.createdAt,
150+
equalTo: previous.createdAt,
151+
toGranularity: .day
152+
)
153+
if isDifferentDay {
154+
return message.createdAt
155+
} else {
156+
return nil
157+
}
158+
}
143159

144160
public static var defaultLinkDisplay: (ChatMessage) -> [NSAttributedString.Key: Any] {
145161
{ _ in

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListDateUtils.swift

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,9 @@ class MessageListDateUtils {
6363
let previousIndex = index + 1
6464
if previousIndex < messages.count {
6565
let previous = messages[previousIndex]
66-
let isDifferentDay = !Calendar.current.isDate(
67-
message.createdAt,
68-
equalTo: previous.createdAt,
69-
toGranularity: .day
70-
)
71-
if isDifferentDay {
72-
return message.createdAt
73-
} else {
74-
return nil
75-
}
66+
return messageListConfig
67+
.messageDisplayOptions
68+
.dateSeparator(message, previous)
7669
} else {
7770
return message.createdAt
7871
}

0 commit comments

Comments
 (0)