TF-1677: Fix validation for email forward

(cherry picked from commit b8741e08799c84ff96fd349d264f00d6b71e08cc)
This commit is contained in:
HuyNguyen
2023-04-05 15:52:50 +07:00
committed by Dat Vu
parent adf0e79cf8
commit abeaf07050
4 changed files with 46 additions and 8 deletions
@@ -21,7 +21,7 @@ import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
typedef OnSuggestionContactCallbackAction = Future<List<EmailAddress>> Function(String query);
typedef OnAddListContactCallbackAction = Function(List<EmailAddress> listEmailAddress);
typedef OnExceptionAddListContactCallbackAction = Function();
typedef OnExceptionAddListContactCallbackAction = Function(bool isListEmpty);
class AutocompleteContactTextFieldWithTags extends StatefulWidget {
@@ -49,6 +49,7 @@ class _AutocompleteContactTextFieldWithTagsState extends State<AutocompleteConta
final _responsiveUtils = Get.find<ResponsiveUtils>();
final _imagePaths = Get.find<ImagePaths>();
final GlobalKey<TagsEditorState> keyToEmailTagEditor = GlobalKey<TagsEditorState>();
late List<EmailAddress> listEmailAddress;
@@ -70,6 +71,7 @@ class _AutocompleteContactTextFieldWithTagsState extends State<AutocompleteConta
@override
Widget build(BuildContext context) {
final itemTagEditor = TagEditor<SuggestionEmailAddress>(
key: keyToEmailTagEditor,
length: listEmailAddress.length,
controller: widget.controller,
borderRadius: 12,
@@ -278,6 +280,23 @@ class _AutocompleteContactTextFieldWithTagsState extends State<AutocompleteConta
color: Colors.white,
fontWeight: FontWeight.w500))
..onPressActionClick(() {
if (widget.controller?.text.isNotEmpty == true) {
if (!_isDuplicatedRecipient(widget.controller?.text ?? '')) {
setState(() {
listEmailAddress.add(EmailAddress(null, widget.controller?.text));
});
_closeSuggestionBox();
} else {
_closeSuggestionBox();
return;
}
}
if (listEmailAddress.isEmpty) {
widget.onExceptionCallback?.call(true);
return;
}
if (_isValidAllEmailAddress(listEmailAddress) && _inputFieldIsEmpty()) {
widget.onAddContactCallback?.call(List.from(listEmailAddress));
setState(() {
@@ -285,10 +304,15 @@ class _AutocompleteContactTextFieldWithTagsState extends State<AutocompleteConta
listEmailAddress.clear();
});
} else {
widget.onExceptionCallback?.call();
widget.onExceptionCallback?.call(false);
}
})
..text(AppLocalizations.of(context).addRecipientButton, isVertical: false)
).build();
}
void _closeSuggestionBox() {
keyToEmailTagEditor.currentState?.resetTextField();
keyToEmailTagEditor.currentState?.closeSuggestionBox();
}
}
@@ -302,4 +302,16 @@ class ForwardController extends BaseController {
}
);
}
void handleExceptionCallback(BuildContext context, bool isListEmailEmpty) {
if (isListEmailEmpty) {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).emptyListEmailForward);
} else {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).incorrectEmailFormat);
}
}
}
@@ -1,7 +1,6 @@
import 'package:core/presentation/extensions/color_extension.dart';
import 'package:core/presentation/resources/image_paths.dart';
import 'package:core/presentation/state/success.dart';
import 'package:core/presentation/utils/app_toast.dart';
import 'package:core/presentation/utils/responsive_utils.dart';
import 'package:core/presentation/utils/style_utils.dart';
import 'package:flutter/material.dart';
@@ -18,7 +17,6 @@ import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
class ForwardView extends GetWidget<ForwardController> with AppLoaderMixin {
final _responsiveUtils = Get.find<ResponsiveUtils>();
final _imagePaths = Get.find<ImagePaths>();
final _appToast = Get.find<AppToast>();
ForwardView({Key? key}) : super(key: key);
@@ -134,10 +132,8 @@ class ForwardView extends GetWidget<ForwardController> with AppLoaderMixin {
onAddContactCallback: (listRecipientsSelected) {
controller.addRecipientAction(context, listRecipientsSelected);
},
onExceptionCallback: () {
_appToast.showToastErrorMessage(
context,
AppLocalizations.of(context).incorrectEmailFormat);
onExceptionCallback: (isListEmailEmpty) {
controller.handleExceptionCallback(context, isListEmailEmpty);
},
);
}
@@ -2827,4 +2827,10 @@ class AppLocalizations {
name: 'mailboxVisibilitySubtitle',
);
}
String get emptyListEmailForward {
return Intl.message(
'Please input at least one recipient',
name: 'emptyListEmailForward');
}
}