sign_in_bloc.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // ignore: import_of_legacy_library_into_null_safe
  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 authImpl;
  10. SignInBloc(this.authImpl) : 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, signInFailure: none());
  23. },
  24. passwordChanged: (PasswordChanged value) async* {
  25. yield state.copyWith(password: value.password, signInFailure: none());
  26. },
  27. );
  28. }
  29. Stream<SignInState> _performActionOnSignIn(SignInState state) async* {
  30. yield state.copyWith(isSubmitting: true);
  31. final result = await authImpl.signIn(state.email, state.password);
  32. yield result.fold(
  33. (userDetail) => state.copyWith(
  34. isSubmitting: false, signInFailure: some(left(userDetail))),
  35. (s) => state.copyWith(isSubmitting: false, signInFailure: some(right(s))),
  36. );
  37. }
  38. }
  39. @freezed
  40. abstract class SignInEvent with _$SignInEvent {
  41. const factory SignInEvent.signedInWithUserEmailAndPassword() =
  42. SignedInWithUserEmailAndPassword;
  43. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  44. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  45. }
  46. @freezed
  47. abstract class SignInState with _$SignInState {
  48. const factory SignInState({
  49. String? email,
  50. String? password,
  51. required bool isSubmitting,
  52. required Option<Either<UserDetail, UserError>> signInFailure,
  53. }) = _SignInState;
  54. factory SignInState.initial() => SignInState(
  55. isSubmitting: false,
  56. signInFailure: none(),
  57. );
  58. }