mirrored_repositories/simple_app
import 'package:bloc/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_firebase/authentication/authentication_events.dart';
import 'package:flutter_firebase/repository/user_repository.dart';
import 'package:flutter_firebase/authentication/authentication_bloc.dart';
import 'package:flutter_firebase/authentication/authentication.dart';
import 'package:flutter_firebase/commons/loading_indicator.dart';
import 'package:flutter_firebase/ui/home_page.dart';
import 'package:flutter_firebase/ui/splash_screen/splash_screen.dart';
import 'package:flutter_firebase/ui/login/login_page.dart';

class SimpleBlocDelegate extends BlocDelegate {
  
  @override
  void onEvent(Bloc bloc, Object event) {
    super.onEvent(bloc, event);
    print(event);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print(transition);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stacktrace) {
    super.onError(bloc, error, stacktrace);
    print(error);
  }
}

void main() {
  BlocSupervisor.delegate = SimpleBlocDelegate();
  final userRepository = UserRepository();

  runApp(
    BlocProvider<AuthenticationBloc>(
      builder: (context) {
        return AuthenticationBloc(userRepository: userRepository)
          ..dispatch(AppStarted());
      },
      child: App(userRepository: userRepository),
    ),
  );
}

class App extends StatelessWidget {
  final UserRepository userRepository;

  App({Key key, @required this.userRepository}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          if (state is AuthenticationAuthenticated) {
            return HomePage();
          }
          if (state is AuthenticationUnauthenticated) {
            return LoginPage(userRepository: userRepository);
          }
          if (state is AuthenticationLoading) {
            return LoadingIndicator();
          }
          return SplashScreen(userRepository: userRepository,);
        },
      ),
    );
  }
}