TF-233 Add mailbox name validator
This commit is contained in:
@@ -44,7 +44,7 @@ abstract class InputDecorationBuilder {
|
||||
errorBorder = newErrorBorder;
|
||||
}
|
||||
|
||||
void setErrorText(String newText) {
|
||||
void setErrorText(String? newText) {
|
||||
errorText = newText;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
abstract class VerifyNameException extends Equatable implements Exception {
|
||||
static const EmptyName = 'The name cannot be empty!';
|
||||
static const DuplicatedName = 'The name already exists!';
|
||||
static const NameContainSpecialCharacter = 'The name cannot contain special characters';
|
||||
|
||||
final String? message;
|
||||
|
||||
VerifyNameException(this.message);
|
||||
}
|
||||
|
||||
class EmptyNameException extends VerifyNameException {
|
||||
EmptyNameException() : super(VerifyNameException.EmptyName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class DuplicatedNameException extends VerifyNameException {
|
||||
DuplicatedNameException() : super(VerifyNameException.DuplicatedName);
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class SpecialCharacterException extends VerifyNameException {
|
||||
SpecialCharacterException() : super(VerifyNameException.NameContainSpecialCharacter);
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
extension ListValidatorExtension on List<Validator> {
|
||||
Either<Failure, Success> getValidatorNameViewState(NewNameRequest newNameRequest) {
|
||||
for (var validator in this) {
|
||||
final either = validator.validate(newNameRequest);
|
||||
if (either.isLeft()) {
|
||||
return either;
|
||||
}
|
||||
}
|
||||
return Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
extension NameValidatorStringExtension on String {
|
||||
bool hasSpecialCharactersInName() {
|
||||
return RegExp(r'^([@#%^$~!%&*()+=\|{}/<>:;?`])').hasMatch(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/extensions/list_validator_extension.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
class CompositeNameValidator extends Validator<NewNameRequest> {
|
||||
|
||||
final List<Validator> _listValidator;
|
||||
|
||||
CompositeNameValidator(this._listValidator);
|
||||
|
||||
@override
|
||||
Either<Failure, Success> validate(NewNameRequest newNameRequest) {
|
||||
return _listValidator.isNotEmpty
|
||||
? _listValidator.getValidatorNameViewState(newNameRequest)
|
||||
: Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/exceptions/verify_name_exception.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
class DuplicateNameValidator extends Validator<NewNameRequest> {
|
||||
final List<String> _listName;
|
||||
|
||||
DuplicateNameValidator(this._listName);
|
||||
|
||||
@override
|
||||
Either<Failure, Success> validate(NewNameRequest newNameRequest) {
|
||||
if (newNameRequest.value != null) {
|
||||
final nameExist = _listName
|
||||
.map((nameItem) => nameItem.toLowerCase())
|
||||
.contains(newNameRequest.value!.toLowerCase());
|
||||
if (nameExist) {
|
||||
return Left<Failure, Success>(VerifyNameFailure(DuplicatedNameException()));
|
||||
} else {
|
||||
return Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
} else {
|
||||
return Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/exceptions/verify_name_exception.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
class EmptyNameValidator extends Validator<NewNameRequest> {
|
||||
|
||||
@override
|
||||
Either<Failure, Success> validate(NewNameRequest newNameRequest) {
|
||||
if (newNameRequest.value == null || newNameRequest.value!.isEmpty) {
|
||||
return Left<Failure, Success>(VerifyNameFailure(EmptyNameException()));
|
||||
} else {
|
||||
return Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class NewNameRequest with EquatableMixin {
|
||||
final String? value;
|
||||
|
||||
NewNameRequest(this.value);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [value];
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/exceptions/verify_name_exception.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/extensions/name_validator_string_extension.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
class SpecialCharacterValidator extends Validator<NewNameRequest> {
|
||||
|
||||
@override
|
||||
Either<Failure, Success> validate(NewNameRequest newNameRequest) {
|
||||
if (newNameRequest.value != null && newNameRequest.value!.hasSpecialCharactersInName()) {
|
||||
return Left<Failure, Success>(VerifyNameFailure(SpecialCharacterException()));
|
||||
} else {
|
||||
return Right<Failure, Success>(VerifyNameViewState());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
|
||||
abstract class Validator<T> {
|
||||
Either<Failure, Success> validate(T value);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
|
||||
class VerifyNameViewState extends UIState {
|
||||
VerifyNameViewState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class VerifyNameFailure extends FeatureFailure {
|
||||
final exception;
|
||||
|
||||
VerifyNameFailure(this.exception);
|
||||
@override
|
||||
List<Object> get props => [exception];
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/composite_name_validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/new_name_request.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
|
||||
class VerifyNameInteractor {
|
||||
Either<Failure, Success> execute(String? newName, List<Validator> listValidator) {
|
||||
try {
|
||||
return CompositeNameValidator(listValidator).validate(NewNameRequest(newName));
|
||||
} catch (exception) {
|
||||
return Left<Failure, Success>(VerifyNameFailure(exception));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/exceptions/verify_name_exception.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart';
|
||||
|
||||
extension ValicatorFailureExtension on VerifyNameFailure {
|
||||
|
||||
String getMessage(BuildContext context) {
|
||||
if (exception is EmptyNameException) {
|
||||
return AppLocalizations.of(context).name_of_mailbox_is_required;
|
||||
} else if (exception is DuplicatedNameException) {
|
||||
return AppLocalizations.of(context).this_folder_name_is_already_taken;
|
||||
} else if (exception is SpecialCharacterException) {
|
||||
return AppLocalizations.of(context).mailbox_name_cannot_contain_special_characters;
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,15 @@
|
||||
import 'package:get/get.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_name_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/presentation/mailbox_creator_controller.dart';
|
||||
|
||||
class MailboxCreatorBindings extends Bindings {
|
||||
|
||||
@override
|
||||
void dependencies() {
|
||||
Get.lazyPut(() => MailboxCreatorController());
|
||||
Get.lazyPut(() => VerifyNameInteractor());
|
||||
|
||||
Get.lazyPut(() => MailboxCreatorController(
|
||||
Get.find<VerifyNameInteractor>(),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,34 @@
|
||||
|
||||
import 'package:core/core.dart';
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:model/model.dart';
|
||||
import 'package:tmail_ui_user/features/base/base_controller.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/duplicate_name_validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/empty_name_validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/model/verification/special_character_validator.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/state/verify_name_view_state.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/domain/usecases/verify_name_interactor.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/presentation/extensions/validator_failure_extension.dart';
|
||||
import 'package:tmail_ui_user/features/mailbox_creator/presentation/model/mailbox_creator_arguments.dart';
|
||||
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
|
||||
import 'package:tmail_ui_user/main/utils/app_logger.dart';
|
||||
|
||||
class MailboxCreatorController extends BaseController {
|
||||
|
||||
final VerifyNameInteractor _verifyNameInteractor;
|
||||
|
||||
final selectedMailbox = Rxn<PresentationMailbox>();
|
||||
final newNameMailbox = Rxn<String>();
|
||||
|
||||
List<PresentationMailbox> allMailboxes = <PresentationMailbox>[];
|
||||
|
||||
MailboxCreatorController();
|
||||
MailboxCreatorController(
|
||||
this._verifyNameInteractor,
|
||||
);
|
||||
|
||||
void setNewNameMailbox(String newName) => newNameMailbox.value = newName;
|
||||
|
||||
@override
|
||||
void onReady() {
|
||||
@@ -38,7 +52,41 @@ class MailboxCreatorController extends BaseController {
|
||||
@override
|
||||
void onError(error) {}
|
||||
|
||||
void closeMailboxCreator() {
|
||||
bool isCreateMailboxValidated(BuildContext context) {
|
||||
final nameValidated = getErrorInputNameString(context);
|
||||
if (nameValidated?.isNotEmpty == true) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
String? getErrorInputNameString(BuildContext context) {
|
||||
final nameMailbox = newNameMailbox.value;
|
||||
final listName = allMailboxes
|
||||
.where((mailbox) => !(mailbox.name.isBlank == true))
|
||||
.map((mailbox) => mailbox.name!.name).toList();
|
||||
|
||||
return _verifyNameInteractor.execute(
|
||||
nameMailbox,
|
||||
[
|
||||
EmptyNameValidator(),
|
||||
DuplicateNameValidator(listName),
|
||||
SpecialCharacterValidator(),
|
||||
]
|
||||
).fold(
|
||||
(failure) {
|
||||
if (failure is VerifyNameFailure) {
|
||||
return failure.getMessage(context);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
(success) => null
|
||||
);
|
||||
}
|
||||
|
||||
void closeMailboxCreator(BuildContext context) {
|
||||
FocusScope.of(context).unfocus();
|
||||
popBack();
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ class MailboxCreatorView extends GetWidget<MailboxCreatorController> {
|
||||
borderOnForeground: false,
|
||||
color: Colors.transparent,
|
||||
child: GestureDetector(
|
||||
onTap: () => controller.closeMailboxCreator(),
|
||||
onTap: () => controller.closeMailboxCreator(context),
|
||||
child: ResponsiveWidget(
|
||||
responsiveUtils: _responsiveUtils,
|
||||
mobile: Container(
|
||||
@@ -92,22 +92,31 @@ class MailboxCreatorView extends GetWidget<MailboxCreatorController> {
|
||||
|
||||
Widget _buildAppBar(BuildContext context) {
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(top: 10, bottom: 4),
|
||||
child: (AppBarMailboxCreatorWidget(context)
|
||||
..addOnCancelActionClick(() => controller.closeMailboxCreator())
|
||||
..addOnCreateActionClick(() {}))
|
||||
.build());
|
||||
padding: EdgeInsets.only(top: 5),
|
||||
child: Obx(() => (AppBarMailboxCreatorWidget(
|
||||
context,
|
||||
isValidated: controller.isCreateMailboxValidated(context))
|
||||
..addOnCancelActionClick(() => controller.closeMailboxCreator(context))
|
||||
..addOnCreateActionClick(() => {}))
|
||||
.build())
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCreateMailboxNameInput(BuildContext context) {
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 14),
|
||||
child: (TextFieldBuilder()
|
||||
child: Obx(() => (TextFieldBuilder()
|
||||
..key(Key('create_mailbox_name_input'))
|
||||
..onChange((value) => {})
|
||||
..onChange((value) => controller.setNewNameMailbox(value))
|
||||
..keyboardType(TextInputType.visiblePassword)
|
||||
..textDecoration((CreateMailboxNameInputDecorationBuilder()).build()))
|
||||
.build());
|
||||
..cursorColor(AppColor.colorTextButton)
|
||||
..autoFocus(true)
|
||||
..textStyle(TextStyle(color: AppColor.colorNameEmail, fontSize: 16))
|
||||
..textDecoration((CreateMailboxNameInputDecorationBuilder()
|
||||
..setErrorText(controller.getErrorInputNameString(context)))
|
||||
.build()))
|
||||
.build())
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMailboxLocation(BuildContext context) {
|
||||
|
||||
+1
-1
@@ -71,7 +71,7 @@ class AppBarMailboxCreatorWidget {
|
||||
AppLocalizations.of(_context).done,
|
||||
style: TextStyle(fontSize: 17, color: isValidated ? AppColor.colorTextButton : AppColor.colorDisableMailboxCreateButton),
|
||||
),
|
||||
onPressed: () => _createActionClick?.call()
|
||||
onPressed: () => isValidated ? _createActionClick?.call() : null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
+3
@@ -16,6 +16,9 @@ class CreateMailboxNameInputDecorationBuilder extends InputDecorationBuilder {
|
||||
errorBorder: errorBorder ?? OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10)),
|
||||
borderSide: BorderSide(width: 1, color: AppColor.colorInputBorderErrorVerifyName)),
|
||||
focusedErrorBorder: errorBorder ?? OutlineInputBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(10)),
|
||||
borderSide: BorderSide(width: 1, color: AppColor.colorInputBorderErrorVerifyName)),
|
||||
prefixText: prefixText,
|
||||
labelText: labelText,
|
||||
floatingLabelBehavior: FloatingLabelBehavior.never,
|
||||
|
||||
@@ -613,4 +613,25 @@ class AppLocalizations {
|
||||
name: 'default_mailbox',
|
||||
);
|
||||
}
|
||||
|
||||
String get name_of_mailbox_is_required {
|
||||
return Intl.message(
|
||||
'Name of mailbox is required',
|
||||
name: 'name_of_mailbox_is_required',
|
||||
);
|
||||
}
|
||||
|
||||
String get mailbox_name_cannot_contain_special_characters {
|
||||
return Intl.message(
|
||||
'Mailbox name cannot contain special characters',
|
||||
name: 'mailbox_name_cannot_contain_special_characters',
|
||||
);
|
||||
}
|
||||
|
||||
String get this_folder_name_is_already_taken {
|
||||
return Intl.message(
|
||||
'This folder name is already taken',
|
||||
name: 'this_folder_name_is_already_taken',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user