app.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import 'package:background_fetch/background_fetch.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_easyloading/flutter_easyloading.dart';
  4. import 'package:flutter_gen/gen_l10n/app_localizations.dart';
  5. import 'package:flutter_localizations/flutter_localizations.dart';
  6. import 'package:logging/logging.dart';
  7. import 'package:photos/core/network.dart';
  8. import 'package:photos/l10n/l10n.dart';
  9. import 'package:photos/services/app_lifecycle_service.dart';
  10. import 'package:photos/services/sync_service.dart';
  11. import 'package:photos/ui/home_widget.dart';
  12. final lightThemeData = ThemeData(
  13. fontFamily: 'Ubuntu',
  14. brightness: Brightness.light,
  15. hintColor: Colors.grey,
  16. iconTheme: IconThemeData(color: Colors.green),
  17. primaryIconTheme: IconThemeData(color: Colors.red, opacity: 1.0, size: 50.0),
  18. colorScheme: ColorScheme.light(primary: Colors.blue),
  19. accentColor: Color.fromRGBO(45, 194, 98, 0.2),
  20. buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
  21. buttonTheme: ButtonThemeData().copyWith(
  22. buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
  23. ),
  24. toggleableActiveColor: Colors.red[400],
  25. scaffoldBackgroundColor: Colors.white,
  26. backgroundColor: Colors.white,
  27. appBarTheme: AppBarTheme().copyWith(color: Colors.blue),
  28. textTheme: TextTheme().copyWith(
  29. caption: TextStyle(
  30. color: Colors.black.withOpacity(0.7),
  31. fontSize: 14,
  32. ),
  33. overline: TextStyle(
  34. color: Colors.black.withOpacity(0.8),
  35. fontSize: 12,
  36. )),
  37. primaryTextTheme: TextTheme().copyWith(
  38. bodyText2: TextStyle(color: Colors.yellow),
  39. bodyText1: TextStyle(color: Colors.orange)),
  40. cardColor: Color.fromRGBO(250, 250, 250, 1.0), //
  41. dialogTheme: DialogTheme().copyWith(
  42. backgroundColor: Color.fromRGBO(250, 250, 250, 1.0), //
  43. ),
  44. textSelectionTheme: TextSelectionThemeData().copyWith(
  45. cursorColor: Colors.white.withOpacity(0.5),
  46. ),
  47. inputDecorationTheme: InputDecorationTheme().copyWith(
  48. focusedBorder: UnderlineInputBorder(
  49. borderSide: BorderSide(
  50. color: Color.fromRGBO(45, 194, 98, 1.0),
  51. ),
  52. ),
  53. ),
  54. );
  55. final darkThemeData = ThemeData(
  56. fontFamily: 'Ubuntu',
  57. brightness: Brightness.dark,
  58. iconTheme: IconThemeData(color: Colors.white),
  59. primaryIconTheme: IconThemeData(color: Colors.red, opacity: 1.0, size: 50.0),
  60. hintColor: Colors.grey,
  61. colorScheme: ColorScheme.dark(),
  62. accentColor: Color.fromRGBO(45, 194, 98, 0.2),
  63. buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
  64. buttonTheme: ButtonThemeData().copyWith(
  65. buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
  66. ),
  67. // primaryColor: Colors.red,
  68. textTheme: TextTheme().copyWith(
  69. caption: TextStyle(
  70. color: Colors.white.withOpacity(0.6),
  71. fontSize: 14,
  72. ),
  73. overline: TextStyle(
  74. color: Colors.white.withOpacity(0.8),
  75. fontSize: 12,
  76. )),
  77. toggleableActiveColor: Colors.green[400],
  78. scaffoldBackgroundColor: Colors.black,
  79. backgroundColor: Colors.black,
  80. appBarTheme: AppBarTheme().copyWith(
  81. color: Color.fromRGBO(10, 20, 20, 1.0),
  82. ),
  83. cardColor: Color.fromRGBO(10, 15, 15, 1.0),
  84. dialogTheme: DialogTheme().copyWith(
  85. backgroundColor: Color.fromRGBO(10, 15, 15, 1.0),
  86. ),
  87. textSelectionTheme: TextSelectionThemeData().copyWith(
  88. cursorColor: Colors.white.withOpacity(0.5),
  89. ),
  90. inputDecorationTheme: InputDecorationTheme().copyWith(
  91. focusedBorder: UnderlineInputBorder(
  92. borderSide: BorderSide(
  93. color: Color.fromRGBO(45, 194, 98, 1.0),
  94. ),
  95. ),
  96. ),
  97. );
  98. extension CustomColorScheme on ColorScheme {
  99. Color get defaultTextColor =>
  100. brightness == Brightness.light ? Colors.black : Colors.white;
  101. }
  102. class EnteApp extends StatefulWidget {
  103. static const _homeWidget = HomeWidget();
  104. final Future<void> Function(String) runBackgroundTask;
  105. final Future<void> Function(String) killBackgroundTask;
  106. EnteApp(
  107. this.runBackgroundTask,
  108. this.killBackgroundTask, {
  109. Key key,
  110. }) : super(key: key);
  111. @override
  112. _EnteAppState createState() => _EnteAppState();
  113. }
  114. class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
  115. final _logger = Logger("EnteAppState");
  116. @override
  117. void initState() {
  118. super.initState();
  119. WidgetsBinding.instance.addObserver(this);
  120. _configureBackgroundFetch();
  121. }
  122. @override
  123. Widget build(BuildContext context) {
  124. return MaterialApp(
  125. title: "ente",
  126. themeMode: ThemeMode.system,
  127. theme: lightThemeData,
  128. darkTheme: darkThemeData,
  129. home: EnteApp._homeWidget,
  130. debugShowCheckedModeBanner: false,
  131. navigatorKey: Network.instance.getAlice().getNavigatorKey(),
  132. builder: EasyLoading.init(),
  133. supportedLocales: L10n.all,
  134. localizationsDelegates: const [
  135. AppLocalizations.delegate,
  136. GlobalMaterialLocalizations.delegate,
  137. GlobalCupertinoLocalizations.delegate,
  138. GlobalWidgetsLocalizations.delegate,
  139. ],
  140. );
  141. }
  142. @override
  143. void dispose() {
  144. WidgetsBinding.instance.removeObserver(this);
  145. super.dispose();
  146. }
  147. @override
  148. void didChangeAppLifecycleState(AppLifecycleState state) {
  149. if (state == AppLifecycleState.resumed) {
  150. AppLifecycleService.instance.onAppInForeground();
  151. SyncService.instance.sync();
  152. } else {
  153. AppLifecycleService.instance.onAppInBackground();
  154. }
  155. }
  156. void _configureBackgroundFetch() {
  157. BackgroundFetch.configure(
  158. BackgroundFetchConfig(
  159. minimumFetchInterval: 15,
  160. forceAlarmManager: false,
  161. stopOnTerminate: false,
  162. startOnBoot: true,
  163. enableHeadless: true,
  164. requiresBatteryNotLow: false,
  165. requiresCharging: false,
  166. requiresStorageNotLow: false,
  167. requiresDeviceIdle: false,
  168. requiredNetworkType: NetworkType.NONE,
  169. ), (String taskId) async {
  170. await widget.runBackgroundTask(taskId);
  171. }, (taskId) {
  172. _logger.info("BG task timeout");
  173. widget.killBackgroundTask(taskId);
  174. }).then((int status) {
  175. _logger.info('[BackgroundFetch] configure success: $status');
  176. }).catchError((e) {
  177. _logger.info('[BackgroundFetch] configure ERROR: $e');
  178. });
  179. }
  180. }