sign_in_bloc.dart 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import 'package:app_flowy/user/domain/interface.dart';
  2. import 'package:dartz/dartz.dart';
  3. import 'package:flowy_sdk/protobuf/errors.pb.dart';
  4. import 'package:flowy_sdk/protobuf/user_detail.pb.dart';
  5. import 'package:freezed_annotation/freezed_annotation.dart';
  6. // ignore: import_of_legacy_library_into_null_safe
  7. import 'package:flutter_bloc/flutter_bloc.dart';
  8. part 'sign_in_event.dart';
  9. part 'sign_in_state.dart';
  10. part 'sign_in_bloc.freezed.dart';
  11. class SignInBloc extends Bloc<SignInEvent, SignInState> {
  12. final IAuth authImpl;
  13. SignInBloc(this.authImpl) : super(SignInState.initial());
  14. @override
  15. Stream<SignInState> mapEventToState(
  16. SignInEvent event,
  17. ) async* {
  18. yield* event.map(
  19. signedInWithUserEmailAndPassword: (e) async* {
  20. yield* _performActionOnSignIn(
  21. state,
  22. );
  23. },
  24. emailChanged: (EmailChanged value) async* {
  25. yield state.copyWith(email: value.email, signInFailure: none());
  26. },
  27. passwordChanged: (PasswordChanged value) async* {
  28. yield state.copyWith(password: value.password, signInFailure: none());
  29. },
  30. );
  31. }
  32. Stream<SignInState> _performActionOnSignIn(SignInState state) async* {
  33. yield state.copyWith(isSubmitting: true);
  34. final result = await authImpl.signIn(state.email, state.password);
  35. yield result.fold(
  36. (userDetail) => state.copyWith(
  37. isSubmitting: false, signInFailure: some(left(userDetail))),
  38. (s) => state.copyWith(isSubmitting: false, signInFailure: some(right(s))),
  39. );
  40. }
  41. }