splash_bloc.dart 1.1 KB

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