From 0b33c258f599e2b4c49192022edc277c4d092c5d Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 11 Aug 2021 18:14:43 +0530 Subject: [PATCH] feat(ui, localization): Move attachment limit exceeded error to translations Signed-off-by: xsahil03x --- packages/stream_chat_flutter/CHANGELOG.md | 3 +++ .../lib/src/localization/translations.dart | 6 ++++++ .../stream_chat_flutter/lib/src/message_input.dart | 12 ++++++++---- .../example/lib/add_new_lang.dart | 4 ++++ .../lib/src/stream_chat_localizations_en.dart | 4 ++++ .../lib/src/stream_chat_localizations_es.dart | 6 ++++++ .../lib/src/stream_chat_localizations_fr.dart | 6 ++++++ .../lib/src/stream_chat_localizations_hi.dart | 6 ++++++ .../lib/src/stream_chat_localizations_it.dart | 6 ++++++ .../test/translations_test.dart | 1 + 10 files changed, 50 insertions(+), 4 deletions(-) diff --git a/packages/stream_chat_flutter/CHANGELOG.md b/packages/stream_chat_flutter/CHANGELOG.md index 5289ec30..464e40ea 100644 --- a/packages/stream_chat_flutter/CHANGELOG.md +++ b/packages/stream_chat_flutter/CHANGELOG.md @@ -10,6 +10,9 @@ - `GalleryHeader` - `GalleryFooter` - `ThreadHeader` +- Added `MessageInput.attachmentLimit` in order to limit the no. of attachments that can be sent with a single message. +- Added `MessageInput.onAttachmentLimitExceed` callback which will be called when the `attachmentLimit` is exceeded. + This will override the default error alert behaviour. 🔄 Changed diff --git a/packages/stream_chat_flutter/lib/src/localization/translations.dart b/packages/stream_chat_flutter/lib/src/localization/translations.dart index d6e5add0..e5dd5df3 100644 --- a/packages/stream_chat_flutter/lib/src/localization/translations.dart +++ b/packages/stream_chat_flutter/lib/src/localization/translations.dart @@ -304,6 +304,8 @@ abstract class Translations { /// The label for "Reply to message" String get replyToMessageLabel; + + String attachmentLimitExceedError(int limit); } /// Default implementation of Translation strings for the stream chat widgets @@ -664,4 +666,8 @@ class DefaultTranslations implements Translations { @override String get replyToMessageLabel => 'Reply to Message'; + + @override + String attachmentLimitExceedError(int limit) => + 'Attachment limit exceeded, limit: $limit'; } diff --git a/packages/stream_chat_flutter/lib/src/message_input.dart b/packages/stream_chat_flutter/lib/src/message_input.dart index 4f61258c..bce164f0 100644 --- a/packages/stream_chat_flutter/lib/src/message_input.dart +++ b/packages/stream_chat_flutter/lib/src/message_input.dart @@ -263,9 +263,12 @@ class MessageInput extends StatefulWidget { /// A callback for error reporting final ErrorListener? onError; + /// A limit for the no. of attachments that can be sent with a single message. final int attachmentLimit; - /// A callback for error reporting + /// A callback for when the [attachmentLimit] is exceeded. + /// + /// This will override the default error alert behaviour. final AttachmentLimitExceedListener? onAttachmentLimitExceed; @override @@ -1856,17 +1859,18 @@ class MessageInputState extends State { /// Adds an attachment to the [_attachments] map void _addAttachments(Iterable attachments) { + final limit = widget.attachmentLimit; final length = _attachments.length + attachments.length; - if (length > widget.attachmentLimit) { + if (length > limit) { final onAttachmentLimitExceed = widget.onAttachmentLimitExceed; if (onAttachmentLimitExceed != null) { return onAttachmentLimitExceed( widget.attachmentLimit, - 'Attachment Limit crossed ${widget.attachmentLimit}', + context.translations.attachmentLimitExceedError(limit), ); } return _showErrorAlert( - 'Attachment Limit crossed ${widget.attachmentLimit}', + context.translations.attachmentLimitExceedError(limit), ); } for (final attachment in attachments) { diff --git a/packages/stream_chat_localizations/example/lib/add_new_lang.dart b/packages/stream_chat_localizations/example/lib/add_new_lang.dart index 8a1d196e..8d2109f6 100644 --- a/packages/stream_chat_localizations/example/lib/add_new_lang.dart +++ b/packages/stream_chat_localizations/example/lib/add_new_lang.dart @@ -381,6 +381,10 @@ class NnStreamChatLocalizations extends GlobalStreamChatLocalizations { @override String get replyToMessageLabel => 'Reply to Message'; + + @override + String attachmentLimitExceedError(int limit) => + 'Attachment limit exceeded, limit: $limit'; } void main() async { diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart index 5041d398..8c905b41 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_en.dart @@ -357,4 +357,8 @@ class StreamChatLocalizationsEn extends GlobalStreamChatLocalizations { @override String get replyToMessageLabel => 'Reply to Message'; + + @override + String attachmentLimitExceedError(int limit) => + 'Attachment limit exceeded, limit: $limit'; } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_es.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_es.dart index b43e4ba8..10d9b2a7 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_es.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_es.dart @@ -362,4 +362,10 @@ class StreamChatLocalizationsEs extends GlobalStreamChatLocalizations { @override String get replyToMessageLabel => 'Responder al Mensaje'; + + @override + String attachmentLimitExceedError(int limit) { + // TODO: implement attachmentLimitExceedError + throw UnimplementedError(); + } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart index 9d8586d5..17efdd70 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_fr.dart @@ -361,4 +361,10 @@ class StreamChatLocalizationsFr extends GlobalStreamChatLocalizations { @override String get replyToMessageLabel => 'Répondre au Message'; + + @override + String attachmentLimitExceedError(int limit) { + // TODO: implement attachmentLimitExceedError + throw UnimplementedError(); + } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart index 850e481b..c08f77a2 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_hi.dart @@ -356,4 +356,10 @@ class StreamChatLocalizationsHi extends GlobalStreamChatLocalizations { @override String get replyToMessageLabel => 'संदेश का जवाब'; + + @override + String attachmentLimitExceedError(int limit) { + // TODO: implement attachmentLimitExceedError + throw UnimplementedError(); + } } diff --git a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart index 8b2e1692..084878ee 100644 --- a/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart +++ b/packages/stream_chat_localizations/lib/src/stream_chat_localizations_it.dart @@ -358,4 +358,10 @@ Il file è troppo grande per essere caricato. Il limite è di $limitInMB MB.'''; @override String get replyToMessageLabel => 'Rispondi al messaggio'; + + @override + String attachmentLimitExceedError(int limit) { + // TODO: implement attachmentLimitExceedError + throw UnimplementedError(); + } } diff --git a/packages/stream_chat_localizations/test/translations_test.dart b/packages/stream_chat_localizations/test/translations_test.dart index 0e621159..f4eca409 100644 --- a/packages/stream_chat_localizations/test/translations_test.dart +++ b/packages/stream_chat_localizations/test/translations_test.dart @@ -177,6 +177,7 @@ void main() { expect(localizations.ofText, isNotNull); expect(localizations.fileText, isNotNull); expect(localizations.replyToMessageLabel, isNotNull); + expect(localizations.attachmentLimitExceedError(3), isNotNull); }); }