| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | 
							- import 'package:app_flowy/user/domain/i_auth.dart';
 
- import 'package:dartz/dartz.dart';
 
- import 'package:easy_localization/easy_localization.dart';
 
- import 'package:flowy_sdk/protobuf/flowy-user-infra/protobuf.dart' show UserProfile, ErrorCode;
 
- import 'package:flowy_sdk/protobuf/flowy-user/errors.pb.dart';
 
- import 'package:freezed_annotation/freezed_annotation.dart';
 
- import 'package:flutter_bloc/flutter_bloc.dart';
 
- import 'package:app_flowy/generated/locale_keys.g.dart';
 
- part 'sign_up_bloc.freezed.dart';
 
- class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
 
-   final IAuth authManager;
 
-   SignUpBloc(this.authManager) : super(SignUpState.initial());
 
-   @override
 
-   Stream<SignUpState> mapEventToState(
 
-     SignUpEvent event,
 
-   ) async* {
 
-     yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
 
-       yield* _performActionOnSignUp();
 
-     }, emailChanged: (EmailChanged value) async* {
 
-       yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
 
-     }, passwordChanged: (PasswordChanged value) async* {
 
-       yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
 
-     }, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
 
-       yield state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none());
 
-     });
 
-   }
 
-   Stream<SignUpState> _performActionOnSignUp() async* {
 
-     yield state.copyWith(
 
-       isSubmitting: true,
 
-       successOrFail: none(),
 
-     );
 
-     final password = state.password;
 
-     final repeatedPassword = state.repeatedPassword;
 
-     if (password == null) {
 
-       yield state.copyWith(
 
-         isSubmitting: false,
 
-         passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
 
-       );
 
-       return;
 
-     }
 
-     if (repeatedPassword == null) {
 
-       yield state.copyWith(
 
-         isSubmitting: false,
 
-         repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
 
-       );
 
-       return;
 
-     }
 
-     if (password != repeatedPassword) {
 
-       yield state.copyWith(
 
-         isSubmitting: false,
 
-         repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
 
-       );
 
-       return;
 
-     }
 
-     yield state.copyWith(
 
-       passwordError: none(),
 
-       repeatPasswordError: none(),
 
-     );
 
-     final result = await authManager.signUp(state.email, state.password, state.email);
 
-     yield result.fold(
 
-       (profile) => state.copyWith(
 
-         isSubmitting: false,
 
-         successOrFail: some(left(profile)),
 
-         emailError: none(),
 
-         passwordError: none(),
 
-         repeatPasswordError: none(),
 
-       ),
 
-       (error) => stateFromCode(error),
 
-     );
 
-   }
 
-   SignUpState stateFromCode(UserError error) {
 
-     switch (ErrorCode.valueOf(error.code)!) {
 
-       case ErrorCode.EmailFormatInvalid:
 
-         return state.copyWith(
 
-           isSubmitting: false,
 
-           emailError: some(error.msg),
 
-           passwordError: none(),
 
-           successOrFail: none(),
 
-         );
 
-       case ErrorCode.PasswordFormatInvalid:
 
-         return state.copyWith(
 
-           isSubmitting: false,
 
-           passwordError: some(error.msg),
 
-           emailError: none(),
 
-           successOrFail: none(),
 
-         );
 
-       default:
 
-         return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
 
-     }
 
-   }
 
- }
 
- @freezed
 
- class SignUpEvent with _$SignUpEvent {
 
-   const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
 
-   const factory SignUpEvent.emailChanged(String email) = EmailChanged;
 
-   const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
 
-   const factory SignUpEvent.repeatPasswordChanged(String password) = RepeatPasswordChanged;
 
- }
 
- @freezed
 
- class SignUpState with _$SignUpState {
 
-   const factory SignUpState({
 
-     String? email,
 
-     String? password,
 
-     String? repeatedPassword,
 
-     required bool isSubmitting,
 
-     required Option<String> passwordError,
 
-     required Option<String> repeatPasswordError,
 
-     required Option<String> emailError,
 
-     required Option<Either<UserProfile, UserError>> successOrFail,
 
-   }) = _SignUpState;
 
-   factory SignUpState.initial() => SignUpState(
 
-         isSubmitting: false,
 
-         passwordError: none(),
 
-         repeatPasswordError: none(),
 
-         emailError: none(),
 
-         successOrFail: none(),
 
-       );
 
- }
 
 
  |