sign_up_bloc.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'package:app_flowy/user/domain/i_auth.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart';
  4. import 'package:freezed_annotation/freezed_annotation.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. part 'sign_up_bloc.freezed.dart';
  7. class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
  8. final IAuth authImpl;
  9. SignUpBloc(this.authImpl) : super(SignUpState.initial());
  10. @override
  11. Stream<SignUpState> mapEventToState(
  12. SignUpEvent event,
  13. ) async* {
  14. yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
  15. yield* _performActionOnSignUp();
  16. }, emailChanged: (EmailChanged value) async* {
  17. yield state.copyWith(
  18. email: value.email, emailError: none(), successOrFail: none());
  19. }, passwordChanged: (PasswordChanged value) async* {
  20. yield state.copyWith(
  21. password: value.password,
  22. passwordError: none(),
  23. successOrFail: none());
  24. }, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
  25. yield state.copyWith(
  26. repeatedPassword: value.password,
  27. repeatPasswordError: none(),
  28. successOrFail: none());
  29. });
  30. }
  31. Stream<SignUpState> _performActionOnSignUp() async* {
  32. yield state.copyWith(
  33. isSubmitting: true,
  34. successOrFail: none(),
  35. );
  36. final password = state.password;
  37. final repeatedPassword = state.repeatedPassword;
  38. if (password == null) {
  39. yield state.copyWith(
  40. isSubmitting: false,
  41. passwordError: some("Password can't be empty"),
  42. );
  43. return;
  44. }
  45. if (repeatedPassword == null) {
  46. yield state.copyWith(
  47. isSubmitting: false,
  48. repeatPasswordError: some("Repeat password can't be empty"),
  49. );
  50. return;
  51. }
  52. if (password != repeatedPassword) {
  53. yield state.copyWith(
  54. isSubmitting: false,
  55. repeatPasswordError:
  56. some("Repeat password is not the same as password"),
  57. );
  58. return;
  59. }
  60. yield state.copyWith(
  61. passwordError: none(),
  62. repeatPasswordError: none(),
  63. );
  64. final result =
  65. await authImpl.signUp(state.email, state.password, state.email);
  66. yield result.fold(
  67. (userProfile) => state.copyWith(
  68. isSubmitting: false,
  69. successOrFail: some(left(userProfile)),
  70. emailError: none(),
  71. passwordError: none(),
  72. repeatPasswordError: none(),
  73. ),
  74. (error) => stateFromCode(error),
  75. );
  76. }
  77. SignUpState stateFromCode(UserError error) {
  78. switch (error.code) {
  79. case ErrorCode.EmailFormatInvalid:
  80. return state.copyWith(
  81. isSubmitting: false,
  82. emailError: some(error.msg),
  83. passwordError: none(),
  84. successOrFail: none(),
  85. );
  86. case ErrorCode.PasswordFormatInvalid:
  87. return state.copyWith(
  88. isSubmitting: false,
  89. passwordError: some(error.msg),
  90. emailError: none(),
  91. successOrFail: none(),
  92. );
  93. default:
  94. return state.copyWith(
  95. isSubmitting: false, successOrFail: some(right(error)));
  96. }
  97. }
  98. }
  99. @freezed
  100. class SignUpEvent with _$SignUpEvent {
  101. const factory SignUpEvent.signUpWithUserEmailAndPassword() =
  102. SignUpWithUserEmailAndPassword;
  103. const factory SignUpEvent.emailChanged(String email) = EmailChanged;
  104. const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
  105. const factory SignUpEvent.repeatPasswordChanged(String password) =
  106. RepeatPasswordChanged;
  107. }
  108. @freezed
  109. class SignUpState with _$SignUpState {
  110. const factory SignUpState({
  111. String? email,
  112. String? password,
  113. String? repeatedPassword,
  114. required bool isSubmitting,
  115. required Option<String> passwordError,
  116. required Option<String> repeatPasswordError,
  117. required Option<String> emailError,
  118. required Option<Either<UserProfile, UserError>> successOrFail,
  119. }) = _SignUpState;
  120. factory SignUpState.initial() => SignUpState(
  121. isSubmitting: false,
  122. passwordError: none(),
  123. repeatPasswordError: none(),
  124. emailError: none(),
  125. successOrFail: none(),
  126. );
  127. }