mirrored_repositories/defiscan
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'core/app_core.dart';
import 'features/settings/bloc/settings_cubit.dart';
import 'shared/locale/app_locale.dart';
import 'shared/prefs/app_preferences.dart';
import 'shared/services/storage/storage_service.dart';
import 'shared/theme/app_theme.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AppPreferences.init();
  await StorageService.initLocalDatabase();
  runApp(
    MultiRepositoryProvider(
      providers: repositoryProviders,
      child: const MyApp(),
    ),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => SettingsCubit(),
      child: BlocBuilder<SettingsCubit, SettingsState>(
        builder: (context, state) {
          return MaterialApp(
            theme: AppTheme.lightTheme(),
            darkTheme: AppTheme.darkTheme(),
            themeMode: state.isDarkMode ? ThemeMode.dark : ThemeMode.light,
            debugShowCheckedModeBanner: false,
            initialRoute: AppRoute.splash,
            onGenerateRoute: RouteGenerator.generateRoute,
            locale: Locale(state.languageCode),
            supportedLocales:
                AppLocale.list.map((e) => Locale(e.code)).toList(),
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              LocalJsonLocalization.delegate,
            ],
          );
        },
      ),
    );
  }
}
