Skip to content

Add view factory method for customising MessageView #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Add `Utils.originalTranslationsStore` to keep track of messages that should show the original text [#815](https://github.com/GetStream/stream-chat-swiftui/pull/815)
- Add `ViewFactory.makeGalleryHeaderView` for customising header view in `GalleryView` [#837](https://github.com/GetStream/stream-chat-swiftui/pull/837)
- Add `ViewFactory.makeVideoPlayerHeaderView` for customising header view in `VideoPlayerView` [#837](https://github.com/GetStream/stream-chat-swiftui/pull/837)
- Add `ViewFactory.makeMessageView` for customising `MessageView` [#841](https://github.com/GetStream/stream-chat-swiftui/pull/841)
### 🐞 Fixed
- Fix swipe to reply enabled when quoting a message is disabled [#824](https://github.com/GetStream/stream-chat-swiftui/pull/824)
- Fix mark unread action not removed when read events are disabled [#823](https://github.com/GetStream/stream-chat-swiftui/pull/823)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ public struct MessageContainerView<Factory: ViewFactory>: View {
)
}

MessageView(
factory: factory,
message: message,
factory.makeMessageView(
for: message,
contentWidth: contentWidth,
isFirst: showsAllInfo,
scrolledId: $scrolledId
Expand Down
15 changes: 15 additions & 0 deletions Sources/StreamChatSwiftUI/DefaultViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,21 @@ extension ViewFactory {
)
}

public func makeMessageView(
for message: ChatMessage,
contentWidth: CGFloat,
isFirst: Bool,
scrolledId: Binding<String?>
) -> some View {
MessageView(
factory: self,
message: message,
contentWidth: contentWidth,
isFirst: isFirst,
scrolledId: scrolledId
)
}

public func makeMessageTextView(
for message: ChatMessage,
isFirst: Bool,
Expand Down
15 changes: 15 additions & 0 deletions Sources/StreamChatSwiftUI/ViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,21 @@ public protocol ViewFactory: AnyObject {
onLongPress: @escaping (MessageDisplayInfo) -> Void,
isLast: Bool
) -> MessageContainerViewType

associatedtype MessageViewType: View
/// Creates the message view.
/// - Parameters:
/// - message: the chat message.
/// - contentWidth: the available width for the message.
/// - isFirst: whether it is first in the group (latest creation date).
/// - scrolledId: binding of the currently scrolled id. Use it to force scrolling to the particular message.
/// - Returns: view shown in the message slot.
func makeMessageView(
for message: ChatMessage,
contentWidth: CGFloat,
isFirst: Bool,
scrolledId: Binding<String?>
) -> MessageViewType

associatedtype MessageTextViewType: View
/// Creates the message text view.
Expand Down
16 changes: 16 additions & 0 deletions StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ class ViewFactory_Tests: StreamChatTestCase {
// Then
XCTAssert(view is DefaultChannelHeaderModifier<DefaultViewFactory>)
}

func test_viewFactory_makeMessageView() {
// Given
let viewFactory = DefaultViewFactory.shared

// When
let view = viewFactory.makeMessageView(
for: message,
contentWidth: 300,
isFirst: true,
scrolledId: .constant(nil)
)

// Then
XCTAssert(view is MessageView<DefaultViewFactory>)
}

func test_viewFactory_makeMessageTextView() {
// Given
Expand Down
Loading