Skip to content

Commit 504bf45

Browse files
Implemented channel config for message length
1 parent c925b55 commit 504bf45

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/ComposerTextInputView.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct ComposerTextInputView: UIViewRepresentable {
1212
@Binding var selectedRangeLocation: Int
1313

1414
var placeholder: String
15+
var maxMessageLength: Int?
1516

1617
func makeUIView(context: Context) -> InputTextView {
1718
let inputTextView = InputTextView()
@@ -34,16 +35,21 @@ struct ComposerTextInputView: UIViewRepresentable {
3435
}
3536

3637
func makeCoordinator() -> Coordinator {
37-
Coordinator(textInput: self)
38+
Coordinator(textInput: self, maxMessageLength: maxMessageLength)
3839
}
3940

4041
class Coordinator: NSObject, UITextViewDelegate, NSLayoutManagerDelegate {
4142
weak var textView: InputTextView?
4243

4344
var textInput: ComposerTextInputView
45+
var maxMessageLength: Int?
4446

45-
init(textInput: ComposerTextInputView) {
47+
init(
48+
textInput: ComposerTextInputView,
49+
maxMessageLength: Int?
50+
) {
4651
self.textInput = textInput
52+
self.maxMessageLength = maxMessageLength
4753
}
4854

4955
func textViewDidChange(_ textView: UITextView) {
@@ -56,7 +62,8 @@ struct ComposerTextInputView: UIViewRepresentable {
5662
shouldChangeTextIn range: NSRange,
5763
replacementText text: String
5864
) -> Bool {
59-
true
65+
guard let maxMessageLength = maxMessageLength else { return true }
66+
return textView.text.count + (text.count - range.length) <= maxMessageLength
6067
}
6168

6269
func layoutManager(

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
7070
addedFileURLs: viewModel.addedFileURLs,
7171
addedCustomAttachments: viewModel.addedCustomAttachments,
7272
quotedMessage: $quotedMessage,
73+
maxMessageLength: channelConfig?.maxMessageLength,
7374
onCustomAttachmentTap: viewModel.customAttachmentTapped(_:),
7475
shouldScroll: viewModel.inputComposerShouldScroll,
7576
removeAttachmentWithId: viewModel.removeAttachment(with:)
@@ -179,6 +180,7 @@ public struct ComposerInputView<Factory: ViewFactory>: View {
179180
var addedFileURLs: [URL]
180181
var addedCustomAttachments: [CustomAttachment]
181182
var quotedMessage: Binding<ChatMessage?>
183+
var maxMessageLength: Int?
182184
var onCustomAttachmentTap: (CustomAttachment) -> Void
183185
var removeAttachmentWithId: (String) -> Void
184186

@@ -255,7 +257,8 @@ public struct ComposerInputView<Factory: ViewFactory>: View {
255257
text: $text,
256258
height: $textHeight,
257259
selectedRangeLocation: $selectedRangeLocation,
258-
placeholder: L10n.Composer.Placeholder.message
260+
placeholder: L10n.Composer.Placeholder.message,
261+
maxMessageLength: maxMessageLength
259262
)
260263
.frame(height: textFieldHeight)
261264
.overlay(

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageContainerView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ struct MessageContainerView<Factory: ViewFactory>: View {
146146
)
147147
MessageDateView(message: message)
148148
}
149-
} else if isInGroup {
149+
} else if !message.isSentByCurrentUser && isInGroup {
150150
MessageAuthorAndDateView(message: message)
151151
} else {
152152
MessageDateView(message: message)

Sources/StreamChatSwiftUI/DefaultViewFactory.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ extension ViewFactory {
311311
addedFileURLs: [URL],
312312
addedCustomAttachments: [CustomAttachment],
313313
quotedMessage: Binding<ChatMessage?>,
314+
maxMessageLength: Int?,
314315
onCustomAttachmentTap: @escaping (CustomAttachment) -> Void,
315316
shouldScroll: Bool,
316317
removeAttachmentWithId: @escaping (String) -> Void
@@ -326,6 +327,7 @@ extension ViewFactory {
326327
addedFileURLs: addedFileURLs,
327328
addedCustomAttachments: addedCustomAttachments,
328329
quotedMessage: quotedMessage,
330+
maxMessageLength: maxMessageLength,
329331
onCustomAttachmentTap: onCustomAttachmentTap,
330332
removeAttachmentWithId: removeAttachmentWithId
331333
)
@@ -341,6 +343,7 @@ extension ViewFactory {
341343
addedFileURLs: addedFileURLs,
342344
addedCustomAttachments: addedCustomAttachments,
343345
quotedMessage: quotedMessage,
346+
maxMessageLength: maxMessageLength,
344347
onCustomAttachmentTap: onCustomAttachmentTap,
345348
removeAttachmentWithId: removeAttachmentWithId
346349
)

Sources/StreamChatSwiftUI/ViewFactory.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public protocol ViewFactory: AnyObject {
305305
/// - addedFileURLs: list of the added file URLs (in case they need to be displayed in the input view).
306306
/// - addedCustomAttachments: list of added custom attachments.
307307
/// - quotedMessage: Optional quoted message, shown in the composer input.
308+
/// - maxMessageLength: the maximum allowed message length.
308309
/// - onCustomAttachmentTap: called when a custom attachment is tapped.
309310
/// - shouldScroll: whether the input field is scrollable.
310311
/// - removeAttachmentWithId: called when the attachment is removed from the input view.
@@ -318,6 +319,7 @@ public protocol ViewFactory: AnyObject {
318319
addedFileURLs: [URL],
319320
addedCustomAttachments: [CustomAttachment],
320321
quotedMessage: Binding<ChatMessage?>,
322+
maxMessageLength: Int?,
321323
onCustomAttachmentTap: @escaping (CustomAttachment) -> Void,
322324
shouldScroll: Bool,
323325
removeAttachmentWithId: @escaping (String) -> Void

0 commit comments

Comments
 (0)