-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8d3a83
commit 3262301
Showing
10 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object?> get props => [message]; | ||
} | ||
|
||
class NameWithSpaceOnlyException extends VerifyNameException { | ||
const NameWithSpaceOnlyException() | ||
: super(VerifyNameException.spaceOnlyWithinName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Validator> { | ||
Either<Failure, Success> getValidatorNameViewState( | ||
NewNameRequest newNameRequest, | ||
) { | ||
for (final validator in this) { | ||
final either = validator.validate(newNameRequest); | ||
if (either.isLeft()) { | ||
return either; | ||
} | ||
} | ||
return Right<Failure, Success>(VerifyNameViewState()); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
lib/domain/model/extensions/validator_failure_extension.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ''; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
lib/domain/model/verification/composite_name_validator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<NewNameRequest> { | ||
final List<Validator> _listValidator; | ||
|
||
CompositeNameValidator(this._listValidator); | ||
|
||
@override | ||
Either<Failure, Success> validate(NewNameRequest value) { | ||
return _listValidator.isNotEmpty | ||
? _listValidator.getValidatorNameViewState(value) | ||
: Right<Failure, Success>(VerifyNameViewState()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
lib/domain/model/verification/name_with_space_only_validator.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<NewNameRequest> { | ||
@override | ||
Either<Failure, Success> validate(NewNameRequest value) { | ||
if (value.value != null && | ||
value.value!.isNotEmpty && | ||
value.value!.trim().isEmpty) { | ||
return const Left<Failure, Success>( | ||
VerifyNameFailure(NameWithSpaceOnlyException()), | ||
); | ||
} else { | ||
return Right<Failure, Success>(VerifyNameViewState()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import 'package:equatable/equatable.dart'; | ||
|
||
class NewNameRequest with EquatableMixin { | ||
final String? value; | ||
|
||
NewNameRequest(this.value); | ||
|
||
@override | ||
List<Object?> get props => [value]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> { | ||
Either<Failure, Success> validate(T value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Failure, Success> execute( | ||
String? newName, | ||
List<Validator> listValidator, | ||
) { | ||
try { | ||
return CompositeNameValidator(listValidator) | ||
.validate(NewNameRequest(newName)); | ||
} catch (exception) { | ||
return Left<Failure, Success>(VerifyNameFailure(exception)); | ||
} | ||
} | ||
} |