sign_up_bloc.dart 4.3 KB

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