main.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:easy_localization/easy_localization.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:hive_flutter/hive_flutter.dart';
  5. import 'package:hooks_riverpod/hooks_riverpod.dart';
  6. import 'package:immich_mobile/constants/immich_colors.dart';
  7. import 'package:immich_mobile/modules/backup/models/hive_backup_albums.model.dart';
  8. import 'package:immich_mobile/modules/backup/providers/backup.provider.dart';
  9. import 'package:immich_mobile/modules/login/models/hive_saved_login_info.model.dart';
  10. import 'package:immich_mobile/modules/login/providers/authentication.provider.dart';
  11. import 'package:immich_mobile/routing/router.dart';
  12. import 'package:immich_mobile/routing/tab_navigation_observer.dart';
  13. import 'package:immich_mobile/shared/providers/app_state.provider.dart';
  14. import 'package:immich_mobile/shared/providers/asset.provider.dart';
  15. import 'package:immich_mobile/shared/providers/release_info.provider.dart';
  16. import 'package:immich_mobile/shared/providers/server_info.provider.dart';
  17. import 'package:immich_mobile/shared/providers/websocket.provider.dart';
  18. import 'package:immich_mobile/shared/views/immich_loading_overlay.dart';
  19. import 'package:immich_mobile/shared/views/version_announcement_overlay.dart';
  20. import 'constants/hive_box.dart';
  21. void main() async {
  22. await Hive.initFlutter();
  23. Hive.registerAdapter(HiveSavedLoginInfoAdapter());
  24. Hive.registerAdapter(HiveBackupAlbumsAdapter());
  25. await Hive.openBox(userInfoBox);
  26. await Hive.openBox<HiveSavedLoginInfo>(hiveLoginInfoBox);
  27. await Hive.openBox<HiveBackupAlbums>(hiveBackupInfoBox);
  28. await Hive.openBox(hiveGithubReleaseInfoBox);
  29. SystemChrome.setSystemUIOverlayStyle(
  30. const SystemUiOverlayStyle(
  31. statusBarIconBrightness: Brightness.light,
  32. ),
  33. );
  34. await EasyLocalization.ensureInitialized();
  35. var locales = const [
  36. // Default locale
  37. Locale('en', 'US'),
  38. // Additional locales
  39. Locale('de', 'DE')
  40. ];
  41. runApp(
  42. EasyLocalization(
  43. supportedLocales: locales,
  44. path: 'assets/i18n',
  45. useFallbackTranslations: true,
  46. fallbackLocale: locales.first,
  47. child: const ProviderScope(child: ImmichApp()),
  48. ),
  49. );
  50. }
  51. class ImmichApp extends ConsumerStatefulWidget {
  52. const ImmichApp({super.key});
  53. @override
  54. ImmichAppState createState() => ImmichAppState();
  55. }
  56. class ImmichAppState extends ConsumerState<ImmichApp>
  57. with WidgetsBindingObserver {
  58. @override
  59. void didChangeAppLifecycleState(AppLifecycleState state) {
  60. switch (state) {
  61. case AppLifecycleState.resumed:
  62. debugPrint("[APP STATE] resumed");
  63. ref.watch(appStateProvider.notifier).state = AppStateEnum.resumed;
  64. var isAuthenticated = ref.watch(authenticationProvider).isAuthenticated;
  65. if (isAuthenticated) {
  66. ref.watch(backupProvider.notifier).resumeBackup();
  67. ref.watch(assetProvider.notifier).getAllAsset();
  68. ref.watch(serverInfoProvider.notifier).getServerVersion();
  69. }
  70. ref.watch(websocketProvider.notifier).connect();
  71. ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
  72. break;
  73. case AppLifecycleState.inactive:
  74. debugPrint("[APP STATE] inactive");
  75. ref.watch(appStateProvider.notifier).state = AppStateEnum.inactive;
  76. ref.watch(websocketProvider.notifier).disconnect();
  77. ref.watch(backupProvider.notifier).cancelBackup();
  78. break;
  79. case AppLifecycleState.paused:
  80. debugPrint("[APP STATE] paused");
  81. ref.watch(appStateProvider.notifier).state = AppStateEnum.paused;
  82. break;
  83. case AppLifecycleState.detached:
  84. debugPrint("[APP STATE] detached");
  85. ref.watch(appStateProvider.notifier).state = AppStateEnum.detached;
  86. break;
  87. }
  88. }
  89. Future<void> initApp() async {
  90. WidgetsBinding.instance.addObserver(this);
  91. }
  92. @override
  93. initState() {
  94. super.initState();
  95. initApp().then((_) => debugPrint("App Init Completed"));
  96. }
  97. @override
  98. void dispose() {
  99. WidgetsBinding.instance.removeObserver(this);
  100. super.dispose();
  101. }
  102. @override
  103. Widget build(BuildContext context) {
  104. var router = ref.watch(appRouterProvider);
  105. ref.watch(releaseInfoProvider.notifier).checkGithubReleaseInfo();
  106. return MaterialApp(
  107. localizationsDelegates: context.localizationDelegates,
  108. supportedLocales: context.supportedLocales,
  109. locale: context.locale,
  110. debugShowCheckedModeBanner: false,
  111. home: Stack(
  112. children: [
  113. MaterialApp.router(
  114. title: 'Immich',
  115. debugShowCheckedModeBanner: false,
  116. theme: ThemeData(
  117. useMaterial3: true,
  118. brightness: Brightness.light,
  119. primarySwatch: Colors.indigo,
  120. fontFamily: 'WorkSans',
  121. snackBarTheme: const SnackBarThemeData(
  122. contentTextStyle: TextStyle(fontFamily: 'WorkSans'),
  123. ),
  124. scaffoldBackgroundColor: immichBackgroundColor,
  125. appBarTheme: const AppBarTheme(
  126. backgroundColor: immichBackgroundColor,
  127. foregroundColor: Colors.indigo,
  128. elevation: 1,
  129. centerTitle: true,
  130. systemOverlayStyle: SystemUiOverlayStyle.dark,
  131. ),
  132. ),
  133. routeInformationParser: router.defaultRouteParser(),
  134. routerDelegate: router.delegate(
  135. navigatorObservers: () => [TabNavigationObserver(ref: ref)],
  136. ),
  137. ),
  138. const ImmichLoadingOverlay(),
  139. const VersionAnnouncementOverlay(),
  140. ],
  141. ),
  142. );
  143. }
  144. }