feat(auth)!: use form_bloc package

This commit is contained in:
2022-04-20 18:25:46 +02:00
parent 703e3deeaf
commit 27015b63d9
104 changed files with 311 additions and 2285 deletions
@@ -15,9 +15,9 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:wyatt_authentication_bloc/src/authentication/cubit/authentication_cubit.dart';
import 'package:wyatt_authentication_bloc/src/models/user/user_interface.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class AuthenticationBuilder extends StatelessWidget {
const AuthenticationBuilder({
@@ -16,9 +16,9 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:wyatt_authentication_bloc/src/form/form_status.dart';
import 'package:wyatt_authentication_bloc/src/models/exceptions/exceptions_interface.dart';
import 'package:wyatt_authentication_bloc/src/repositories/authentication_repository_interface.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart' show FormStatus;
part 'email_verification_state.dart';
@@ -1,22 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'form_data.dart';
export 'form_entry.dart';
export 'form_input.dart';
export 'form_input_status.dart';
export 'form_status.dart';
export 'form_validator.dart';
@@ -1,137 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/foundation.dart';
import 'package:wyatt_authentication_bloc/src/form/form_entry.dart';
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
@immutable
class FormData {
const FormData(this._entries);
FormData.empty() : this(<FormEntry>[]);
final List<FormEntry> _entries;
List<FormEntry> get entries => _entries;
List<FormInput<T, ValidationError>> inputs<T>() {
return _entries
.map((FormEntry entry) => entry.input as FormInput<T, ValidationError>)
.toList();
}
FormInput<T, ValidationError> input<T>(String field) {
if (contains(field)) {
return _entries
.firstWhere((FormEntry entry) => entry.field == field)
.input as FormInput<T, ValidationError>;
} else {
throw Exception('Field $field does not exist in form');
}
}
bool contains(String field) {
return _entries.any((FormEntry entry) => entry.field == field);
}
FormData intersection(FormData other) {
final List<FormEntry> entries = <FormEntry>[];
for (final FormEntry entry in _entries) {
if (other.contains(entry.field)) {
entries.add(entry);
}
}
return FormData(entries);
}
FormData difference(FormData other) {
final List<FormEntry> entries = <FormEntry>[];
for (final FormEntry otherEntry in other._entries) {
if (!contains(otherEntry.field)) {
entries.add(otherEntry);
}
}
for (final FormEntry entry in _entries) {
if (!other.contains(entry.field)) {
entries.add(entry);
}
}
return FormData(entries);
}
FormData union(FormData other) {
final List<FormEntry> entries = <FormEntry>[];
for (final FormEntry entry in _entries) {
entries.add(entry);
}
for (final FormEntry otherEntry in other._entries) {
if (!contains(otherEntry.field)) {
entries.add(otherEntry);
}
}
return FormData(entries);
}
void update(String field, FormInput input) {
if (contains(field)) {
final index = _entries.indexOf(
_entries.firstWhere((FormEntry entry) => entry.field == field),
);
_entries[index] = _entries[index].copyWith(input: input);
}
}
FormData clone() {
return FormData(
_entries
.map((FormEntry entry) => entry.clone())
.toList(),
);
}
Map<String, dynamic> toMap() {
final map = <String, dynamic>{};
for (final entry in _entries) {
if (entry.export) {
map[entry.fieldName ?? entry.field] = entry.input.value;
}
}
return map;
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is FormData && listEquals(other._entries, _entries);
}
@override
int get hashCode => _entries.hashCode;
@override
String toString() => 'FormData(entries: $_entries)';
}
@@ -1,77 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/foundation.dart';
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
@immutable
class FormEntry {
const FormEntry(this.field, this.input, {this.export = true, this.fieldName});
final String field;
final FormInput input;
final bool export;
final String? fieldName;
FormEntry copyWith({
String? field,
FormInput? input,
bool? export,
String? fieldName,
}) {
return FormEntry(
field ?? this.field,
input ?? this.input,
export: export ?? this.export,
fieldName: fieldName ?? this.fieldName,
);
}
FormEntry clone() {
return FormEntry(
field,
input,
export: export,
fieldName: fieldName,
);
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is FormEntry &&
other.field == field &&
other.input == input &&
other.export == export &&
other.fieldName == fieldName;
}
@override
int get hashCode {
return field.hashCode ^
input.hashCode ^
export.hashCode ^
fieldName.hashCode;
}
@override
String toString() {
// ignore: lines_longer_than_80_chars
return 'FormEntry(field: $field, input: $input, export: $export, fieldName: $fieldName)';
}
}
@@ -1,111 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/foundation.dart';
import 'package:wyatt_authentication_bloc/src/form/form_input_status.dart';
/// {@template form_input}
/// A [FormInput] represents the value of a single form input field.
/// It contains information about the [FormInputStatus], [value], as well
/// as validation status.
///
/// [FormInput] should be extended to define custom [FormInput] instances.
///
/// ```dart
/// enum FirstNameError { empty }
/// class FirstName extends FormInput<String, FirstNameError> {
/// const FirstName.pure({String value = ''}) : super.pure(value);
/// const FirstName.dirty({String value = ''}) : super.dirty(value);
///
/// @override
/// FirstNameError? validator(String value) {
/// return value.isEmpty ? FirstNameError.empty : null;
/// }
/// }
/// ```
/// {@endtemplate}
@immutable
abstract class FormInput<T, E> {
const FormInput._(this.value, [this.pure = true]);
/// Constructor which create a `pure` [FormInput] with a given value.
const FormInput.pure(T value) : this._(value);
/// Constructor which create a `dirty` [FormInput] with a given value.
const FormInput.dirty(T value) : this._(value, false);
/// The value of the given [FormInput].
/// For example, if you have a `FormInput` for `FirstName`,
/// the value could be 'Joe'.
final T value;
/// If the [FormInput] is pure (has been touched/modified).
/// Typically when the `FormInput` is initially created,
/// it is created using the `FormInput.pure` constructor to
/// signify that the user has not modified it.
///
/// For subsequent changes (in response to user input), the
/// `FormInput.dirty` constructor should be used to signify that
/// the `FormInput` has been manipulated.
final bool pure;
/// The [FormInputStatus] which can be one of the following:
/// * [FormInputStatus.pure]
/// - if the input has not been modified.
/// * [FormInputStatus.invalid]
/// - if the input has been modified and validation failed.
/// * [FormInputStatus.valid]
/// - if the input has been modified and validation succeeded.
FormInputStatus get status => pure
? FormInputStatus.pure
: valid
? FormInputStatus.valid
: FormInputStatus.invalid;
/// Returns a validation error if the [FormInput] is invalid.
/// Returns `null` if the [FormInput] is valid.
E? get error => validator(value);
/// Whether the [FormInput] value is valid according to the
/// overridden `validator`.
///
/// Returns `true` if `validator` returns `null` for the
/// current [FormInput] value and `false` otherwise.
bool get valid => validator(value) == null;
/// Whether the [FormInput] value is not valid.
/// A value is invalid when the overridden `validator`
/// returns an error (non-null value).
bool get invalid => status == FormInputStatus.invalid;
/// A function that must return a validation error if the provided
/// [value] is invalid and `null` otherwise.
E? validator(T value);
@override
int get hashCode => value.hashCode ^ pure.hashCode;
@override
bool operator ==(Object other) {
if (other.runtimeType != runtimeType) return false;
return other is FormInput<T, E> &&
other.value == value &&
other.pure == pure;
}
@override
String toString() => '$runtimeType($value, $pure)';
}
@@ -1,27 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Enum representing the status of a form input at any given point in time.
enum FormInputStatus {
/// The form input has not been touched.
pure,
/// The form input is valid.
valid,
/// The form input is not valid.
invalid,
}
@@ -1,79 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
/// Enum representing the status of a form at any given point in time.
enum FormStatus {
/// The form has not been touched.
pure,
/// The form has been completely validated.
valid,
/// The form contains one or more invalid inputs.
invalid,
/// The form is in the process of being submitted.
submissionInProgress,
/// The form has been submitted successfully.
submissionSuccess,
/// The form submission failed.
submissionFailure,
/// The form submission has been canceled.
submissionCanceled
}
const Set<FormStatus> _validatedFormStatuses = <FormStatus>{
FormStatus.valid,
FormStatus.submissionInProgress,
FormStatus.submissionSuccess,
FormStatus.submissionFailure,
FormStatus.submissionCanceled,
};
/// Useful extensions on [FormStatus]
extension FormStatusX on FormStatus {
/// Indicates whether the form is untouched.
bool get isPure => this == FormStatus.pure;
/// Indicates whether the form is completely validated.
bool get isValid => this == FormStatus.valid;
/// Indicates whether the form has been validated successfully.
/// This means the [FormStatus] is either:
/// * `FormStatus.valid`
/// * `FormStatus.submissionInProgress`
/// * `FormStatus.submissionSuccess`
/// * `FormStatus.submissionFailure`
bool get isValidated => _validatedFormStatuses.contains(this);
/// Indicates whether the form contains one or more invalid inputs.
bool get isInvalid => this == FormStatus.invalid;
/// Indicates whether the form is in the process of being submitted.
bool get isSubmissionInProgress => this == FormStatus.submissionInProgress;
/// Indicates whether the form has been submitted successfully.
bool get isSubmissionSuccess => this == FormStatus.submissionSuccess;
/// Indicates whether the form submission failed.
bool get isSubmissionFailure => this == FormStatus.submissionFailure;
/// Indicates whether the form submission has been canceled.
bool get isSubmissionCanceled => this == FormStatus.submissionCanceled;
}
@@ -1,32 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/form/form_input_status.dart';
import 'package:wyatt_authentication_bloc/src/form/form_status.dart';
/// Class which contains methods that help manipulate and manage
/// [FormStatus] and [FormInputStatus] instances.
class FormValidator {
/// Returns a [FormStatus] given a list of [FormInput].
static FormStatus validate(List<FormInput> inputs) {
return inputs.every((FormInput element) => element.pure)
? FormStatus.pure
: inputs.any((FormInput input) => input.valid == false)
? FormStatus.invalid
: FormStatus.valid;
}
}
@@ -1,35 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template boolean}
/// Form input for a bool input
/// {@endtemplate}
class Boolean extends FormInput<bool, ValidationError> {
/// {@macro boolean}
const Boolean.pure({bool? defaultValue = false})
: super.pure(defaultValue ?? false);
/// {@macro boolean}
const Boolean.dirty({bool value = false}) : super.dirty(value);
@override
ValidationError? validator(bool? value) {
return value != null ? null : ValidationError.invalid;
}
}
@@ -1,39 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template confirmed_password}
/// Form input for a confirmed password input.
/// {@endtemplate}
class ConfirmedPassword
extends FormInput<String, ValidationError> {
/// {@macro confirmed_password}
const ConfirmedPassword.pure({this.password = ''}) : super.pure('');
/// {@macro confirmed_password}
const ConfirmedPassword.dirty({required this.password, String value = ''})
: super.dirty(value);
/// The original password.
final String password;
@override
ValidationError? validator(String? value) {
return password == value ? null : ValidationError.invalid;
}
}
@@ -1,40 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template email}
/// Form input for an email input.
/// {@endtemplate}
class Email extends FormInput<String, ValidationError> {
/// {@macro email}
const Email.pure() : super.pure('');
/// {@macro email}
const Email.dirty([String value = '']) : super.dirty(value);
static final RegExp _emailRegExp = RegExp(
r'^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
);
@override
ValidationError? validator(String? value) {
return _emailRegExp.hasMatch(value ?? '')
? null
: ValidationError.invalid;
}
}
@@ -1,38 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template iban}
/// Form input for an IBAN input.
/// {@endtemplate}
class Iban extends FormInput<String, ValidationError> {
/// {@macro iban}
const Iban.pure() : super.pure('');
/// {@macro iban}
const Iban.dirty([String value = '']) : super.dirty(value);
static final RegExp _regExp = RegExp(
r'^(?:((?:IT|SM)\d{2}[A-Z]{1}\d{22})|(NL\d{2}[A-Z]{4}\d{10})|(LV\d{2}[A-Z]{4}\d{13})|((?:BG|GB|IE)\d{2}[A-Z]{4}\d{14})|(GI\d{2}[A-Z]{4}\d{15})|(RO\d{2}[A-Z]{4}\d{16})|(MT\d{2}[A-Z]{4}\d{23})|(NO\d{13})|((?:DK|FI)\d{16})|((?:SI)\d{17})|((?:AT|EE|LU|LT)\d{18})|((?:HR|LI|CH)\d{19})|((?:DE|VA)\d{20})|((?:AD|CZ|ES|MD|SK|SE)\d{22})|(PT\d{23})|((?:IS)\d{24})|((?:BE)\d{14})|((?:FR|MC|GR)\d{25})|((?:PL|HU|CY)\d{26}))$',
);
@override
ValidationError? validator(String? value) {
return _regExp.hasMatch(value ?? '') ? null : ValidationError.invalid;
}
}
@@ -14,16 +14,5 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
export 'boolean.dart';
export 'confirmed_password.dart';
export 'email.dart';
export 'exceptions/exceptions.dart';
export 'iban.dart';
export 'name.dart';
export 'operations.dart';
export 'password.dart';
export 'phone.dart';
export 'siren.dart';
export 'text_string.dart';
export 'user/user.dart';
export 'validation_error.dart';
@@ -1,36 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template name}
/// Form input for a name input.
/// {@endtemplate}
class Name extends FormInput<String, ValidationError> {
/// {@macro name}
const Name.pure() : super.pure('');
/// {@macro name}
const Name.dirty([String value = '']) : super.dirty(value);
static final RegExp _nameRegExp = RegExp(r"^([ \u00c0-\u01ffa-zA-Z'\-])+$");
@override
ValidationError? validator(String? value) {
return _nameRegExp.hasMatch(value ?? '') ? null : ValidationError.invalid;
}
}
@@ -1,17 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
enum SetOperation { replace, intersection, difference, union }
@@ -1,39 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template password}
/// Form input for a password input.
/// {@endtemplate}
class Password extends FormInput<String, ValidationError> {
/// {@macro password}
const Password.pure() : super.pure('');
/// {@macro password}
const Password.dirty([String value = '']) : super.dirty(value);
static final RegExp _passwordRegExp =
RegExp(r'^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$');
@override
ValidationError? validator(String? value) {
return _passwordRegExp.hasMatch(value ?? '')
? null
: ValidationError.invalid;
}
}
@@ -1,37 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template phone}
/// Form input for a phone input.
/// {@endtemplate}
class Phone extends FormInput<String, ValidationError> {
/// {@macro phone}
const Phone.pure() : super.pure('');
/// {@macro phone}
const Phone.dirty([String value = '']) : super.dirty(value);
static final RegExp _phoneRegExp =
RegExp(r'^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$');
@override
ValidationError? validator(String? value) {
return _phoneRegExp.hasMatch(value ?? '') ? null : ValidationError.invalid;
}
}
@@ -1,36 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template siren}
/// Form input for a SIREN input.
/// {@endtemplate}
class Siren extends FormInput<String, ValidationError> {
/// {@macro siren}
const Siren.pure() : super.pure('');
/// {@macro siren}
const Siren.dirty([String value = '']) : super.dirty(value);
static final RegExp _regExp = RegExp(r'(\d{9}|\d{3}[ ]\d{3}[ ]\d{3})$');
@override
ValidationError? validator(String? value) {
return _regExp.hasMatch(value ?? '') ? null : ValidationError.invalid;
}
}
@@ -1,36 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
/// {@template text_string}
/// Form input for a text input
/// {@endtemplate}
class TextString extends FormInput<String, ValidationError> {
/// {@macro text_string}
const TextString.pure() : super.pure('');
/// {@macro text_string}
const TextString.dirty([String value = '']) : super.dirty(value);
@override
ValidationError? validator(String? value) {
return (value?.isNotEmpty ?? false)
? null
: ValidationError.invalid;
}
}
@@ -1,20 +0,0 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
enum ValidationError {
/// Generic invalid error.
invalid
}
@@ -16,11 +16,9 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:wyatt_authentication_bloc/src/form/form_status.dart';
import 'package:wyatt_authentication_bloc/src/form/form_validator.dart';
import 'package:wyatt_authentication_bloc/src/models/email.dart';
import 'package:wyatt_authentication_bloc/src/models/exceptions/exceptions_interface.dart';
import 'package:wyatt_authentication_bloc/src/repositories/authentication_repository_interface.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'password_reset_state.dart';
@@ -35,7 +33,7 @@ class PasswordResetCubit extends Cubit<PasswordResetState> {
emit(
state.copyWith(
email: email,
status: FormValidator.validate([email]),
status: FormData.validate([email]),
),
);
}
@@ -17,12 +17,9 @@
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:wyatt_authentication_bloc/src/authentication/cubit/authentication_cubit.dart';
import 'package:wyatt_authentication_bloc/src/form/form_status.dart';
import 'package:wyatt_authentication_bloc/src/form/form_validator.dart';
import 'package:wyatt_authentication_bloc/src/models/email.dart';
import 'package:wyatt_authentication_bloc/src/models/exceptions/exceptions_interface.dart';
import 'package:wyatt_authentication_bloc/src/models/password.dart';
import 'package:wyatt_authentication_bloc/src/repositories/authentication_repository_interface.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'sign_in_state.dart';
@@ -42,7 +39,7 @@ class SignInCubit extends Cubit<SignInState> {
emit(
state.copyWith(
email: email,
status: FormValidator.validate([email, state.password]),
status: FormData.validate([email, state.password]),
),
);
}
@@ -52,13 +49,12 @@ class SignInCubit extends Cubit<SignInState> {
emit(
state.copyWith(
password: password,
status: FormValidator.validate([state.email, password]),
status: FormData.validate([state.email, password]),
),
);
}
Future<void> signInAnonymously() async {
// TODO(hpcl): Maybe check this in UI.
if (state.status.isSubmissionInProgress) {
return;
}
@@ -19,16 +19,9 @@ import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:flutter/foundation.dart';
import 'package:wyatt_authentication_bloc/src/authentication/cubit/authentication_cubit.dart';
import 'package:wyatt_authentication_bloc/src/form/form_data.dart';
import 'package:wyatt_authentication_bloc/src/form/form_input.dart';
import 'package:wyatt_authentication_bloc/src/form/form_status.dart';
import 'package:wyatt_authentication_bloc/src/form/form_validator.dart';
import 'package:wyatt_authentication_bloc/src/models/email.dart';
import 'package:wyatt_authentication_bloc/src/models/exceptions/exceptions_interface.dart';
import 'package:wyatt_authentication_bloc/src/models/operations.dart';
import 'package:wyatt_authentication_bloc/src/models/password.dart';
import 'package:wyatt_authentication_bloc/src/models/validation_error.dart';
import 'package:wyatt_authentication_bloc/src/repositories/authentication_repository_interface.dart';
import 'package:wyatt_form_bloc/wyatt_form_bloc.dart';
part 'sign_up_state.dart';
@@ -60,7 +53,7 @@ class SignUpCubit extends Cubit<SignUpState> {
emit(
state.copyWith(
email: email,
status: FormValidator.validate(inputsToValidate),
status: FormData.validate(inputsToValidate),
),
);
}
@@ -77,11 +70,12 @@ class SignUpCubit extends Cubit<SignUpState> {
emit(
state.copyWith(
password: password,
status: FormValidator.validate(inputsToValidate),
status: FormData.validate(inputsToValidate),
),
);
}
// Take from wyatt_form_bloc/wyatt_form_bloc.dart
void dataChanged<T>(String field, FormInput dirtyValue) {
final _form = state.data.clone();
@@ -94,13 +88,14 @@ class SignUpCubit extends Cubit<SignUpState> {
emit(
state.copyWith(
data: _form,
status: FormValidator.validate(
status: FormData.validate(
[state.email, state.password, ..._form.inputs<dynamic>()],
),
),
);
}
// Take from wyatt_form_bloc/wyatt_form_bloc.dart
void updateFormData(
FormData data, {
SetOperation operation = SetOperation.replace,
@@ -125,7 +120,7 @@ class SignUpCubit extends Cubit<SignUpState> {
emit(
state.copyWith(
data: _form,
status: FormValidator.validate(
status: FormData.validate(
[state.email, state.password, ..._form.inputs<dynamic>()],
),
),
@@ -16,7 +16,6 @@
export 'authentication/authentication.dart';
export 'email_verification/email_verification.dart';
export 'form/form.dart';
export 'models/models.dart';
export 'password_reset/password_reset.dart';
export 'repositories/repositories.dart';