Skip to content

Commit 88bd595

Browse files
authored
Allow customisation of message previews in lists (#839)
1 parent 71f50b9 commit 88bd595

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
- Add `Utils.originalTranslationsStore` to keep track of messages that should show the original text [#815](https://github.com/GetStream/stream-chat-swiftui/pull/815)
1414
- Add `ViewFactory.makeGalleryHeaderView` for customising header view in `GalleryView` [#837](https://github.com/GetStream/stream-chat-swiftui/pull/837)
1515
- Add `ViewFactory.makeVideoPlayerHeaderView` for customising header view in `VideoPlayerView` [#837](https://github.com/GetStream/stream-chat-swiftui/pull/837)
16+
- Add `Utils.messagePreviewFormatter` for customising message previews in lists [#839](https://github.com/GetStream/stream-chat-swiftui/pull/839)
1617
### 🐞 Fixed
1718
- Fix swipe to reply enabled when quoting a message is disabled [#824](https://github.com/GetStream/stream-chat-swiftui/pull/824)
1819
- Fix mark unread action not removed when read events are disabled [#823](https://github.com/GetStream/stream-chat-swiftui/pull/823)

Sources/StreamChatSwiftUI/Utils.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import StreamChat
88
/// Class providing implementations of several utilities used in the SDK.
99
/// The default implementations can be replaced in the init method, or directly via the variables.
1010
public class Utils {
11-
// TODO: Make it public in future versions.
12-
internal var messagePreviewFormatter = MessagePreviewFormatter()
1311
var markdownFormatter = MarkdownFormatter()
1412

1513
public var dateFormatter: DateFormatter
@@ -29,6 +27,7 @@ public class Utils {
2927
public var channelAvatarsMerger: ChannelAvatarsMerging
3028
public var messageTypeResolver: MessageTypeResolving
3129
public var messageActionsResolver: MessageActionsResolving
30+
public var messagePreviewFormatter: MessagePreviewFormatter
3231
public var commandsConfig: CommandsConfig
3332
public var channelListConfig: ChannelListConfig
3433
public var messageListConfig: MessageListConfig
@@ -87,6 +86,7 @@ public class Utils {
8786
channelAvatarsMerger: ChannelAvatarsMerging = ChannelAvatarsMerger(),
8887
messageTypeResolver: MessageTypeResolving = MessageTypeResolver(),
8988
messageActionResolver: MessageActionsResolving = MessageActionsResolver(),
89+
messagePreviewFormatter: MessagePreviewFormatter = MessagePreviewFormatter(),
9090
commandsConfig: CommandsConfig = DefaultCommandsConfig(),
9191
channelListConfig: ChannelListConfig = ChannelListConfig(),
9292
messageListConfig: MessageListConfig = MessageListConfig(),
@@ -115,6 +115,7 @@ public class Utils {
115115
self.channelAvatarsMerger = channelAvatarsMerger
116116
self.messageTypeResolver = messageTypeResolver
117117
messageActionsResolver = messageActionResolver
118+
self.messagePreviewFormatter = messagePreviewFormatter
118119
self.commandsConfig = commandsConfig
119120
self.channelListConfig = channelListConfig
120121
self.messageListConfig = messageListConfig

Sources/StreamChatSwiftUI/Utils/MessagePreviewFormatter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import SwiftUI
77

88
/// A formatter that converts a message to a text preview representation.
99
/// By default it is used to show message previews in the Channel List and Thread List.
10-
struct MessagePreviewFormatter {
10+
open class MessagePreviewFormatter {
1111
@Injected(\.chatClient) var chatClient
1212

13-
init() {}
13+
public init() {}
1414

1515
/// Formats the message including the author's name.
16-
func format(_ previewMessage: ChatMessage, in channel: ChatChannel) -> String {
16+
open func format(_ previewMessage: ChatMessage, in channel: ChatChannel) -> String {
1717
if let poll = previewMessage.poll {
1818
return formatPoll(poll)
1919
}
2020
return "\(previewMessage.author.name ?? previewMessage.author.id): \(formatContent(for: previewMessage, in: channel))"
2121
}
2222

2323
/// Formats only the content of the message without the author's name.
24-
func formatContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String {
24+
open func formatContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String {
2525
if let attachmentPreviewText = formatAttachmentContent(for: previewMessage, in: channel) {
2626
return attachmentPreviewText
2727
}
@@ -32,7 +32,7 @@ struct MessagePreviewFormatter {
3232
}
3333

3434
/// Formats only the attachment content of the message in case it contains attachments.
35-
func formatAttachmentContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String? {
35+
open func formatAttachmentContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String? {
3636
if let poll = previewMessage.poll {
3737
return "📊 \(poll.name)"
3838
}

0 commit comments

Comments
 (0)