feature(registration): refactor

This commit is contained in:
mso7nueh
2024-07-14 16:30:10 +03:00
parent 12a0db5093
commit 3cdab9e1bb
31 changed files with 1214 additions and 689 deletions

View File

@@ -4,7 +4,7 @@ import 'package:eatvio_app/features/registration/presentation/bloc/registration_
import 'package:eatvio_app/presentation/registration/language_select_screen/models/language_select_model.dart';
class RegistrationCubit extends Cubit<RegistrationState> {
RegistrationCubit() : super(RegistrationState.loaded(sheet: LanguageSelectModel().userRegistrationSheet));
RegistrationCubit() : super(RegistrationState(sheet: LanguageSelectModel().userRegistrationSheet));
updateSheet(UserRegistrationSheet sheet) => emit(RegistrationState.loaded(sheet: sheet));
updateSheet(UserRegistrationSheet sheet) => emit(RegistrationState(sheet: sheet));
}

View File

@@ -4,10 +4,8 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'registration_state.freezed.dart';
@freezed
abstract class RegistrationState with _$RegistrationState {
const factory RegistrationState() = _RegistrationState;
const factory RegistrationState.loaded({
class RegistrationState with _$RegistrationState {
const factory RegistrationState({
required UserRegistrationSheet sheet,
}) = RegistrationStateLoaded;
}) = _RegistrationState;
}

View File

@@ -16,43 +16,10 @@ final _privateConstructorUsedError = UnsupportedError(
/// @nodoc
mixin _$RegistrationState {
@optionalTypeArgs
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function(UserRegistrationSheet sheet) loaded,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function(UserRegistrationSheet sheet)? loaded,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function(UserRegistrationSheet sheet)? loaded,
required TResult orElse(),
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult map<TResult extends Object?>(
TResult Function(_RegistrationState value) $default, {
required TResult Function(RegistrationStateLoaded value) loaded,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>(
TResult? Function(_RegistrationState value)? $default, {
TResult? Function(RegistrationStateLoaded value)? loaded,
}) =>
throw _privateConstructorUsedError;
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>(
TResult Function(_RegistrationState value)? $default, {
TResult Function(RegistrationStateLoaded value)? loaded,
required TResult orElse(),
}) =>
UserRegistrationSheet get sheet => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$RegistrationStateCopyWith<RegistrationState> get copyWith =>
throw _privateConstructorUsedError;
}
@@ -61,6 +28,10 @@ abstract class $RegistrationStateCopyWith<$Res> {
factory $RegistrationStateCopyWith(
RegistrationState value, $Res Function(RegistrationState) then) =
_$RegistrationStateCopyWithImpl<$Res, RegistrationState>;
@useResult
$Res call({UserRegistrationSheet sheet});
$UserRegistrationSheetCopyWith<$Res> get sheet;
}
/// @nodoc
@@ -72,13 +43,41 @@ class _$RegistrationStateCopyWithImpl<$Res, $Val extends RegistrationState>
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? sheet = null,
}) {
return _then(_value.copyWith(
sheet: null == sheet
? _value.sheet
: sheet // ignore: cast_nullable_to_non_nullable
as UserRegistrationSheet,
) as $Val);
}
@override
@pragma('vm:prefer-inline')
$UserRegistrationSheetCopyWith<$Res> get sheet {
return $UserRegistrationSheetCopyWith<$Res>(_value.sheet, (value) {
return _then(_value.copyWith(sheet: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$RegistrationStateImplCopyWith<$Res> {
abstract class _$$RegistrationStateImplCopyWith<$Res>
implements $RegistrationStateCopyWith<$Res> {
factory _$$RegistrationStateImplCopyWith(_$RegistrationStateImpl value,
$Res Function(_$RegistrationStateImpl) then) =
__$$RegistrationStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({UserRegistrationSheet sheet});
@override
$UserRegistrationSheetCopyWith<$Res> get sheet;
}
/// @nodoc
@@ -88,119 +87,13 @@ class __$$RegistrationStateImplCopyWithImpl<$Res>
__$$RegistrationStateImplCopyWithImpl(_$RegistrationStateImpl _value,
$Res Function(_$RegistrationStateImpl) _then)
: super(_value, _then);
}
/// @nodoc
class _$RegistrationStateImpl implements _RegistrationState {
const _$RegistrationStateImpl();
@override
String toString() {
return 'RegistrationState()';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType && other is _$RegistrationStateImpl);
}
@override
int get hashCode => runtimeType.hashCode;
@override
@optionalTypeArgs
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function(UserRegistrationSheet sheet) loaded,
}) {
return $default();
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function(UserRegistrationSheet sheet)? loaded,
}) {
return $default?.call();
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function(UserRegistrationSheet sheet)? loaded,
required TResult orElse(),
}) {
if ($default != null) {
return $default();
}
return orElse();
}
@override
@optionalTypeArgs
TResult map<TResult extends Object?>(
TResult Function(_RegistrationState value) $default, {
required TResult Function(RegistrationStateLoaded value) loaded,
}) {
return $default(this);
}
@override
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>(
TResult? Function(_RegistrationState value)? $default, {
TResult? Function(RegistrationStateLoaded value)? loaded,
}) {
return $default?.call(this);
}
@override
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>(
TResult Function(_RegistrationState value)? $default, {
TResult Function(RegistrationStateLoaded value)? loaded,
required TResult orElse(),
}) {
if ($default != null) {
return $default(this);
}
return orElse();
}
}
abstract class _RegistrationState implements RegistrationState {
const factory _RegistrationState() = _$RegistrationStateImpl;
}
/// @nodoc
abstract class _$$RegistrationStateLoadedImplCopyWith<$Res> {
factory _$$RegistrationStateLoadedImplCopyWith(
_$RegistrationStateLoadedImpl value,
$Res Function(_$RegistrationStateLoadedImpl) then) =
__$$RegistrationStateLoadedImplCopyWithImpl<$Res>;
@useResult
$Res call({UserRegistrationSheet sheet});
}
/// @nodoc
class __$$RegistrationStateLoadedImplCopyWithImpl<$Res>
extends _$RegistrationStateCopyWithImpl<$Res, _$RegistrationStateLoadedImpl>
implements _$$RegistrationStateLoadedImplCopyWith<$Res> {
__$$RegistrationStateLoadedImplCopyWithImpl(
_$RegistrationStateLoadedImpl _value,
$Res Function(_$RegistrationStateLoadedImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? sheet = null,
}) {
return _then(_$RegistrationStateLoadedImpl(
return _then(_$RegistrationStateImpl(
sheet: null == sheet
? _value.sheet
: sheet // ignore: cast_nullable_to_non_nullable
@@ -211,22 +104,22 @@ class __$$RegistrationStateLoadedImplCopyWithImpl<$Res>
/// @nodoc
class _$RegistrationStateLoadedImpl implements RegistrationStateLoaded {
const _$RegistrationStateLoadedImpl({required this.sheet});
class _$RegistrationStateImpl implements _RegistrationState {
const _$RegistrationStateImpl({required this.sheet});
@override
final UserRegistrationSheet sheet;
@override
String toString() {
return 'RegistrationState.loaded(sheet: $sheet)';
return 'RegistrationState(sheet: $sheet)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$RegistrationStateLoadedImpl &&
other is _$RegistrationStateImpl &&
(identical(other.sheet, sheet) || other.sheet == sheet));
}
@@ -236,80 +129,19 @@ class _$RegistrationStateLoadedImpl implements RegistrationStateLoaded {
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$RegistrationStateLoadedImplCopyWith<_$RegistrationStateLoadedImpl>
get copyWith => __$$RegistrationStateLoadedImplCopyWithImpl<
_$RegistrationStateLoadedImpl>(this, _$identity);
@override
@optionalTypeArgs
TResult when<TResult extends Object?>(
TResult Function() $default, {
required TResult Function(UserRegistrationSheet sheet) loaded,
}) {
return loaded(sheet);
}
@override
@optionalTypeArgs
TResult? whenOrNull<TResult extends Object?>(
TResult? Function()? $default, {
TResult? Function(UserRegistrationSheet sheet)? loaded,
}) {
return loaded?.call(sheet);
}
@override
@optionalTypeArgs
TResult maybeWhen<TResult extends Object?>(
TResult Function()? $default, {
TResult Function(UserRegistrationSheet sheet)? loaded,
required TResult orElse(),
}) {
if (loaded != null) {
return loaded(sheet);
}
return orElse();
}
@override
@optionalTypeArgs
TResult map<TResult extends Object?>(
TResult Function(_RegistrationState value) $default, {
required TResult Function(RegistrationStateLoaded value) loaded,
}) {
return loaded(this);
}
@override
@optionalTypeArgs
TResult? mapOrNull<TResult extends Object?>(
TResult? Function(_RegistrationState value)? $default, {
TResult? Function(RegistrationStateLoaded value)? loaded,
}) {
return loaded?.call(this);
}
@override
@optionalTypeArgs
TResult maybeMap<TResult extends Object?>(
TResult Function(_RegistrationState value)? $default, {
TResult Function(RegistrationStateLoaded value)? loaded,
required TResult orElse(),
}) {
if (loaded != null) {
return loaded(this);
}
return orElse();
}
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith =>
__$$RegistrationStateImplCopyWithImpl<_$RegistrationStateImpl>(
this, _$identity);
}
abstract class RegistrationStateLoaded implements RegistrationState {
const factory RegistrationStateLoaded(
{required final UserRegistrationSheet sheet}) =
_$RegistrationStateLoadedImpl;
abstract class _RegistrationState implements RegistrationState {
const factory _RegistrationState(
{required final UserRegistrationSheet sheet}) = _$RegistrationStateImpl;
@override
UserRegistrationSheet get sheet;
@override
@JsonKey(ignore: true)
_$$RegistrationStateLoadedImplCopyWith<_$RegistrationStateLoadedImpl>
get copyWith => throw _privateConstructorUsedError;
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -20,6 +20,7 @@ import 'package:eatvio_app/widgets/app_bar/appbar_title_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:validators/validators.dart';
class RegistrationScreen extends StatefulWidget {
const RegistrationScreen({super.key});
@@ -64,7 +65,6 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
create: (BuildContext context) => RegistrationCubit(),
child: BlocBuilder<RegistrationCubit, RegistrationState>(
builder: (BuildContext context, RegistrationState state) {
(state as RegistrationStateLoaded);
return PopScope(
canPop: false,
onPopInvoked: (_) => _showPopDialog(),
@@ -112,12 +112,14 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
),
backgroundColor: appTheme.blueA400,
onPressed: () {
setState(() {
pageController.nextPage(
duration: const Duration(milliseconds: 350),
curve: Curves.easeInOut,
);
});
if (canGoNext(state)) {
setState(() {
pageController.nextPage(
duration: const Duration(milliseconds: 350),
curve: Curves.easeInOut,
);
});
}
},
label: Text(
'Продолжить',
@@ -169,21 +171,21 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
});
},
children: [
LanguageSelectScreen.builder(context),
CredentialsScreen.builder(context, state.sheet),
NameScreen.builder(context, state.sheet),
GenderScreen.builder(context, state.sheet),
DateOfBirthScreen.builder(context, state.sheet),
CityOfLivingScreen.builder(context, state.sheet),
BodyCircumferencesScreen.builder(context, state.sheet),
WeightScreen.builder(context, state.sheet),
HeightScreen.builder(context, state.sheet),
BloodPressureScreen.builder(context, state.sheet),
AllergiesScreen.builder(context, state.sheet),
MedicineIntakeScreen.builder(context, state.sheet),
FavoriteSportsScreen.builder(context, state.sheet),
FavoriteFoodsScreen.builder(context, state.sheet),
AdditionalInformationScreen.builder(context, state.sheet),
LanguageSelectScreen(sheet: context.read<RegistrationCubit>().state.sheet),
CredentialsScreen(sheet: context.read<RegistrationCubit>().state.sheet),
NameScreen(),
GenderScreen(),
DateOfBirthScreen(),
CityOfLivingScreen(),
BodyCircumferencesScreen(),
WeightScreen(),
HeightScreen(),
BloodPressureScreen(),
AllergiesScreen(),
MedicineIntakeScreen(),
FavoriteSportsScreen(),
FavoriteFoodsScreen(),
AdditionalInformationScreen(),
],
),
),
@@ -192,4 +194,71 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
),
);
}
bool canGoNext(state) {
switch (step) {
case 2:
List<String> errors = validateFields(state.sheet.email, state.sheet.name, state.sheet.password, state.sheet.passwordConfirmation);
if (errors.isEmpty) {
return true;
} else {
String errorMessage = '';
for (var error in errors) {
errorMessage += error + '\n\n';
}
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Ошибка'),
content: Text(errorMessage),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'),
),
],
),
);
return false;
}
default:
return true;
}
}
List<String> validateFields(String email, String name, String password, String passwordConfirmation) {
List<String> errors = [];
if (!isEmail(email)) {
errors.add('email_invalid'.tr);
}
if (email.isEmpty) {
errors.add('email_empty'.tr);
}
if (name.length < 6) {
errors.add('name_short'.tr);
}
if (name.isEmpty) {
errors.add('name_empty'.tr);
}
if (password.isEmpty) {
errors.add('name_empty'.tr);
}
if (password.length < 6) {
errors.add('password_short'.tr);
}
if (password != passwordConfirmation) {
errors.add('password_confirmation_mismatch'.tr);
}
return errors;
}
}