sign_up_bloc.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'package:app_flowy/user/domain/i_auth.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 IAuth authManager;
  12. SignUpBloc(this.authManager) : 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 authManager.signUp(state.email, state.password, state.email);
  58. emit(result.fold(
  59. (profile) => state.copyWith(
  60. isSubmitting: false,
  61. successOrFail: some(left(profile)),
  62. emailError: none(),
  63. passwordError: none(),
  64. repeatPasswordError: none(),
  65. ),
  66. (error) => stateFromCode(error),
  67. ));
  68. }
  69. SignUpState stateFromCode(FlowyError error) {
  70. switch (ErrorCode.valueOf(error.code)!) {
  71. case ErrorCode.EmailFormatInvalid:
  72. return state.copyWith(
  73. isSubmitting: false,
  74. emailError: some(error.msg),
  75. passwordError: none(),
  76. successOrFail: none(),
  77. );
  78. case ErrorCode.PasswordFormatInvalid:
  79. return state.copyWith(
  80. isSubmitting: false,
  81. passwordError: some(error.msg),
  82. emailError: none(),
  83. successOrFail: none(),
  84. );
  85. default:
  86. return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
  87. }
  88. }
  89. }
  90. @freezed
  91. class SignUpEvent with _$SignUpEvent {
  92. const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
  93. const factory SignUpEvent.emailChanged(String email) = EmailChanged;
  94. const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
  95. const factory SignUpEvent.repeatPasswordChanged(String password) = RepeatPasswordChanged;
  96. }
  97. @freezed
  98. class SignUpState with _$SignUpState {
  99. const factory SignUpState({
  100. String? email,
  101. String? password,
  102. String? repeatedPassword,
  103. required bool isSubmitting,
  104. required Option<String> passwordError,
  105. required Option<String> repeatPasswordError,
  106. required Option<String> emailError,
  107. required Option<Either<UserProfile, FlowyError>> successOrFail,
  108. }) = _SignUpState;
  109. factory SignUpState.initial() => SignUpState(
  110. isSubmitting: false,
  111. passwordError: none(),
  112. repeatPasswordError: none(),
  113. emailError: none(),
  114. successOrFail: none(),
  115. );
  116. }