Skip to content

Commit ca946e7

Browse files
Show error indicator when attachment max size exceeded
1 parent 9a09e67 commit ca946e7

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerView.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
8181
shouldScroll: viewModel.inputComposerShouldScroll,
8282
removeAttachmentWithId: viewModel.removeAttachment(with:)
8383
)
84+
.alert(isPresented: $viewModel.attachmentSizeExceeded) {
85+
Alert(
86+
title: Text(L10n.Attachment.MaxSize.title),
87+
message: Text(L10n.Attachment.MaxSize.message),
88+
dismissButton: .cancel(Text(L10n.Alert.Actions.ok))
89+
)
90+
}
8491

8592
factory.makeTrailingComposerView(
8693
enabled: viewModel.sendButtonEnabled,
@@ -95,6 +102,9 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
95102
onMessageSent()
96103
}
97104
}
105+
.alert(isPresented: $viewModel.errorShown) {
106+
Alert.defaultErrorAlert
107+
}
98108
}
99109
.padding(.all, 8)
100110

@@ -169,9 +179,6 @@ public struct MessageComposerView<Factory: ViewFactory>: View, KeyboardReadable
169179
alignment: .bottom
170180
)
171181
.modifier(factory.makeComposerViewModifier())
172-
.alert(isPresented: $viewModel.errorShown) {
173-
Alert.defaultErrorAlert
174-
}
175182
.onChange(of: editedMessage) { _ in
176183
viewModel.text = editedMessage?.text ?? ""
177184
if editedMessage != nil {

Sources/StreamChatSwiftUI/ChatChannel/Composer/MessageComposerViewModel.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ open class MessageComposerViewModel: ObservableObject {
123123
@Published public var showReplyInChannel = false
124124
@Published public var suggestions = [String: Any]()
125125
@Published public var cooldownDuration: Int = 0
126+
@Published public var attachmentSizeExceeded: Bool = false
126127

127128
public let channelController: ChatChannelController
128129
public var messageController: ChatMessageController?
@@ -575,7 +576,9 @@ open class MessageComposerViewModel: ObservableObject {
575576

576577
do {
577578
let fileSize = try AttachmentFile(url: url).size
578-
return fileSize < chatClient.config.maxAttachmentSize
579+
let canAdd = fileSize < chatClient.config.maxAttachmentSize
580+
attachmentSizeExceeded = !canAdd
581+
return canAdd
579582
} catch {
580583
return false
581584
}

Sources/StreamChatSwiftUI/Generated/L10n.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ internal enum L10n {
4747
internal enum Attachment {
4848
/// Attachment size exceed the limit.
4949
internal static var maxSizeExceeded: String { L10n.tr("Localizable", "attachment.max-size-exceeded") }
50+
internal enum MaxSize {
51+
/// Please select a smaller attachment.
52+
internal static var message: String { L10n.tr("Localizable", "attachment.max-size.message") }
53+
/// Attachment size exceed the limit
54+
internal static var title: String { L10n.tr("Localizable", "attachment.max-size.title") }
55+
}
5056
}
5157

5258
internal enum Channel {

Sources/StreamChatSwiftUI/Resources/en.lproj/Localizable.strings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
"composer.suggestions.commands.header" = "Instant Commands";
9494
"message.sending.attachment-uploading-failed" = "UPLOADING FAILED";
9595

96+
"attachment.max-size.title" = "Attachment size exceed the limit";
97+
"attachment.max-size.message" = "Please select a smaller attachment.";
98+
9699
"message.title.online" = "Online";
97100
"message.title.offline" = "Offline";
98101
"message.title.group" = "%d members, %d online";

StreamChatSwiftUITests/Tests/ChatChannel/MessageComposerViewModel_Tests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,11 @@ class MessageComposerViewModel_Tests: StreamChatTestCase {
485485
// When
486486
let newAsset = defaultAsset
487487
viewModel.imageTapped(newAsset) // will not be added because of small max attachment size.
488+
let alertShown = viewModel.attachmentSizeExceeded
488489

489490
// Then
490491
XCTAssert(viewModel.addedAssets.isEmpty)
492+
XCTAssert(alertShown == true)
491493
}
492494

493495
func test_messageComposerVM_mentionUsers() {

0 commit comments

Comments
 (0)