sign_in_bloc.dart 1.5 KB

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