Skip to content

Commit a0b9f51

Browse files
[Feature] Add the makeAttachmentTextView method to ViewFactory (#1023)
1 parent 47d57c1 commit a0b9f51

File tree

9 files changed

+48
-8
lines changed

9 files changed

+48
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6+
### ✅ Added
7+
- Add the `makeAttachmentTextView` method to ViewFactory [#1013](https://github.com/GetStream/stream-chat-swiftui/pull/1013)
8+
69
### 🐞 Fixed
710
- Fix composer not being locked after the channel was frozen [#1015](https://github.com/GetStream/stream-chat-swiftui/pull/1015)
811

Sources/StreamChatSwiftUI/ChatChannel/MessageList/AsyncVoiceMessages/VoiceRecordingContainerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public struct VoiceRecordingContainerView<Factory: ViewFactory>: View {
6767
}
6868
}
6969
if !message.text.isEmpty {
70-
AttachmentTextView(message: message)
70+
AttachmentTextView(factory: factory, message: message)
7171
.frame(maxWidth: .infinity)
7272
}
7373
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public struct ImageAttachmentContainer<Factory: ViewFactory>: View {
4747
}
4848

4949
if !message.text.isEmpty {
50-
AttachmentTextView(message: message)
50+
AttachmentTextView(factory: factory, message: message)
5151
.frame(width: width)
5252
}
5353
}
@@ -93,21 +93,23 @@ public struct ImageAttachmentContainer<Factory: ViewFactory>: View {
9393
}
9494
}
9595

96-
public struct AttachmentTextView: View {
96+
public struct AttachmentTextView<Factory: ViewFactory>: View {
9797
@Injected(\.colors) private var colors
9898
@Injected(\.fonts) private var fonts
9999

100+
var factory: Factory
100101
var message: ChatMessage
101102
let injectedBackgroundColor: UIColor?
102103

103-
public init(message: ChatMessage, injectedBackgroundColor: UIColor? = nil) {
104+
public init(factory: Factory = DefaultViewFactory.shared, message: ChatMessage, injectedBackgroundColor: UIColor? = nil) {
105+
self.factory = factory
104106
self.message = message
105107
self.injectedBackgroundColor = injectedBackgroundColor
106108
}
107109

108110
public var body: some View {
109111
HStack {
110-
StreamTextView(message: message)
112+
factory.makeAttachmentTextView(options: .init(mesage: message))
111113
.standardPadding()
112114
.fixedSize(horizontal: false, vertical: true)
113115
Spacer()

Sources/StreamChatSwiftUI/ChatChannel/MessageList/LinkAttachmentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public struct LinkAttachmentContainer<Factory: ViewFactory>: View {
5151

5252
if #available(iOS 15, *) {
5353
HStack {
54-
StreamTextView(message: message)
54+
factory.makeAttachmentTextView(options: .init(mesage: message))
5555
.standardPadding()
5656
Spacer()
5757
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageView.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public struct MessageTextView<Factory: ViewFactory>: View {
167167
)
168168
}
169169

170-
StreamTextView(message: message)
170+
factory.makeAttachmentTextView(options: .init(mesage: message))
171171
.padding(.leading, leadingPadding)
172172
.padding(.trailing, trailingPadding)
173173
.padding(.top, topPadding)
@@ -247,6 +247,16 @@ struct StreamTextView: View {
247247
}
248248
}
249249

250+
// Options for the attachment text view.
251+
public class AttachmentTextViewOptions {
252+
// The message to display the text for.
253+
public let message: ChatMessage
254+
255+
public init(mesage: ChatMessage) {
256+
self.message = mesage
257+
}
258+
}
259+
250260
@available(iOS 15, *)
251261
public struct LinkDetectionTextView: View {
252262
@Environment(\.layoutDirection) var layoutDirection

Sources/StreamChatSwiftUI/ChatChannel/MessageList/VideoAttachmentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public struct VideoAttachmentsContainer<Factory: ViewFactory>: View {
4646
}
4747

4848
if !message.text.isEmpty {
49-
AttachmentTextView(message: message)
49+
AttachmentTextView(factory: factory, message: message)
5050
.frame(width: width)
5151
}
5252
}

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,12 @@ extension ViewFactory {
11511151
) -> some View {
11521152
AddUsersView(loadedUserIds: options.loadedUsers.map(\.id), onUserTap: onUserTap)
11531153
}
1154+
1155+
public func makeAttachmentTextView(
1156+
options: AttachmentTextViewOptions
1157+
) -> some View {
1158+
StreamTextView(message: options.message)
1159+
}
11541160
}
11551161

11561162
/// Default class conforming to `ViewFactory`, used throughout the SDK.

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,4 +1182,12 @@ public protocol ViewFactory: AnyObject {
11821182
options: AddUsersOptions,
11831183
onUserTap: @escaping (ChatUser) -> Void
11841184
) -> AddUsersViewType
1185+
1186+
associatedtype AttachmentTextViewType: View
1187+
/// Creates a view for displaying the text of an attachment.
1188+
/// - Parameter options: Configuration options for the attachment text view, such as message.
1189+
/// - Returns: The view shown in the attachment text slot.
1190+
func makeAttachmentTextView(
1191+
options: AttachmentTextViewOptions
1192+
) -> AttachmentTextViewType
11851193
}

StreamChatSwiftUITests/Tests/Utils/ViewFactory_Tests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,17 @@ class ViewFactory_Tests: StreamChatTestCase {
10431043
// Then
10441044
XCTAssert(view is AddUsersView<DefaultViewFactory>)
10451045
}
1046+
1047+
func test_viewFactory_makeAttachmentTextView() {
1048+
// Given
1049+
let viewFactory = DefaultViewFactory.shared
1050+
1051+
// When
1052+
let view = viewFactory.makeAttachmentTextView(options: .init(mesage: message))
1053+
1054+
// Then
1055+
XCTAssert(view is StreamTextView)
1056+
}
10461057
}
10471058

10481059
extension ChannelAction: Equatable {

0 commit comments

Comments
 (0)