sign_up_bloc.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.pb.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfilePB;
  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(
  19. email: value.email, emailError: none(), successOrFail: none()));
  20. }, passwordChanged: (_PasswordChanged value) async {
  21. emit(state.copyWith(
  22. password: value.password,
  23. passwordError: none(),
  24. successOrFail: none()));
  25. }, repeatPasswordChanged: (_RepeatPasswordChanged value) async {
  26. emit(state.copyWith(
  27. repeatedPassword: value.password,
  28. repeatPasswordError: none(),
  29. successOrFail: none()));
  30. });
  31. });
  32. }
  33. Future<void> _performActionOnSignUp(Emitter<SignUpState> emit) async {
  34. emit(state.copyWith(
  35. isSubmitting: true,
  36. successOrFail: none(),
  37. ));
  38. final password = state.password;
  39. final repeatedPassword = state.repeatedPassword;
  40. if (password == null) {
  41. emit(state.copyWith(
  42. isSubmitting: false,
  43. passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
  44. ));
  45. return;
  46. }
  47. if (repeatedPassword == null) {
  48. emit(state.copyWith(
  49. isSubmitting: false,
  50. repeatPasswordError:
  51. some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
  52. ));
  53. return;
  54. }
  55. if (password != repeatedPassword) {
  56. emit(state.copyWith(
  57. isSubmitting: false,
  58. repeatPasswordError:
  59. some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
  60. ));
  61. return;
  62. }
  63. emit(state.copyWith(
  64. passwordError: none(),
  65. repeatPasswordError: none(),
  66. ));
  67. final result = await authService.signUp(
  68. name: state.email,
  69. password: state.password,
  70. email: state.email,
  71. );
  72. emit(result.fold(
  73. (profile) => state.copyWith(
  74. isSubmitting: false,
  75. successOrFail: some(left(profile)),
  76. emailError: none(),
  77. passwordError: none(),
  78. repeatPasswordError: none(),
  79. ),
  80. (error) => stateFromCode(error),
  81. ));
  82. }
  83. SignUpState stateFromCode(FlowyError error) {
  84. switch (ErrorCode.valueOf(error.code)!) {
  85. case ErrorCode.EmailFormatInvalid:
  86. return state.copyWith(
  87. isSubmitting: false,
  88. emailError: some(error.msg),
  89. passwordError: none(),
  90. successOrFail: none(),
  91. );
  92. case ErrorCode.PasswordFormatInvalid:
  93. return state.copyWith(
  94. isSubmitting: false,
  95. passwordError: some(error.msg),
  96. emailError: none(),
  97. successOrFail: none(),
  98. );
  99. default:
  100. return state.copyWith(
  101. isSubmitting: false, successOrFail: some(right(error)));
  102. }
  103. }
  104. }
  105. @freezed
  106. class SignUpEvent with _$SignUpEvent {
  107. const factory SignUpEvent.signUpWithUserEmailAndPassword() =
  108. SignUpWithUserEmailAndPassword;
  109. const factory SignUpEvent.emailChanged(String email) = _EmailChanged;
  110. const factory SignUpEvent.passwordChanged(String password) = _PasswordChanged;
  111. const factory SignUpEvent.repeatPasswordChanged(String password) =
  112. _RepeatPasswordChanged;
  113. }
  114. @freezed
  115. class SignUpState with _$SignUpState {
  116. const factory SignUpState({
  117. String? email,
  118. String? password,
  119. String? repeatedPassword,
  120. required bool isSubmitting,
  121. required Option<String> passwordError,
  122. required Option<String> repeatPasswordError,
  123. required Option<String> emailError,
  124. required Option<Either<UserProfilePB, FlowyError>> successOrFail,
  125. }) = _SignUpState;
  126. factory SignUpState.initial() => SignUpState(
  127. isSubmitting: false,
  128. passwordError: none(),
  129. repeatPasswordError: none(),
  130. emailError: none(),
  131. successOrFail: none(),
  132. );
  133. }