diff --git a/packages/wyatt_authentication_bloc/example/lib/app/app.dart b/packages/wyatt_authentication_bloc/example/lib/app/app.dart index 2886a69d..89338fe5 100644 --- a/packages/wyatt_authentication_bloc/example/lib/app/app.dart +++ b/packages/wyatt_authentication_bloc/example/lib/app/app.dart @@ -31,23 +31,29 @@ class App extends StatelessWidget { static FormData getNormalFormData() { return const FormData([ - FormEntry(formFieldName, Name.pure()), - FormEntry(formFieldPhone, Phone.pure()), - FormEntry(formFieldPro, Boolean.pure()), - FormEntry(formFieldConfirmedPassword, ConfirmedPassword.pure(), - export: false), + FormInput(formFieldName, Name.pure()), + FormInput(formFieldPhone, Phone.pure()), + FormInput(formFieldPro, Boolean.pure()), + FormInput( + formFieldConfirmedPassword, + ConfirmedPassword.pure(), + metadata: FormInputMetadata(export: false), + ), ]); } static FormData getProFormData() { return const FormData([ - FormEntry(formFieldName, Name.pure()), - FormEntry(formFieldPhone, Phone.pure()), - FormEntry(formFieldPro, Boolean.pure()), - FormEntry(formFieldSiren, Siren.pure()), - FormEntry(formFieldIban, Iban.pure()), - FormEntry(formFieldConfirmedPassword, ConfirmedPassword.pure(), - export: false), + FormInput(formFieldName, Name.pure()), + FormInput(formFieldPhone, Phone.pure()), + FormInput(formFieldPro, Boolean.pure()), + FormInput(formFieldSiren, Siren.pure()), + FormInput(formFieldIban, Iban.pure()), + FormInput( + formFieldConfirmedPassword, + ConfirmedPassword.pure(), + metadata: FormInputMetadata(export: false), + ), ]); } @@ -103,42 +109,42 @@ class App extends StatelessWidget { @override Widget build(BuildContext context) { - AuthenticationRepositoryInterface _authenticationRepository = + AuthenticationRepositoryInterface authenticationRepository = AuthenticationRepositoryFirebase(); - AuthenticationCubit _authenticationCubit = AuthenticationCubit( - authenticationRepository: _authenticationRepository, + AuthenticationCubit authenticationCubit = AuthenticationCubit( + authenticationRepository: authenticationRepository, onAuthSuccess: onAuthSuccess, ); - SignUpCubit _signUpCubit = SignUpCubit( - authenticationRepository: _authenticationRepository, - authenticationCubit: _authenticationCubit, + SignUpCubit signUpCubit = SignUpCubit( + authenticationRepository: authenticationRepository, + authenticationCubit: authenticationCubit, entries: getNormalFormData(), onSignUpSuccess: onSignUpSuccess, ); - SignInCubit _signInCubit = SignInCubit( - authenticationRepository: _authenticationRepository, - authenticationCubit: _authenticationCubit, + SignInCubit signInCubit = SignInCubit( + authenticationRepository: authenticationRepository, + authenticationCubit: authenticationCubit, ); return MultiRepositoryProvider( providers: [ RepositoryProvider( - create: (context) => _authenticationRepository, + create: (context) => authenticationRepository, ), ], child: MultiBlocProvider( providers: [ BlocProvider( - create: (context) => _authenticationCubit..init(), + create: (context) => authenticationCubit..init(), ), BlocProvider( - create: (context) => _signUpCubit, + create: (context) => signUpCubit, ), BlocProvider( - create: (context) => _signInCubit, + create: (context) => signInCubit, ), ], child: const AppView(), diff --git a/packages/wyatt_authentication_bloc/example/lib/sign_up/widgets/sign_up_form.dart b/packages/wyatt_authentication_bloc/example/lib/sign_up/widgets/sign_up_form.dart index a44c0d0f..36f2e231 100644 --- a/packages/wyatt_authentication_bloc/example/lib/sign_up/widgets/sign_up_form.dart +++ b/packages/wyatt_authentication_bloc/example/lib/sign_up/widgets/sign_up_form.dart @@ -37,7 +37,7 @@ class _NameInput extends StatelessWidget { labelText: 'name', helperText: '', errorText: - state.data.input(formFieldName).invalid ? 'invalid name' : null, + state.data.isNotValid(formFieldName) ? 'invalid name' : null, ), ); }, @@ -58,7 +58,7 @@ class _PhoneInput extends StatelessWidget { decoration: InputDecoration( labelText: 'phone', helperText: '', - errorText: state.data.input(formFieldPhone).invalid + errorText: state.data.isNotValid(formFieldPhone) ? 'invalid phone' : null, ), @@ -81,7 +81,7 @@ class _SirenInput extends StatelessWidget { decoration: InputDecoration( labelText: 'siren', helperText: '', - errorText: state.data.input(formFieldSiren).invalid + errorText: state.data.isNotValid(formFieldSiren) ? 'invalid SIREN' : null, ), @@ -105,7 +105,7 @@ class _IbanInput extends StatelessWidget { labelText: 'iban', helperText: '', errorText: - state.data.input(formFieldIban).invalid ? 'invalid IBAN' : null, + state.data.isNotValid(formFieldIban) ? 'invalid IBAN' : null, ), ); }, @@ -149,8 +149,7 @@ class _PasswordInput extends StatelessWidget { .read() .state .data - .input(formFieldConfirmedPassword) - .value, + .valueOf(formFieldConfirmedPassword), ), ); }, @@ -185,7 +184,7 @@ class _ConfirmPasswordInput extends StatelessWidget { decoration: InputDecoration( labelText: 'confirm password', helperText: '', - errorText: state.data.input(formFieldConfirmedPassword).invalid + errorText: state.data.isNotValid(formFieldConfirmedPassword) ? 'passwords do not match' : null, ), @@ -203,7 +202,7 @@ class _CheckIsProInput extends StatelessWidget { trailing: BlocBuilder( builder: (context, state) { return Checkbox( - value: state.data.input(formFieldPro).value, + value: state.data.valueOf(formFieldPro), onChanged: (isPro) { final value = isPro!; // tristate is false, so value can't be null @@ -297,7 +296,7 @@ class SignUpForm extends StatelessWidget { const SizedBox(height: 8), BlocBuilder( builder: (context, state) { - if (state.data.input(formFieldPro).value) { + if (state.data.valueOf(formFieldPro)) { return Column(children: [ _SirenInput(), const SizedBox(height: 8), diff --git a/packages/wyatt_authentication_bloc/lib/src/password_reset/cubit/password_reset_cubit.dart b/packages/wyatt_authentication_bloc/lib/src/password_reset/cubit/password_reset_cubit.dart index b71935d0..2252cc3d 100644 --- a/packages/wyatt_authentication_bloc/lib/src/password_reset/cubit/password_reset_cubit.dart +++ b/packages/wyatt_authentication_bloc/lib/src/password_reset/cubit/password_reset_cubit.dart @@ -33,7 +33,7 @@ class PasswordResetCubit extends Cubit { emit( state.copyWith( email: email, - status: FormData.validate([email]), + status: FormStatus.validate([email]), ), ); } diff --git a/packages/wyatt_authentication_bloc/lib/src/sign_in/cubit/sign_in_cubit.dart b/packages/wyatt_authentication_bloc/lib/src/sign_in/cubit/sign_in_cubit.dart index f90b0ca5..b30dbab9 100644 --- a/packages/wyatt_authentication_bloc/lib/src/sign_in/cubit/sign_in_cubit.dart +++ b/packages/wyatt_authentication_bloc/lib/src/sign_in/cubit/sign_in_cubit.dart @@ -39,7 +39,7 @@ class SignInCubit extends Cubit { emit( state.copyWith( email: email, - status: FormData.validate([email, state.password]), + status: FormStatus.validate([email, state.password]), ), ); } @@ -49,7 +49,7 @@ class SignInCubit extends Cubit { emit( state.copyWith( password: password, - status: FormData.validate([state.email, password]), + status: FormStatus.validate([state.email, password]), ), ); } diff --git a/packages/wyatt_authentication_bloc/lib/src/sign_up/cubit/sign_up_cubit.dart b/packages/wyatt_authentication_bloc/lib/src/sign_up/cubit/sign_up_cubit.dart index ed08bcc4..31556053 100644 --- a/packages/wyatt_authentication_bloc/lib/src/sign_up/cubit/sign_up_cubit.dart +++ b/packages/wyatt_authentication_bloc/lib/src/sign_up/cubit/sign_up_cubit.dart @@ -44,16 +44,16 @@ class SignUpCubit extends Cubit { void emailChanged(String value) { final Email email = Email.dirty(value); - final List> inputsToValidate = [ + final List> toValidate = [ email, state.password, - ...state.data.inputs(), + ...state.data.validators(), ]; emit( state.copyWith( email: email, - status: FormData.validate(inputsToValidate), + status: FormStatus.validate(toValidate), ), ); } @@ -61,26 +61,26 @@ class SignUpCubit extends Cubit { void passwordChanged(String value) { final Password password = Password.dirty(value); - final List> inputsToValidate = [ + final List> toValidate = [ state.email, password, - ...state.data.inputs(), + ...state.data.validators(), ]; emit( state.copyWith( password: password, - status: FormData.validate(inputsToValidate), + status: FormStatus.validate(toValidate), ), ); } // Take from wyatt_form_bloc/wyatt_form_bloc.dart - void dataChanged(String field, FormInput dirtyValue) { + void dataChanged(String field, FormInputValidator dirtyValue) { final _form = state.data.clone(); if (_form.contains(field)) { - _form.update(field, dirtyValue); + _form.updateValidator(field, dirtyValue); } else { throw Exception('Form field $field not found'); } @@ -88,8 +88,12 @@ class SignUpCubit extends Cubit { emit( state.copyWith( data: _form, - status: FormData.validate( - [state.email, state.password, ..._form.inputs()], + status: FormStatus.validate( + [ + state.email, + state.password, + ...state.data.validators(), + ], ), ), ); @@ -120,8 +124,12 @@ class SignUpCubit extends Cubit { emit( state.copyWith( data: _form, - status: FormData.validate( - [state.email, state.password, ..._form.inputs()], + status: FormStatus.validate( + [ + state.email, + state.password, + ...state.data.validators(), + ], ), ), ); diff --git a/packages/wyatt_authentication_bloc/test/sign_up/sign_up_cubit_test.dart b/packages/wyatt_authentication_bloc/test/sign_up/sign_up_cubit_test.dart index f34fc404..08977706 100644 --- a/packages/wyatt_authentication_bloc/test/sign_up/sign_up_cubit_test.dart +++ b/packages/wyatt_authentication_bloc/test/sign_up/sign_up_cubit_test.dart @@ -68,9 +68,9 @@ void main() { SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ).state, - SignUpState(data: FormData.empty()), + const SignUpState(data: FormData.empty()), ); }); @@ -80,11 +80,11 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), act: (SignUpCubit cubit) => cubit.emailChanged(invalidEmailString), expect: () => [ - SignUpState( + const SignUpState( email: invalidEmail, status: FormStatus.invalid, data: FormData.empty(), @@ -97,15 +97,15 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), - seed: () => SignUpState( + seed: () => const SignUpState( password: validPassword, data: FormData.empty(), ), act: (SignUpCubit cubit) => cubit.emailChanged(validEmailString), expect: () => [ - SignUpState( + const SignUpState( email: validEmail, password: validPassword, status: FormStatus.valid, @@ -121,12 +121,12 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), act: (SignUpCubit cubit) => cubit.passwordChanged(invalidPasswordString), expect: () => [ - SignUpState( + const SignUpState( password: invalidPassword, status: FormStatus.invalid, data: FormData.empty(), @@ -139,15 +139,15 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), - seed: () => SignUpState( + seed: () => const SignUpState( email: validEmail, data: FormData.empty(), ), act: (SignUpCubit cubit) => cubit.passwordChanged(validPasswordString), expect: () => [ - SignUpState( + const SignUpState( email: validEmail, password: validPassword, status: FormStatus.valid, @@ -163,7 +163,7 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(), expect: () => const [], @@ -174,9 +174,9 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), - seed: () => SignUpState( + seed: () => const SignUpState( status: FormStatus.valid, email: validEmail, password: validPassword, @@ -199,9 +199,9 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), - seed: () => SignUpState( + seed: () => const SignUpState( status: FormStatus.valid, email: validEmail, password: validPassword, @@ -209,13 +209,13 @@ void main() { ), act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(), expect: () => [ - SignUpState( + const SignUpState( status: FormStatus.submissionInProgress, email: validEmail, password: validPassword, data: FormData.empty(), ), - SignUpState( + const SignUpState( status: FormStatus.submissionSuccess, email: validEmail, password: validPassword, @@ -238,9 +238,9 @@ void main() { build: () => SignUpCubit( authenticationRepository: authenticationRepository, authenticationCubit: authenticationCubit, - entries: FormData.empty(), + entries: const FormData.empty(), ), - seed: () => SignUpState( + seed: () => const SignUpState( status: FormStatus.valid, email: validEmail, password: validPassword, @@ -248,13 +248,13 @@ void main() { ), act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(), expect: () => [ - SignUpState( + const SignUpState( status: FormStatus.submissionInProgress, email: validEmail, password: validPassword, data: FormData.empty(), ), - SignUpState( + const SignUpState( status: FormStatus.submissionFailure, email: validEmail, password: validPassword, diff --git a/packages/wyatt_authentication_bloc/test/sign_up/sign_up_state_test.dart b/packages/wyatt_authentication_bloc/test/sign_up/sign_up_state_test.dart index fe3af1e3..722c3043 100644 --- a/packages/wyatt_authentication_bloc/test/sign_up/sign_up_state_test.dart +++ b/packages/wyatt_authentication_bloc/test/sign_up/sign_up_state_test.dart @@ -26,10 +26,10 @@ void main() { group('SignUpState', () { test('supports value comparisons', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ), - SignUpState( + const SignUpState( data: FormData.empty(), ), ); @@ -37,10 +37,10 @@ void main() { test('returns same object when no properties are passed', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ).copyWith(), - SignUpState( + const SignUpState( data: FormData.empty(), ), ); @@ -48,10 +48,10 @@ void main() { test('returns object with updated status when status is passed', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ).copyWith(status: FormStatus.pure), - SignUpState( + const SignUpState( data: FormData.empty(), ), ); @@ -59,10 +59,10 @@ void main() { test('returns object with updated email when email is passed', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ).copyWith(email: email), - SignUpState( + const SignUpState( email: email, data: FormData.empty(), ), @@ -71,10 +71,10 @@ void main() { test('returns object with updated password when password is passed', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ).copyWith(password: password), - SignUpState( + const SignUpState( password: password, data: FormData.empty(), ), @@ -85,12 +85,12 @@ void main() { 'returns object with updated data' ' when data is passed', () { expect( - SignUpState( + const SignUpState( data: FormData.empty(), ).copyWith( data: const FormData( [ - FormEntry( + FormInput( 'field', Name.pure(), ), @@ -100,7 +100,7 @@ void main() { const SignUpState( data: FormData( [ - FormEntry( + FormInput( 'field', Name.pure(), ),