Skip to content

Commit 1efb9f8

Browse files
Added method for customizing quoted message avatar
1 parent a28c821 commit 1efb9f8

File tree

12 files changed

+82
-21
lines changed

12 files changed

+82
-21
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/FileAttachmentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import StreamChat
66
import SwiftUI
77

8-
public struct FileAttachmentsContainer: View {
8+
public struct FileAttachmentsContainer<Factory: ViewFactory>: View {
99

1010
@Injected(\.utils) private var utils
1111

12+
var factory: Factory
1213
var message: ChatMessage
1314
var width: CGFloat
1415
var isFirst: Bool
@@ -18,6 +19,7 @@ public struct FileAttachmentsContainer: View {
1819
VStack(alignment: message.alignmentInBubble) {
1920
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
2021
QuotedMessageViewContainer(
22+
factory: factory,
2123
quotedMessage: quotedMessage,
2224
fillAvailableSpace: !message.attachmentCounts.isEmpty,
2325
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/ChatChannel/MessageList/GiphyAttachmentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct GiphyAttachmentView<Factory: ViewFactory>: View {
2828
) {
2929
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
3030
QuotedMessageViewContainer(
31+
factory: factory,
3132
quotedMessage: quotedMessage,
3233
fillAvailableSpace: !message.attachmentCounts.isEmpty,
3334
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import NukeUI
77
import StreamChat
88
import SwiftUI
99

10-
public struct ImageAttachmentContainer: View {
10+
public struct ImageAttachmentContainer<Factory: ViewFactory>: View {
1111
@Injected(\.colors) private var colors
1212
@Injected(\.utils) private var utils
1313

14+
var factory: Factory
1415
let message: ChatMessage
1516
let width: CGFloat
1617
let isFirst: Bool
@@ -26,6 +27,7 @@ public struct ImageAttachmentContainer: View {
2627
) {
2728
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
2829
QuotedMessageViewContainer(
30+
factory: factory,
2931
quotedMessage: quotedMessage,
3032
fillAvailableSpace: !message.attachmentCounts.isEmpty,
3133
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/ChatChannel/MessageList/LinkAttachmentView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import SwiftUI
99

1010
/// Container for presenting link attachments.
1111
/// In case of more than one link, only the first link is previewed.
12-
public struct LinkAttachmentContainer: View {
12+
public struct LinkAttachmentContainer<Factory: ViewFactory>: View {
1313
@Injected(\.colors) private var colors
1414
@Injected(\.utils) private var utils
1515

16+
var factory: Factory
1617
var message: ChatMessage
1718
var width: CGFloat
1819
var isFirst: Bool
@@ -27,6 +28,7 @@ public struct LinkAttachmentContainer: View {
2728
) {
2829
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
2930
QuotedMessageViewContainer(
31+
factory: factory,
3032
quotedMessage: quotedMessage,
3133
fillAvailableSpace: !message.attachmentCounts.isEmpty,
3234
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageView.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ struct MessageView<Factory: ViewFactory>: View {
9494
}
9595
}
9696

97-
public struct MessageTextView: View {
97+
public struct MessageTextView<Factory: ViewFactory>: View {
9898
@Injected(\.colors) private var colors
9999
@Injected(\.fonts) private var fonts
100100
@Injected(\.utils) private var utils
101101

102+
var factory: Factory
102103
var message: ChatMessage
103104
var isFirst: Bool
104105
@Binding var scrolledId: String?
@@ -110,6 +111,7 @@ public struct MessageTextView: View {
110111
) {
111112
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
112113
QuotedMessageViewContainer(
114+
factory: factory,
113115
quotedMessage: quotedMessage,
114116
fillAvailableSpace: !message.attachmentCounts.isEmpty,
115117
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/ChatChannel/MessageList/QuotedMessageView.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import StreamChat
88
import SwiftUI
99

1010
/// Container showing the quoted message view with the user avatar.
11-
struct QuotedMessageViewContainer: View {
11+
struct QuotedMessageViewContainer<Factory: ViewFactory>: View {
1212

1313
@Injected(\.utils) private var utils
1414

1515
private let avatarSize: CGFloat = 24
1616

17+
var factory: Factory
1718
var quotedMessage: ChatMessage
1819
var fillAvailableSpace: Bool
1920
var forceLeftToRight = false
@@ -22,9 +23,9 @@ struct QuotedMessageViewContainer: View {
2223
var body: some View {
2324
HStack(alignment: .bottom) {
2425
if !quotedMessage.isSentByCurrentUser || forceLeftToRight {
25-
MessageAvatarView(
26-
avatarURL: utils.messageCachingUtils.authorImageURL(for: quotedMessage),
27-
size: .init(width: avatarSize, height: avatarSize)
26+
factory.makeQuotedMessageAvatarView(
27+
for: utils.messageCachingUtils.authorInfo(from: quotedMessage),
28+
size: CGSize(width: avatarSize, height: avatarSize)
2829
)
2930

3031
QuotedMessageView(
@@ -39,9 +40,9 @@ struct QuotedMessageViewContainer: View {
3940
forceLeftToRight: forceLeftToRight
4041
)
4142

42-
MessageAvatarView(
43-
avatarURL: utils.messageCachingUtils.authorImageURL(for: quotedMessage),
44-
size: .init(width: avatarSize, height: avatarSize)
43+
factory.makeQuotedMessageAvatarView(
44+
for: utils.messageCachingUtils.authorInfo(from: quotedMessage),
45+
size: CGSize(width: avatarSize, height: avatarSize)
4546
)
4647
}
4748
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/VideoAttachmentView.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import AVKit
66
import StreamChat
77
import SwiftUI
88

9-
public struct VideoAttachmentsContainer: View {
9+
public struct VideoAttachmentsContainer<Factory: ViewFactory>: View {
1010

1111
@Injected(\.utils) private var utils
12-
12+
13+
var factory: Factory
1314
let message: ChatMessage
1415
let width: CGFloat
1516
@Binding var scrolledId: String?
@@ -19,6 +20,7 @@ public struct VideoAttachmentsContainer: View {
1920
if let quotedMessage = utils.messageCachingUtils.quotedMessage(for: message) {
2021
VStack {
2122
QuotedMessageViewContainer(
23+
factory: factory,
2224
quotedMessage: quotedMessage,
2325
fillAvailableSpace: !message.attachmentCounts.isEmpty,
2426
scrolledId: $scrolledId

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ extension ViewFactory {
200200
MessageAvatarView(avatarURL: userDisplayInfo.imageURL)
201201
}
202202

203+
public func makeQuotedMessageAvatarView(
204+
for userDisplayInfo: UserDisplayInfo,
205+
size: CGSize
206+
) -> some View {
207+
MessageAvatarView(avatarURL: userDisplayInfo.imageURL, size: size)
208+
}
209+
203210
public func makeChannelHeaderViewModifier(
204211
for channel: ChatChannel
205212
) -> some ChatChannelHeaderViewModifier {
@@ -249,6 +256,7 @@ extension ViewFactory {
249256
scrolledId: Binding<String?>
250257
) -> some View {
251258
MessageTextView(
259+
factory: self,
252260
message: message,
253261
isFirst: isFirst,
254262
scrolledId: scrolledId
@@ -262,6 +270,7 @@ extension ViewFactory {
262270
scrolledId: Binding<String?>
263271
) -> some View {
264272
ImageAttachmentContainer(
273+
factory: self,
265274
message: message,
266275
width: availableWidth,
267276
isFirst: isFirst,
@@ -291,6 +300,7 @@ extension ViewFactory {
291300
scrolledId: Binding<String?>
292301
) -> some View {
293302
LinkAttachmentContainer(
303+
factory: self,
294304
message: message,
295305
width: availableWidth,
296306
isFirst: isFirst,
@@ -305,6 +315,7 @@ extension ViewFactory {
305315
scrolledId: Binding<String?>
306316
) -> some View {
307317
FileAttachmentsContainer(
318+
factory: self,
308319
message: message,
309320
width: availableWidth,
310321
isFirst: isFirst,
@@ -319,6 +330,7 @@ extension ViewFactory {
319330
scrolledId: Binding<String?>
320331
) -> some View {
321332
VideoAttachmentsContainer(
333+
factory: self,
322334
message: message,
323335
width: availableWidth,
324336
scrolledId: scrolledId
@@ -664,6 +676,7 @@ extension ViewFactory {
664676
quotedMessage: ChatMessage
665677
) -> some View {
666678
QuotedMessageViewContainer(
679+
factory: self,
667680
quotedMessage: quotedMessage,
668681
fillAvailableSpace: true,
669682
forceLeftToRight: true,

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ public protocol ViewFactory: AnyObject {
183183
/// - Parameter userDisplayInfo: the author's display info.
184184
func makeMessageAvatarView(for userDisplayInfo: UserDisplayInfo) -> UserAvatar
185185

186+
associatedtype QuotedUserAvatar: View
187+
/// Creates the user avatar shown in quoted messages.
188+
/// - Parameters:
189+
/// - userDisplayInfo: the author's display info.
190+
/// - size: the required size of the view.
191+
func makeQuotedMessageAvatarView(
192+
for userDisplayInfo: UserDisplayInfo,
193+
size: CGSize
194+
) -> QuotedUserAvatar
195+
186196
associatedtype ChatHeaderViewModifier: ChatChannelHeaderViewModifier
187197
/// Creates the channel header view modifier.
188198
/// - Parameter channel: the displayed channel.

StreamChatSwiftUITests/Tests/ChatChannel/QuotedMessageView_Tests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class QuotedMessageView_Tests: StreamChatTestCase {
2020
func test_quotedMessageViewContainer_snapshot() {
2121
// Given
2222
let view = QuotedMessageViewContainer(
23+
factory: DefaultViewFactory.shared,
2324
quotedMessage: testMessage,
2425
fillAvailableSpace: true,
2526
scrolledId: .constant(nil)

0 commit comments

Comments
 (0)