splash_bloc.dart 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import 'package:appflowy/startup/startup.dart';
  2. import 'package:appflowy/user/application/auth/auth_service.dart';
  3. import 'package:appflowy/user/domain/auth_state.dart';
  4. import 'package:flutter_bloc/flutter_bloc.dart';
  5. import 'package:freezed_annotation/freezed_annotation.dart';
  6. part 'splash_bloc.freezed.dart';
  7. class SplashBloc extends Bloc<SplashEvent, SplashState> {
  8. SplashBloc() : super(SplashState.initial()) {
  9. on<SplashEvent>((event, emit) async {
  10. await event.map(
  11. getUser: (val) async {
  12. final response = await getIt<AuthService>().getUser();
  13. final authState = response.fold(
  14. (error) => AuthState.unauthenticated(error),
  15. (user) => AuthState.authenticated(user),
  16. );
  17. emit(state.copyWith(auth: authState));
  18. },
  19. );
  20. });
  21. }
  22. }
  23. @freezed
  24. class SplashEvent with _$SplashEvent {
  25. const factory SplashEvent.getUser() = _GetUser;
  26. }
  27. @freezed
  28. class SplashState with _$SplashState {
  29. const factory SplashState({
  30. required AuthState auth,
  31. }) = _SplashState;
  32. factory SplashState.initial() => const SplashState(
  33. auth: AuthState.initial(),
  34. );
  35. }