Skip to content

Commit 2f449d6

Browse files
Updated message avatar creation method with additional user data
1 parent dfebc42 commit 2f449d6

File tree

7 files changed

+30
-15
lines changed

7 files changed

+30
-15
lines changed

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct MessageContainerView<Factory: ViewFactory>: View {
4545
if messageListConfig.messageDisplayOptions.showAvatars {
4646
if showsAllInfo {
4747
factory.makeMessageAvatarView(
48-
for: utils.messageCachingUtils.authorImageURL(for: message)
48+
for: utils.messageCachingUtils.authorInfo(from: message)
4949
)
5050
} else {
5151
Color.clear

Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsOverlayView.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import StreamChat
66
import SwiftUI
77

88
public struct ReactionsOverlayView<Factory: ViewFactory>: View {
9+
@Injected(\.utils) private var utils
10+
911
@StateObject var viewModel: ReactionsOverlayViewModel
1012

1113
var factory: Factory
@@ -64,11 +66,13 @@ public struct ReactionsOverlayView<Factory: ViewFactory>: View {
6466
}
6567

6668
if !messageDisplayInfo.message.isSentByCurrentUser {
67-
factory.makeMessageAvatarView(for: messageDisplayInfo.message.author.imageURL)
68-
.offset(
69-
x: paddingValue / 2,
70-
y: originY + messageDisplayInfo.frame.height - paddingValue + 2
71-
)
69+
factory.makeMessageAvatarView(
70+
for: utils.messageCachingUtils.authorInfo(from: messageDisplayInfo.message)
71+
)
72+
.offset(
73+
x: paddingValue / 2,
74+
y: originY + messageDisplayInfo.frame.height - paddingValue + 2
75+
)
7276
}
7377

7478
GeometryReader { reader in

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ extension ViewFactory {
188188
}
189189
}
190190

191-
public func makeMessageAvatarView(for avatarURL: URL?) -> some View {
192-
MessageAvatarView(avatarURL: avatarURL)
191+
public func makeMessageAvatarView(for userDisplayInfo: UserDisplayInfo) -> some View {
192+
MessageAvatarView(avatarURL: userDisplayInfo.imageURL)
193193
}
194194

195195
public func makeChannelHeaderViewModifier(

Sources/StreamChatSwiftUI/Utils/MessageCachingUtils.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ class MessageCachingUtils {
4242
return userDisplayInfo.imageURL
4343
}
4444

45+
func authorInfo(from message: ChatMessage) -> UserDisplayInfo {
46+
if let userDisplayInfo = userDisplayInfo(for: message) {
47+
return userDisplayInfo
48+
}
49+
50+
let userDisplayInfo = saveUserDisplayInfo(for: message)
51+
return userDisplayInfo
52+
}
53+
4554
func quotedMessage(for message: ChatMessage) -> ChatMessage? {
4655
if checkedMessageIds.contains(message.id) {
4756
return nil
@@ -101,7 +110,8 @@ class MessageCachingUtils {
101110
}
102111
}
103112

104-
struct UserDisplayInfo {
113+
/// Contains display information for the user.
114+
public struct UserDisplayInfo {
105115
let id: String
106116
let name: String
107117
let imageURL: URL?

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public protocol ViewFactory: AnyObject {
170170

171171
associatedtype UserAvatar: View
172172
/// Creates the message avatar view.
173-
/// - Parameter avatarURL: the author's avatar URL..
174-
func makeMessageAvatarView(for avatarURL: URL?) -> UserAvatar
173+
/// - Parameter userDisplayInfo: the author's display info.
174+
func makeMessageAvatarView(for userDisplayInfo: UserDisplayInfo) -> UserAvatar
175175

176176
associatedtype ChatHeaderViewModifier: ChatChannelHeaderViewModifier
177177
/// Creates the channel header view modifier.

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ class ViewFactory_Tests: StreamChatTestCase {
103103
func test_viewFactory_makeMessageAvatarView() {
104104
// Given
105105
let viewFactory = DefaultViewFactory.shared
106+
let userInfo = UserDisplayInfo(id: .unique, name: .unique, imageURL: URL(string: "https://example.com"))
106107

107108
// When
108-
let view = viewFactory.makeMessageAvatarView(for: URL(string: "https://example.com"))
109+
let view = viewFactory.makeMessageAvatarView(for: userInfo)
109110

110111
// Then
111112
XCTAssert(view is MessageAvatarView)

docusaurus/docs/iOS/swiftui/components/custom-avatar.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ class CustomFactory: ViewFactory {
4242

4343
init() {}
4444

45-
func makeMessageAvatarView(for avatarURL: URL?) -> some View {
46-
CustomUserAvatar(avatarURL: avatarURL)
45+
func makeMessageAvatarView(for userDisplayInfo: UserDisplayInfo) -> some View {
46+
CustomUserAvatar(avatarURL: userDisplayInfo.avatarURL)
4747
}
4848

4949
}
5050
```
5151

52-
With this, you can have a custom avatar view in the message list view.
52+
With this, you can have a custom avatar view in the message list view. You can also use the name and the user id from the `UserDisplayInfo`, in case you want to present additional information in the avatar view.

0 commit comments

Comments
 (0)