| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  | import 'package:appflowy/generated/locale_keys.g.dart'; | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  | import 'package:appflowy/user/application/auth/auth_service.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-error/code.pb.dart'; | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-01-08 12:10:53 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart' | 
					
						
							|  |  |  |     show UserProfilePB; | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  | import 'package:appflowy_result/appflowy_result.dart'; | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  | import 'package:easy_localization/easy_localization.dart'; | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  | import 'package:flutter_bloc/flutter_bloc.dart'; | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  | import 'package:freezed_annotation/freezed_annotation.dart'; | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 22:52:20 +08:00
										 |  |  | part 'sign_up_bloc.freezed.dart'; | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 22:52:20 +08:00
										 |  |  | class SignUpBloc extends Bloc<SignUpEvent, SignUpState> { | 
					
						
							| 
									
										
										
										
											2022-03-01 10:22:28 -05:00
										 |  |  |   SignUpBloc(this.authService) : super(SignUpState.initial()) { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |     _dispatch(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   final AuthService authService; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void _dispatch() { | 
					
						
							|  |  |  |     on<SignUpEvent>( | 
					
						
							|  |  |  |       (event, emit) async { | 
					
						
							|  |  |  |         await event.map( | 
					
						
							|  |  |  |           signUpWithUserEmailAndPassword: (e) async { | 
					
						
							|  |  |  |             await _performActionOnSignUp(emit); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           emailChanged: (_EmailChanged value) async { | 
					
						
							|  |  |  |             emit( | 
					
						
							|  |  |  |               state.copyWith( | 
					
						
							|  |  |  |                 email: value.email, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |                 emailError: null, | 
					
						
							|  |  |  |                 successOrFail: null, | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |               ), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           passwordChanged: (_PasswordChanged value) async { | 
					
						
							|  |  |  |             emit( | 
					
						
							|  |  |  |               state.copyWith( | 
					
						
							|  |  |  |                 password: value.password, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |                 passwordError: null, | 
					
						
							|  |  |  |                 successOrFail: null, | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |               ), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           repeatPasswordChanged: (_RepeatPasswordChanged value) async { | 
					
						
							|  |  |  |             emit( | 
					
						
							|  |  |  |               state.copyWith( | 
					
						
							|  |  |  |                 repeatedPassword: value.password, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |                 repeatPasswordError: null, | 
					
						
							|  |  |  |                 successOrFail: null, | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |               ), | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-04 22:44:03 +08:00
										 |  |  |   Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async { | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     emit( | 
					
						
							|  |  |  |       state.copyWith( | 
					
						
							|  |  |  |         isSubmitting: true, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         successOrFail: null, | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     final password = state.password; | 
					
						
							|  |  |  |     final repeatedPassword = state.repeatedPassword; | 
					
						
							|  |  |  |     if (password == null) { | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       emit( | 
					
						
							|  |  |  |         state.copyWith( | 
					
						
							|  |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           passwordError: LocaleKeys.signUp_emptyPasswordError.tr(), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |         ), | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (repeatedPassword == null) { | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       emit( | 
					
						
							|  |  |  |         state.copyWith( | 
					
						
							|  |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           repeatPasswordError: LocaleKeys.signUp_repeatPasswordEmptyError.tr(), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |         ), | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     if (password != repeatedPassword) { | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       emit( | 
					
						
							|  |  |  |         state.copyWith( | 
					
						
							|  |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           repeatPasswordError: LocaleKeys.signUp_unmatchedPasswordError.tr(), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |         ), | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     emit( | 
					
						
							|  |  |  |       state.copyWith( | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         passwordError: null, | 
					
						
							|  |  |  |         repeatPasswordError: null, | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |       ), | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 10:22:28 -05:00
										 |  |  |     final result = await authService.signUp( | 
					
						
							| 
									
										
										
										
											2023-05-21 18:53:59 +08:00
										 |  |  |       name: state.email ?? '', | 
					
						
							|  |  |  |       password: state.password ?? '', | 
					
						
							|  |  |  |       email: state.email ?? '', | 
					
						
							| 
									
										
										
										
											2022-01-31 08:15:49 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     emit( | 
					
						
							|  |  |  |       result.fold( | 
					
						
							|  |  |  |         (profile) => state.copyWith( | 
					
						
							|  |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           successOrFail: FlowyResult.success(profile), | 
					
						
							|  |  |  |           emailError: null, | 
					
						
							|  |  |  |           passwordError: null, | 
					
						
							|  |  |  |           repeatPasswordError: null, | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |         ), | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         (error) => stateFromCode(error), | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |       ), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-07-25 18:04:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-14 18:04:51 +08:00
										 |  |  |   SignUpState stateFromCode(FlowyError error) { | 
					
						
							| 
									
										
										
										
											2023-08-22 00:19:15 +08:00
										 |  |  |     switch (error.code) { | 
					
						
							| 
									
										
										
										
											2021-08-31 11:32:51 +08:00
										 |  |  |       case ErrorCode.EmailFormatInvalid: | 
					
						
							| 
									
										
										
										
											2021-07-25 18:04:16 +08:00
										 |  |  |         return state.copyWith( | 
					
						
							| 
									
										
										
										
											2021-10-09 16:43:56 +08:00
										 |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           emailError: error.msg, | 
					
						
							|  |  |  |           passwordError: null, | 
					
						
							|  |  |  |           successOrFail: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 16:43:56 +08:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-08-31 11:32:51 +08:00
										 |  |  |       case ErrorCode.PasswordFormatInvalid: | 
					
						
							| 
									
										
										
										
											2021-07-25 18:04:16 +08:00
										 |  |  |         return state.copyWith( | 
					
						
							| 
									
										
										
										
											2021-10-09 16:43:56 +08:00
										 |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           passwordError: error.msg, | 
					
						
							|  |  |  |           emailError: null, | 
					
						
							|  |  |  |           successOrFail: null, | 
					
						
							| 
									
										
										
										
											2021-10-09 16:43:56 +08:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-07-25 18:04:16 +08:00
										 |  |  |       default: | 
					
						
							| 
									
										
										
										
											2022-12-30 11:16:47 +08:00
										 |  |  |         return state.copyWith( | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |           isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |           successOrFail: FlowyResult.failure(error), | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-07-25 18:04:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-07-13 13:14:49 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							| 
									
										
										
										
											2021-10-09 10:09:31 +08:00
										 |  |  | class SignUpEvent with _$SignUpEvent { | 
					
						
							| 
									
										
										
										
											2022-12-30 11:16:47 +08:00
										 |  |  |   const factory SignUpEvent.signUpWithUserEmailAndPassword() = | 
					
						
							|  |  |  |       SignUpWithUserEmailAndPassword; | 
					
						
							| 
									
										
										
										
											2022-03-25 15:02:43 +08:00
										 |  |  |   const factory SignUpEvent.emailChanged(String email) = _EmailChanged; | 
					
						
							|  |  |  |   const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged; | 
					
						
							| 
									
										
										
										
											2022-12-30 11:16:47 +08:00
										 |  |  |   const factory SignUpEvent.repeatPasswordChanged(String password) = | 
					
						
							|  |  |  |       _RepeatPasswordChanged; | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @freezed | 
					
						
							| 
									
										
										
										
											2021-10-09 10:09:31 +08:00
										 |  |  | class SignUpState with _$SignUpState { | 
					
						
							| 
									
										
										
										
											2021-09-05 22:52:20 +08:00
										 |  |  |   const factory SignUpState({ | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  |     String? email, | 
					
						
							|  |  |  |     String? password, | 
					
						
							| 
									
										
										
										
											2021-09-06 16:18:34 +08:00
										 |  |  |     String? repeatedPassword, | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  |     required bool isSubmitting, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |     required String? passwordError, | 
					
						
							|  |  |  |     required String? repeatPasswordError, | 
					
						
							|  |  |  |     required String? emailError, | 
					
						
							|  |  |  |     required FlowyResult<UserProfilePB, FlowyError>? successOrFail, | 
					
						
							| 
									
										
										
										
											2021-09-05 22:52:20 +08:00
										 |  |  |   }) = _SignUpState; | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |   factory SignUpState.initial() => const SignUpState( | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  |         isSubmitting: false, | 
					
						
							| 
									
										
										
										
											2024-02-24 20:54:10 +07:00
										 |  |  |         passwordError: null, | 
					
						
							|  |  |  |         repeatPasswordError: null, | 
					
						
							|  |  |  |         emailError: null, | 
					
						
							|  |  |  |         successOrFail: null, | 
					
						
							| 
									
										
										
										
											2021-07-24 15:05:47 +08:00
										 |  |  |       ); | 
					
						
							|  |  |  | } |