Skip to content

Commit 5b21d5f

Browse files
committed
Allow customisation of message previews in lists
1 parent 3bd6162 commit 5b21d5f

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-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: MessagePreviewFormatting
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: MessagePreviewFormatting = 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: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,33 @@
55
import StreamChat
66
import SwiftUI
77

8+
/// Provides message preview representation for lists.
9+
public protocol MessagePreviewFormatting {
10+
/// Formats the message including the author's name.
11+
func format(_ previewMessage: ChatMessage, in channel: ChatChannel) -> String
12+
/// Formats only the content of the message without the author's name.
13+
func formatContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String
14+
/// Formats only the attachment content of the message in case it contains attachments.
15+
func formatAttachmentContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String?
16+
}
17+
818
/// A formatter that converts a message to a text preview representation.
919
/// By default it is used to show message previews in the Channel List and Thread List.
10-
struct MessagePreviewFormatter {
20+
open class MessagePreviewFormatter: MessagePreviewFormatting {
1121
@Injected(\.chatClient) var chatClient
1222

13-
init() {}
23+
public init() {}
1424

1525
/// Formats the message including the author's name.
16-
func format(_ previewMessage: ChatMessage, in channel: ChatChannel) -> String {
26+
open func format(_ previewMessage: ChatMessage, in channel: ChatChannel) -> String {
1727
if let poll = previewMessage.poll {
1828
return formatPoll(poll)
1929
}
2030
return "\(previewMessage.author.name ?? previewMessage.author.id): \(formatContent(for: previewMessage, in: channel))"
2131
}
2232

2333
/// Formats only the content of the message without the author's name.
24-
func formatContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String {
34+
open func formatContent(for previewMessage: ChatMessage, in channel: ChatChannel) -> String {
2535
if let attachmentPreviewText = formatAttachmentContent(for: previewMessage, in: channel) {
2636
return attachmentPreviewText
2737
}
@@ -32,7 +42,7 @@ struct MessagePreviewFormatter {
3242
}
3343

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

0 commit comments

Comments
 (0)