sign_in_bloc.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(email: value.email, successOrFail: none());
  22. },
  23. passwordChanged: (PasswordChanged value) async* {
  24. yield state.copyWith(password: value.password, successOrFail: none());
  25. },
  26. );
  27. }
  28. Stream<SignInState> _performActionOnSignIn(SignInState state) async* {
  29. yield state.copyWith(isSubmitting: true);
  30. final result = await authImpl.signIn(state.email, state.password);
  31. yield result.fold(
  32. (userProfile) => state.copyWith(
  33. isSubmitting: false, successOrFail: some(left(userProfile))),
  34. (error) => stateFromCode(error),
  35. );
  36. }
  37. SignInState stateFromCode(UserError error) {
  38. switch (error.code) {
  39. case ErrorCode.EmailFormatInvalid:
  40. return state.copyWith(
  41. isSubmitting: false,
  42. emailError: some(error.msg),
  43. passwordError: none());
  44. case ErrorCode.PasswordFormatInvalid:
  45. return state.copyWith(
  46. isSubmitting: false,
  47. passwordError: some(error.msg),
  48. emailError: none());
  49. default:
  50. return state.copyWith(
  51. isSubmitting: false, successOrFail: some(right(error)));
  52. }
  53. }
  54. }
  55. @freezed
  56. abstract class SignInEvent with _$SignInEvent {
  57. const factory SignInEvent.signedInWithUserEmailAndPassword() =
  58. SignedInWithUserEmailAndPassword;
  59. const factory SignInEvent.emailChanged(String email) = EmailChanged;
  60. const factory SignInEvent.passwordChanged(String password) = PasswordChanged;
  61. }
  62. @freezed
  63. abstract class SignInState with _$SignInState {
  64. const factory SignInState({
  65. String? email,
  66. String? password,
  67. required bool isSubmitting,
  68. required Option<String> passwordError,
  69. required Option<String> emailError,
  70. required Option<Either<UserProfile, UserError>> successOrFail,
  71. }) = _SignInState;
  72. factory SignInState.initial() => SignInState(
  73. isSubmitting: false,
  74. passwordError: none(),
  75. emailError: none(),
  76. successOrFail: none(),
  77. );
  78. }