sign_up_bloc.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'package:app_flowy/user/application/auth_service.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:easy_localization/easy_localization.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-error-code/code.pb.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile;
  6. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  7. import 'package:freezed_annotation/freezed_annotation.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:app_flowy/generated/locale_keys.g.dart';
  10. part 'sign_up_bloc.freezed.dart';
  11. class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
  12. final AuthService authService;
  13. SignUpBloc(this.authService) : super(SignUpState.initial()) {
  14. on<SignUpEvent>((event, emit) async {
  15. await event.map(signUpWithUserEmailAndPassword: (e) async {
  16. await _performActionOnSignUp(emit);
  17. }, emailChanged: (_EmailChanged value) async {
  18. emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none()));
  19. }, passwordChanged: (_PasswordChanged value) async {
  20. emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none()));
  21. }, repeatPasswordChanged: (_RepeatPasswordChanged value) async {
  22. emit(state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none()));
  23. });
  24. });
  25. }
  26. Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async {
  27. emit(state.copyWith(
  28. isSubmitting: true,
  29. successOrFail: none(),
  30. ));
  31. final password = state.password;
  32. final repeatedPassword = state.repeatedPassword;
  33. if (password == null) {
  34. emit(state.copyWith(
  35. isSubmitting: false,
  36. passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
  37. ));
  38. return;
  39. }
  40. if (repeatedPassword == null) {
  41. emit(state.copyWith(
  42. isSubmitting: false,
  43. repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
  44. ));
  45. return;
  46. }
  47. if (password != repeatedPassword) {
  48. emit(state.copyWith(
  49. isSubmitting: false,
  50. repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
  51. ));
  52. return;
  53. }
  54. emit(state.copyWith(
  55. passwordError: none(),
  56. repeatPasswordError: none(),
  57. ));
  58. final result = await authService.signUp(
  59. name: state.email,
  60. password: state.password,
  61. email: state.email,
  62. );
  63. emit(result.fold(
  64. (profile) => state.copyWith(
  65. isSubmitting: false,
  66. successOrFail: some(left(profile)),
  67. emailError: none(),
  68. passwordError: none(),
  69. repeatPasswordError: none(),
  70. ),
  71. (error) => stateFromCode(error),
  72. ));
  73. }
  74. SignUpState stateFromCode(FlowyError error) {
  75. switch (ErrorCode.valueOf(error.code)!) {
  76. case ErrorCode.EmailFormatInvalid:
  77. return state.copyWith(
  78. isSubmitting: false,
  79. emailError: some(error.msg),
  80. passwordError: none(),
  81. successOrFail: none(),
  82. );
  83. case ErrorCode.PasswordFormatInvalid:
  84. return state.copyWith(
  85. isSubmitting: false,
  86. passwordError: some(error.msg),
  87. emailError: none(),
  88. successOrFail: none(),
  89. );
  90. default:
  91. return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
  92. }
  93. }
  94. }
  95. @freezed
  96. class SignUpEvent with _$SignUpEvent {
  97. const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
  98. const factory SignUpEvent.emailChanged(String email) = _EmailChanged;
  99. const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged;
  100. const factory SignUpEvent.repeatPasswordChanged(String password) = _RepeatPasswordChanged;
  101. }
  102. @freezed
  103. class SignUpState with _$SignUpState {
  104. const factory SignUpState({
  105. String? email,
  106. String? password,
  107. String? repeatedPassword,
  108. required bool isSubmitting,
  109. required Option<String> passwordError,
  110. required Option<String> repeatPasswordError,
  111. required Option<String> emailError,
  112. required Option<Either<UserProfile, FlowyError>> successOrFail,
  113. }) = _SignUpState;
  114. factory SignUpState.initial() => SignUpState(
  115. isSubmitting: false,
  116. passwordError: none(),
  117. repeatPasswordError: none(),
  118. emailError: none(),
  119. successOrFail: none(),
  120. );
  121. }