sign_up_bloc.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import 'package:app_flowy/user/domain/i_auth.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-user-infra/errors.pb.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart';
  5. import 'package:freezed_annotation/freezed_annotation.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. part 'sign_up_bloc.freezed.dart';
  8. class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
  9. final IAuth authManager;
  10. SignUpBloc(this.authManager) : super(SignUpState.initial());
  11. @override
  12. Stream<SignUpState> mapEventToState(
  13. SignUpEvent event,
  14. ) async* {
  15. yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
  16. yield* _performActionOnSignUp();
  17. }, emailChanged: (EmailChanged value) async* {
  18. yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
  19. }, passwordChanged: (PasswordChanged value) async* {
  20. yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
  21. }, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
  22. yield state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none());
  23. });
  24. }
  25. Stream<SignUpState> _performActionOnSignUp() async* {
  26. yield state.copyWith(
  27. isSubmitting: true,
  28. successOrFail: none(),
  29. );
  30. final password = state.password;
  31. final repeatedPassword = state.repeatedPassword;
  32. if (password == null) {
  33. yield state.copyWith(
  34. isSubmitting: false,
  35. passwordError: some("Password can't be empty"),
  36. );
  37. return;
  38. }
  39. if (repeatedPassword == null) {
  40. yield state.copyWith(
  41. isSubmitting: false,
  42. repeatPasswordError: some("Repeat password can't be empty"),
  43. );
  44. return;
  45. }
  46. if (password != repeatedPassword) {
  47. yield state.copyWith(
  48. isSubmitting: false,
  49. repeatPasswordError: some("Repeat password is not the same as password"),
  50. );
  51. return;
  52. }
  53. yield state.copyWith(
  54. passwordError: none(),
  55. repeatPasswordError: none(),
  56. );
  57. final result = await authManager.signUp(state.email, state.password, state.email);
  58. yield result.fold(
  59. (newUser) => state.copyWith(
  60. isSubmitting: false,
  61. successOrFail: some(left(newUser.profile)),
  62. emailError: none(),
  63. passwordError: none(),
  64. repeatPasswordError: none(),
  65. ),
  66. (error) => stateFromCode(error),
  67. );
  68. }
  69. SignUpState stateFromCode(UserError 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, UserError>> 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. }