diff --git a/assets/l10n/intl_en.arb b/assets/l10n/intl_en.arb index b99c4eed3e..736094b979 100644 --- a/assets/l10n/intl_en.arb +++ b/assets/l10n/intl_en.arb @@ -3095,5 +3095,6 @@ }, "enable_notifications":"Enable notifications", "disable_notifications":"Disable notifications", - "logoutDialogWarning": "You will lose access to encrypted messages. We recommend that you enable chat backups before logging out" + "logoutDialogWarning": "You will lose access to encrypted messages. We recommend that you enable chat backups before logging out", + "this_field_cannot_be_blank":"This field cannot be blank" } diff --git a/lib/domain/app_state/validator/verify_name_view_state.dart b/lib/domain/app_state/validator/verify_name_view_state.dart new file mode 100644 index 0000000000..0d778065b0 --- /dev/null +++ b/lib/domain/app_state/validator/verify_name_view_state.dart @@ -0,0 +1,8 @@ +import 'package:fluffychat/presentation/state/failure.dart'; +import 'package:fluffychat/presentation/state/success.dart'; + +class VerifyNameViewState extends UIState {} + +class VerifyNameFailure extends FeatureFailure { + const VerifyNameFailure(dynamic exception) : super(exception: exception); +} diff --git a/lib/domain/exception/verify_name_exception.dart b/lib/domain/exception/verify_name_exception.dart new file mode 100644 index 0000000000..6ed322c060 --- /dev/null +++ b/lib/domain/exception/verify_name_exception.dart @@ -0,0 +1,17 @@ +import 'package:equatable/equatable.dart'; + +abstract class VerifyNameException extends Equatable implements Exception { + static const spaceOnlyWithinName = 'The name cannot contain only spaces'; + + final String? message; + + const VerifyNameException(this.message); + + @override + List get props => [message]; +} + +class NameWithSpaceOnlyException extends VerifyNameException { + const NameWithSpaceOnlyException() + : super(VerifyNameException.spaceOnlyWithinName); +} diff --git a/lib/domain/model/extensions/list_validator_extension.dart b/lib/domain/model/extensions/list_validator_extension.dart new file mode 100644 index 0000000000..b7af9e2453 --- /dev/null +++ b/lib/domain/model/extensions/list_validator_extension.dart @@ -0,0 +1,20 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +extension ListValidatorExtension on List { + Either getValidatorNameViewState( + NewNameRequest newNameRequest, + ) { + for (final validator in this) { + final either = validator.validate(newNameRequest); + if (either.isLeft()) { + return either; + } + } + return Right(VerifyNameViewState()); + } +} diff --git a/lib/domain/model/extensions/validator_failure_extension.dart b/lib/domain/model/extensions/validator_failure_extension.dart new file mode 100644 index 0000000000..d19343f409 --- /dev/null +++ b/lib/domain/model/extensions/validator_failure_extension.dart @@ -0,0 +1,14 @@ +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/exception/verify_name_exception.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; + +extension ValidatorFailureExtension on VerifyNameFailure { + String getMessage(BuildContext context) { + if (exception is NameWithSpaceOnlyException) { + return L10n.of(context)!.this_field_cannot_be_blank; + } else { + return ''; + } + } +} diff --git a/lib/domain/model/verification/composite_name_validator.dart b/lib/domain/model/verification/composite_name_validator.dart new file mode 100644 index 0000000000..05eef0808b --- /dev/null +++ b/lib/domain/model/verification/composite_name_validator.dart @@ -0,0 +1,20 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; +import 'package:fluffychat/domain/model/extensions/list_validator_extension.dart'; + +class CompositeNameValidator extends Validator { + final List _listValidator; + + CompositeNameValidator(this._listValidator); + + @override + Either validate(NewNameRequest value) { + return _listValidator.isNotEmpty + ? _listValidator.getValidatorNameViewState(value) + : Right(VerifyNameViewState()); + } +} diff --git a/lib/domain/model/verification/name_with_space_only_validator.dart b/lib/domain/model/verification/name_with_space_only_validator.dart new file mode 100644 index 0000000000..e1169c3432 --- /dev/null +++ b/lib/domain/model/verification/name_with_space_only_validator.dart @@ -0,0 +1,22 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/exception/verify_name_exception.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +class NameWithSpaceOnlyValidator extends Validator { + @override + Either validate(NewNameRequest value) { + if (value.value != null && + value.value!.isNotEmpty && + value.value!.trim().isEmpty) { + return const Left( + VerifyNameFailure(NameWithSpaceOnlyException()), + ); + } else { + return Right(VerifyNameViewState()); + } + } +} diff --git a/lib/domain/model/verification/new_name_request.dart b/lib/domain/model/verification/new_name_request.dart new file mode 100644 index 0000000000..51af99fefd --- /dev/null +++ b/lib/domain/model/verification/new_name_request.dart @@ -0,0 +1,10 @@ +import 'package:equatable/equatable.dart'; + +class NewNameRequest with EquatableMixin { + final String? value; + + NewNameRequest(this.value); + + @override + List get props => [value]; +} diff --git a/lib/domain/model/verification/validator.dart b/lib/domain/model/verification/validator.dart new file mode 100644 index 0000000000..266a63eabb --- /dev/null +++ b/lib/domain/model/verification/validator.dart @@ -0,0 +1,7 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; + +abstract class Validator { + Either validate(T value); +} diff --git a/lib/domain/usecase/verify_name_interactor.dart b/lib/domain/usecase/verify_name_interactor.dart new file mode 100644 index 0000000000..308ef83c7d --- /dev/null +++ b/lib/domain/usecase/verify_name_interactor.dart @@ -0,0 +1,21 @@ +import 'package:dartz/dartz.dart'; +import 'package:fluffychat/app_state/failure.dart'; +import 'package:fluffychat/app_state/success.dart'; +import 'package:fluffychat/domain/app_state/validator/verify_name_view_state.dart'; +import 'package:fluffychat/domain/model/verification/composite_name_validator.dart'; +import 'package:fluffychat/domain/model/verification/new_name_request.dart'; +import 'package:fluffychat/domain/model/verification/validator.dart'; + +class VerifyNameInteractor { + Either execute( + String? newName, + List listValidator, + ) { + try { + return CompositeNameValidator(listValidator) + .validate(NewNameRequest(newName)); + } catch (exception) { + return Left(VerifyNameFailure(exception)); + } + } +}