sign_up_bloc.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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(email: value.email, successOrFail: none());
  18. }, passwordChanged: (PasswordChanged value) async* {
  19. yield state.copyWith(password: value.password, successOrFail: none());
  20. }, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
  21. yield state.copyWith(
  22. repeatedPassword: value.password, successOrFail: none());
  23. });
  24. }
  25. Stream<SignUpState> _performActionOnSignUp() async* {
  26. yield state.copyWith(
  27. isSubmitting: true,
  28. );
  29. final password = state.password;
  30. final repeatedPassword = state.repeatedPassword;
  31. if (password == null) {
  32. yield state.copyWith(
  33. isSubmitting: false,
  34. passwordError: some("Password can't be empty"),
  35. );
  36. return;
  37. }
  38. if (repeatedPassword == null) {
  39. yield state.copyWith(
  40. isSubmitting: false,
  41. repeatPasswordError: some("Repeat password can't be empty"),
  42. );
  43. return;
  44. }
  45. if (password != repeatedPassword) {
  46. yield state.copyWith(
  47. isSubmitting: false,
  48. repeatPasswordError:
  49. 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 =
  58. await authImpl.signUp(state.email, state.password, state.email);
  59. yield result.fold(
  60. (userProfile) => state.copyWith(
  61. isSubmitting: false,
  62. successOrFail: some(left(userProfile)),
  63. emailError: none(),
  64. passwordError: none(),
  65. repeatPasswordError: none(),
  66. ),
  67. (error) => stateFromCode(error),
  68. );
  69. }
  70. SignUpState stateFromCode(UserError error) {
  71. switch (error.code) {
  72. case ErrorCode.EmailFormatInvalid:
  73. return state.copyWith(
  74. isSubmitting: false,
  75. emailError: some(error.msg),
  76. passwordError: none());
  77. case ErrorCode.PasswordFormatInvalid:
  78. return state.copyWith(
  79. isSubmitting: false,
  80. passwordError: some(error.msg),
  81. emailError: none());
  82. default:
  83. return state.copyWith(
  84. isSubmitting: false, successOrFail: some(right(error)));
  85. }
  86. }
  87. }
  88. @freezed
  89. class SignUpEvent with _$SignUpEvent {
  90. const factory SignUpEvent.signUpWithUserEmailAndPassword() =
  91. SignUpWithUserEmailAndPassword;
  92. const factory SignUpEvent.emailChanged(String email) = EmailChanged;
  93. const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
  94. const factory SignUpEvent.repeatPasswordChanged(String password) =
  95. 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. }