sign_in_bloc.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_in_bloc.freezed.dart';
  7. class SignInBloc extends Bloc<SignInEvent, SignInState> {
  8. final IAuth authImpl;
  9. SignInBloc(this.authImpl) : super(SignInState.initial());
  10. @override
  11. Stream<SignInState> mapEventToState(
  12. SignInEvent event,
  13. ) async* {
  14. yield* event.map(
  15. signedInWithUserEmailAndPassword: (e) async* {
  16. yield* _performActionOnSignIn(
  17. state,
  18. );
  19. },
  20. emailChanged: (EmailChanged value) async* {
  21. yield state.copyWith(
  22. email: value.email, emailError: none(), successOrFail: none());
  23. },
  24. passwordChanged: (PasswordChanged value) async* {
  25. yield state.copyWith(
  26. password: value.password,
  27. passwordError: none(),
  28. successOrFail: none());
  29. },
  30. );
  31. }
  32. Stream<SignInState> _performActionOnSignIn(SignInState state) async* {
  33. yield state.copyWith(
  34. isSubmitting: true,
  35. emailError: none(),
  36. passwordError: none(),
  37. successOrFail: none());
  38. final result = await authImpl.signIn(state.email, state.password);
  39. yield result.fold(
  40. (userProfile) => state.copyWith(
  41. isSubmitting: false, successOrFail: some(left(userProfile))),
  42. (error) => stateFromCode(error),
  43. );
  44. }
  45. SignInState stateFromCode(UserError error) {
  46. switch (error.code) {
  47. case ErrorCode.EmailFormatInvalid:
  48. return state.copyWith(
  49. isSubmitting: false,
  50. emailError: some(error.msg),
  51. passwordError: none());
  52. case ErrorCode.PasswordFormatInvalid:
  53. return state.copyWith(
  54. isSubmitting: false,
  55. passwordError: some(error.msg),
  56. emailError: none());
  57. default:
  58. return state.copyWith(
  59. isSubmitting: false, successOrFail: some(right(error)));
  60. }
  61. }
  62. }
  63. @freezed
  64. abstract class SignInEvent with _$SignInEvent {
  65. const factory SignInEvent.signedInWithUserEmailAndPassword() =
  66. SignedInWithUserEmailAndPassword;
  67. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  68. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  69. }
  70. @freezed
  71. abstract class SignInState with _$SignInState {
  72. const factory SignInState({
  73. String? email,
  74. String? password,
  75. required bool isSubmitting,
  76. required Option<String> passwordError,
  77. required Option<String> emailError,
  78. required Option<Either<UserProfile, UserError>> successOrFail,
  79. }) = _SignInState;
  80. factory SignInState.initial() => SignInState(
  81. isSubmitting: false,
  82. passwordError: none(),
  83. emailError: none(),
  84. successOrFail: none(),
  85. );
  86. }