sign_up_bloc.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. @override
  14. Stream<SignUpState> mapEventToState(
  15. SignUpEvent event,
  16. ) async* {
  17. yield* event.map(signUpWithUserEmailAndPassword: (e) async* {
  18. yield* _performActionOnSignUp();
  19. }, emailChanged: (EmailChanged value) async* {
  20. yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
  21. }, passwordChanged: (PasswordChanged value) async* {
  22. yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
  23. }, repeatPasswordChanged: (RepeatPasswordChanged value) async* {
  24. yield state.copyWith(repeatedPassword: value.password, repeatPasswordError: none(), successOrFail: none());
  25. });
  26. }
  27. Stream<SignUpState> _performActionOnSignUp() async* {
  28. yield state.copyWith(
  29. isSubmitting: true,
  30. successOrFail: none(),
  31. );
  32. final password = state.password;
  33. final repeatedPassword = state.repeatedPassword;
  34. if (password == null) {
  35. yield state.copyWith(
  36. isSubmitting: false,
  37. passwordError: some(LocaleKeys.signUp_emptyPasswordError.tr()),
  38. );
  39. return;
  40. }
  41. if (repeatedPassword == null) {
  42. yield state.copyWith(
  43. isSubmitting: false,
  44. repeatPasswordError: some(LocaleKeys.signUp_repeatPasswordEmptyError.tr()),
  45. );
  46. return;
  47. }
  48. if (password != repeatedPassword) {
  49. yield state.copyWith(
  50. isSubmitting: false,
  51. repeatPasswordError: some(LocaleKeys.signUp_unmatchedPasswordError.tr()),
  52. );
  53. return;
  54. }
  55. yield state.copyWith(
  56. passwordError: none(),
  57. repeatPasswordError: none(),
  58. );
  59. final result = await authManager.signUp(state.email, state.password, state.email);
  60. yield result.fold(
  61. (profile) => state.copyWith(
  62. isSubmitting: false,
  63. successOrFail: some(left(profile)),
  64. emailError: none(),
  65. passwordError: none(),
  66. repeatPasswordError: none(),
  67. ),
  68. (error) => stateFromCode(error),
  69. );
  70. }
  71. SignUpState stateFromCode(FlowyError error) {
  72. switch (ErrorCode.valueOf(error.code)!) {
  73. case ErrorCode.EmailFormatInvalid:
  74. return state.copyWith(
  75. isSubmitting: false,
  76. emailError: some(error.msg),
  77. passwordError: none(),
  78. successOrFail: none(),
  79. );
  80. case ErrorCode.PasswordFormatInvalid:
  81. return state.copyWith(
  82. isSubmitting: false,
  83. passwordError: some(error.msg),
  84. emailError: none(),
  85. successOrFail: none(),
  86. );
  87. default:
  88. return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
  89. }
  90. }
  91. }
  92. @freezed
  93. class SignUpEvent with _$SignUpEvent {
  94. const factory SignUpEvent.signUpWithUserEmailAndPassword() = SignUpWithUserEmailAndPassword;
  95. const factory SignUpEvent.emailChanged(String email) = EmailChanged;
  96. const factory SignUpEvent.passwordChanged(String password) = PasswordChanged;
  97. const factory SignUpEvent.repeatPasswordChanged(String password) = RepeatPasswordChanged;
  98. }
  99. @freezed
  100. class SignUpState with _$SignUpState {
  101. const factory SignUpState({
  102. String? email,
  103. String? password,
  104. String? repeatedPassword,
  105. required bool isSubmitting,
  106. required Option<String> passwordError,
  107. required Option<String> repeatPasswordError,
  108. required Option<String> emailError,
  109. required Option<Either<UserProfile, FlowyError>> successOrFail,
  110. }) = _SignUpState;
  111. factory SignUpState.initial() => SignUpState(
  112. isSubmitting: false,
  113. passwordError: none(),
  114. repeatPasswordError: none(),
  115. emailError: none(),
  116. successOrFail: none(),
  117. );
  118. }