sign_in_bloc.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'package:app_flowy/user/application/auth_service.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 AuthService authService;
  10. SignInBloc(this.authService) : super(SignInState.initial()) {
  11. on<SignInEvent>((event, emit) async {
  12. await event.map(
  13. signedInWithUserEmailAndPassword: (e) async {
  14. await _performActionOnSignIn(
  15. state,
  16. emit,
  17. );
  18. },
  19. emailChanged: (EmailChanged value) async {
  20. emit(state.copyWith(email: value.email, emailError: none(), successOrFail: none()));
  21. },
  22. passwordChanged: (PasswordChanged value) async {
  23. emit(state.copyWith(password: value.password, passwordError: none(), successOrFail: none()));
  24. },
  25. );
  26. });
  27. }
  28. Future<void> _performActionOnSignIn(SignInState state, Emitter<SignInState> emit) async {
  29. emit(state.copyWith(isSubmitting: true, emailError: none(), passwordError: none(), successOrFail: none()));
  30. final result = await authService.signIn(
  31. email: state.email,
  32. password: state.password,
  33. );
  34. emit(result.fold(
  35. (userProfile) => state.copyWith(isSubmitting: false, successOrFail: some(left(userProfile))),
  36. (error) => stateFromCode(error),
  37. ));
  38. }
  39. SignInState stateFromCode(FlowyError error) {
  40. switch (ErrorCode.valueOf(error.code)!) {
  41. case ErrorCode.EmailFormatInvalid:
  42. return state.copyWith(isSubmitting: false, emailError: some(error.msg), passwordError: none());
  43. case ErrorCode.PasswordFormatInvalid:
  44. return state.copyWith(isSubmitting: false, passwordError: some(error.msg), emailError: none());
  45. default:
  46. return state.copyWith(isSubmitting: false, successOrFail: some(right(error)));
  47. }
  48. }
  49. }
  50. @freezed
  51. class SignInEvent with _$SignInEvent {
  52. const factory SignInEvent.signedInWithUserEmailAndPassword() = SignedInWithUserEmailAndPassword;
  53. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  54. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  55. }
  56. @freezed
  57. class SignInState with _$SignInState {
  58. const factory SignInState({
  59. String? email,
  60. String? password,
  61. required bool isSubmitting,
  62. required Option<String> passwordError,
  63. required Option<String> emailError,
  64. required Option<Either<UserProfile, FlowyError>> successOrFail,
  65. }) = _SignInState;
  66. factory SignInState.initial() => SignInState(
  67. isSubmitting: false,
  68. passwordError: none(),
  69. emailError: none(),
  70. successOrFail: none(),
  71. );
  72. }