app.dart 3.6 KB

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