sign_in_bloc.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'package:app_flowy/user/domain/i_auth.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  4. import 'package:flowy_sdk/protobuf/flowy-user-data-model/protobuf.dart' show UserProfile, ErrorCode;
  5. import 'package:freezed_annotation/freezed_annotation.dart';
  6. import 'package:flutter_bloc/flutter_bloc.dart';
  7. part 'sign_in_bloc.freezed.dart';
  8. class SignInBloc extends Bloc<SignInEvent, SignInState> {
  9. final IAuth authManager;
  10. SignInBloc(this.authManager) : super(SignInState.initial());
  11. @override
  12. Stream<SignInState> mapEventToState(
  13. SignInEvent event,
  14. ) async* {
  15. yield* event.map(
  16. signedInWithUserEmailAndPassword: (e) async* {
  17. yield* _performActionOnSignIn(
  18. state,
  19. );
  20. },
  21. emailChanged: (EmailChanged value) async* {
  22. yield state.copyWith(email: value.email, emailError: none(), successOrFail: none());
  23. },
  24. passwordChanged: (PasswordChanged value) async* {
  25. yield state.copyWith(password: value.password, passwordError: none(), successOrFail: none());
  26. },
  27. );
  28. }
  29. Stream<SignInState> _performActionOnSignIn(SignInState state) async* {
  30. yield state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none());
  31. final result = await authManager.signIn(state.email, state.password);
  32. yield result.fold(
  33. (userProfile) => state.copyWith(isSubmitting: false, successOrFail: some(left(userProfile))),
  34. (error) => stateFromCode(error),
  35. );
  36. }
  37. SignInState stateFromCode(FlowyError error) {
  38. switch (ErrorCode.valueOf(error.code)!) {
  39. case ErrorCode.EmailFormatInvalid:
  40. return state.copyWith(isSubmitting: false, emailError: some(error.msg), passwordError: none());
  41. case ErrorCode.PasswordFormatInvalid:
  42. return state.copyWith(isSubmitting: false, passwordError: some(error.msg), emailError: none());
  43. default:
  44. return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
  45. }
  46. }
  47. }
  48. @freezed
  49. abstract class SignInEvent with _$SignInEvent {
  50. const factory SignInEvent.signedInWithUserEmailAndPassword() = SignedInWithUserEmailAndPassword;
  51. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  52. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  53. }
  54. @freezed
  55. abstract class SignInState with _$SignInState {
  56. const factory SignInState({
  57. String? email,
  58. String? password,
  59. required bool isSubmitting,
  60. required Option<String> passwordError,
  61. required Option<String> emailError,
  62. required Option<Either<UserProfile, FlowyError>> successOrFail,
  63. }) = _SignInState;
  64. factory SignInState.initial() => SignInState(
  65. isSubmitting: false,
  66. passwordError: none(),
  67. emailError: none(),
  68. successOrFail: none(),
  69. );
  70. }