wonders/lib/main.dart

93 lines
4.0 KiB
Dart
Raw Normal View History

2022-08-29 20:38:28 -06:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:wonders/common_libs.dart';
2023-10-03 14:17:20 -06:00
import 'package:wonders/logic/artifact_api_logic.dart';
import 'package:wonders/logic/artifact_api_service.dart';
2023-12-04 14:54:00 -07:00
import 'package:wonders/logic/collectibles_logic.dart';
import 'package:wonders/logic/native_widget_service.dart';
2023-12-04 14:54:00 -07:00
import 'package:wonders/logic/locale_logic.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/logic/timeline_logic.dart';
import 'package:wonders/logic/unsplash_logic.dart';
import 'package:wonders/logic/wonders_logic.dart';
import 'package:wonders/ui/common/app_shortcuts.dart';
2022-08-29 20:38:28 -06:00
void main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
// Keep native splash screen up until app is finished bootstrapping
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
GoRouter.optionURLReflectsImperativeAPIs = true;
2022-08-29 20:38:28 -06:00
// Start app
registerSingletons();
2024-01-17 10:51:45 -07:00
runApp(WondersApp());
await appLogic.bootstrap();
2022-08-29 20:38:28 -06:00
// Remove splash screen when bootstrap is complete
FlutterNativeSplash.remove();
}
2023-01-01 12:20:42 -07:00
/// Creates an app using the [MaterialApp.router] constructor and the global `appRouter`, an instance of [GoRouter].
class WondersApp extends StatelessWidget with GetItMixin {
WondersApp({Key? key}) : super(key: key);
2022-08-29 20:38:28 -06:00
@override
Widget build(BuildContext context) {
final locale = watchX((SettingsLogic s) => s.currentLocale);
return MaterialApp.router(
2023-01-01 12:20:42 -07:00
routeInformationProvider: appRouter.routeInformationProvider,
routeInformationParser: appRouter.routeInformationParser,
locale: locale == null ? null : Locale(locale),
debugShowCheckedModeBanner: false,
routerDelegate: appRouter.routerDelegate,
shortcuts: AppShortcuts.defaults,
2023-04-11 14:46:22 -06:00
theme: ThemeData(fontFamily: $styles.text.body.fontFamily, useMaterial3: true),
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: AppLocalizations.supportedLocales,
2022-08-29 20:38:28 -06:00
);
}
}
2023-01-01 12:20:42 -07:00
/// Create singletons (logic and services) that can be shared across the app.
2022-08-29 20:38:28 -06:00
void registerSingletons() {
// Top level app controller
GetIt.I.registerLazySingleton<AppLogic>(() => AppLogic());
// Wonders
GetIt.I.registerLazySingleton<WondersLogic>(() => WondersLogic());
// Timeline / Events
GetIt.I.registerLazySingleton<TimelineLogic>(() => TimelineLogic());
// Search
2023-10-03 14:17:20 -06:00
GetIt.I.registerLazySingleton<ArtifactAPILogic>(() => ArtifactAPILogic());
GetIt.I.registerLazySingleton<ArtifactAPIService>(() => ArtifactAPIService());
2022-08-29 20:38:28 -06:00
// Settings
GetIt.I.registerLazySingleton<SettingsLogic>(() => SettingsLogic());
// Unsplash
GetIt.I.registerLazySingleton<UnsplashLogic>(() => UnsplashLogic());
// Collectibles
GetIt.I.registerLazySingleton<CollectiblesLogic>(() => CollectiblesLogic());
// Localizations
GetIt.I.registerLazySingleton<LocaleLogic>(() => LocaleLogic());
// Home Widget Service
GetIt.I.registerLazySingleton<NativeWidgetService>(() => NativeWidgetService());
2022-08-29 20:38:28 -06:00
}
2023-01-01 12:20:42 -07:00
/// Add syntax sugar for quickly accessing the main "logic" controllers in the app
2022-08-29 20:38:28 -06:00
/// We deliberately do not create shortcuts for services, to discourage their use directly in the view/widget layer.
AppLogic get appLogic => GetIt.I.get<AppLogic>();
WondersLogic get wondersLogic => GetIt.I.get<WondersLogic>();
TimelineLogic get timelineLogic => GetIt.I.get<TimelineLogic>();
SettingsLogic get settingsLogic => GetIt.I.get<SettingsLogic>();
UnsplashLogic get unsplashLogic => GetIt.I.get<UnsplashLogic>();
ArtifactAPILogic get artifactLogic => GetIt.I.get<ArtifactAPILogic>();
2022-08-29 20:38:28 -06:00
CollectiblesLogic get collectiblesLogic => GetIt.I.get<CollectiblesLogic>();
LocaleLogic get localeLogic => GetIt.I.get<LocaleLogic>();
2023-01-01 12:20:42 -07:00
/// Global helpers for readability
2022-08-29 20:38:28 -06:00
AppLocalizations get $strings => localeLogic.strings;
2023-01-01 12:20:42 -07:00
AppStyle get $styles => WondersAppScaffold.style;