app.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'dart:io';
  2. import 'package:adaptive_theme/adaptive_theme.dart';
  3. import 'package:background_fetch/background_fetch.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_easyloading/flutter_easyloading.dart';
  6. import 'package:flutter_gen/gen_l10n/app_localizations.dart';
  7. import 'package:flutter_localizations/flutter_localizations.dart';
  8. import 'package:logging/logging.dart';
  9. import 'package:photos/core/network.dart';
  10. import 'package:photos/ente_theme_data.dart';
  11. import 'package:photos/l10n/l10n.dart';
  12. import 'package:photos/services/app_lifecycle_service.dart';
  13. import 'package:photos/services/sync_service.dart';
  14. import 'package:photos/ui/home_widget.dart';
  15. class EnteApp extends StatefulWidget {
  16. static const _homeWidget = HomeWidget();
  17. final Future<void> Function(String) runBackgroundTask;
  18. final Future<void> Function(String) killBackgroundTask;
  19. const EnteApp(
  20. this.runBackgroundTask,
  21. this.killBackgroundTask, {
  22. Key key,
  23. }) : super(key: key);
  24. @override
  25. State<EnteApp> createState() => _EnteAppState();
  26. }
  27. class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
  28. final _logger = Logger("EnteAppState");
  29. @override
  30. void initState() {
  31. _logger.info('init App');
  32. super.initState();
  33. WidgetsBinding.instance.addObserver(this);
  34. _configureBackgroundFetch();
  35. }
  36. @override
  37. Widget build(BuildContext context) {
  38. if (Platform.isAndroid) {
  39. return AdaptiveTheme(
  40. light: lightThemeData,
  41. dark: darkThemeData,
  42. initial: AdaptiveThemeMode.system,
  43. builder: (lightTheme, dartTheme) => MaterialApp(
  44. title: "ente",
  45. themeMode: ThemeMode.system,
  46. theme: lightTheme,
  47. darkTheme: dartTheme,
  48. home: EnteApp._homeWidget,
  49. debugShowCheckedModeBanner: false,
  50. navigatorKey: Network.instance.getAlice().getNavigatorKey(),
  51. builder: EasyLoading.init(),
  52. supportedLocales: L10n.all,
  53. localizationsDelegates: const [
  54. AppLocalizations.delegate,
  55. GlobalMaterialLocalizations.delegate,
  56. GlobalCupertinoLocalizations.delegate,
  57. GlobalWidgetsLocalizations.delegate,
  58. ],
  59. ),
  60. );
  61. } else {
  62. return MaterialApp(
  63. title: "ente",
  64. themeMode: ThemeMode.system,
  65. theme: lightThemeData,
  66. darkTheme: darkThemeData,
  67. home: EnteApp._homeWidget,
  68. debugShowCheckedModeBanner: false,
  69. navigatorKey: Network.instance.getAlice().getNavigatorKey(),
  70. builder: EasyLoading.init(),
  71. supportedLocales: L10n.all,
  72. localizationsDelegates: const [
  73. AppLocalizations.delegate,
  74. GlobalMaterialLocalizations.delegate,
  75. GlobalCupertinoLocalizations.delegate,
  76. GlobalWidgetsLocalizations.delegate,
  77. ],
  78. );
  79. }
  80. }
  81. @override
  82. void dispose() {
  83. WidgetsBinding.instance.removeObserver(this);
  84. super.dispose();
  85. }
  86. @override
  87. void didChangeAppLifecycleState(AppLifecycleState state) {
  88. final String stateChangeReason = 'app -> $state';
  89. if (state == AppLifecycleState.resumed) {
  90. AppLifecycleService.instance
  91. .onAppInForeground(stateChangeReason + ': sync now');
  92. SyncService.instance.sync();
  93. } else {
  94. AppLifecycleService.instance.onAppInBackground(stateChangeReason);
  95. }
  96. }
  97. void _configureBackgroundFetch() {
  98. BackgroundFetch.configure(
  99. BackgroundFetchConfig(
  100. minimumFetchInterval: 15,
  101. forceAlarmManager: false,
  102. stopOnTerminate: false,
  103. startOnBoot: true,
  104. enableHeadless: true,
  105. requiresBatteryNotLow: true,
  106. requiresCharging: false,
  107. requiresStorageNotLow: false,
  108. requiresDeviceIdle: false,
  109. requiredNetworkType: NetworkType.ANY,
  110. ), (String taskId) async {
  111. await widget.runBackgroundTask(taskId);
  112. }, (taskId) {
  113. _logger.info("BG task timeout taskID: $taskId");
  114. widget.killBackgroundTask(taskId);
  115. }).then((int status) {
  116. _logger.info('[BackgroundFetch] configure success: $status');
  117. }).catchError((e) {
  118. _logger.info('[BackgroundFetch] configure ERROR: $e');
  119. });
  120. }
  121. }