main.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import 'dart:async';
  2. import 'package:computer/computer.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:photos/core/constants.dart';
  7. import 'package:photos/core/configuration.dart';
  8. import 'package:photos/favorite_files_repository.dart';
  9. import 'package:photos/folder_service.dart';
  10. import 'package:photos/memories_service.dart';
  11. import 'package:photos/photo_sync_manager.dart';
  12. import 'package:photos/ui/home_widget.dart';
  13. import 'package:sentry/sentry.dart';
  14. import 'package:super_logging/super_logging.dart';
  15. import 'package:logging/logging.dart';
  16. import 'package:uni_links/uni_links.dart';
  17. final _logger = Logger("main");
  18. void main() async {
  19. WidgetsFlutterBinding.ensureInitialized();
  20. await SuperLogging.main(LogConfig(
  21. body: _main,
  22. logDirPath: (await getTemporaryDirectory()).path + "/logs",
  23. enableInDebugMode: true,
  24. ));
  25. }
  26. void _main() async {
  27. Computer().turnOn(
  28. workersCount: 4,
  29. areLogsEnabled: false,
  30. );
  31. await Configuration.instance.init();
  32. await PhotoSyncManager.instance.init();
  33. await MemoriesService.instance.init();
  34. await FavoriteFilesRepository.instance.init();
  35. await initDeepLinks();
  36. _sync();
  37. final SentryClient sentry = new SentryClient(dsn: SENTRY_DSN);
  38. FlutterError.onError = (FlutterErrorDetails details) async {
  39. FlutterError.dumpErrorToConsole(details, forceReport: true);
  40. _sendErrorToSentry(sentry, details.exception, details.stack);
  41. };
  42. runZoned(
  43. () => runApp(MyApp()),
  44. onError: (Object error, StackTrace stackTrace) =>
  45. _sendErrorToSentry(sentry, error, stackTrace),
  46. );
  47. }
  48. void _sync() async {
  49. FolderSharingService.instance.sync().catchError((e) {
  50. _logger.warning(e);
  51. });
  52. PhotoSyncManager.instance.sync().catchError((e) {
  53. _logger.warning(e);
  54. });
  55. }
  56. void _sendErrorToSentry(SentryClient sentry, Object error, StackTrace stack) {
  57. _logger.shout("Uncaught error", error, stack);
  58. try {
  59. sentry.captureException(
  60. exception: error,
  61. stackTrace: stack,
  62. );
  63. print('Error sent to sentry.io: $error');
  64. } catch (e) {
  65. print('Sending report to sentry.io failed: $e');
  66. print('Original error: $error');
  67. }
  68. }
  69. Future<void> initDeepLinks() async {
  70. // Platform messages may fail, so we use a try/catch PlatformException.
  71. try {
  72. String initialLink = await getInitialLink();
  73. // Parse the link and warn the user, if it is not correct,
  74. // but keep in mind it could be `null`.
  75. if (initialLink != null) {
  76. _logger.info("Initial link received: " + initialLink);
  77. } else {
  78. _logger.info("No initial link received.");
  79. }
  80. } on PlatformException {
  81. // Handle exception by warning the user their action did not succeed
  82. // return?
  83. _logger.severe("PlatformException thrown while getting initial link");
  84. }
  85. // Attach a listener to the stream
  86. getLinksStream().listen((String link) {
  87. _logger.info("Link received: " + link);
  88. }, onError: (err) {
  89. _logger.severe(err);
  90. });
  91. }
  92. class MyApp extends StatelessWidget with WidgetsBindingObserver {
  93. final _title = 'ente';
  94. @override
  95. Widget build(BuildContext context) {
  96. WidgetsBinding.instance.addObserver(this);
  97. return MaterialApp(
  98. title: _title,
  99. theme: ThemeData.dark()
  100. .copyWith(hintColor: Colors.grey, accentColor: Colors.pink[400]),
  101. home: HomeWidget(_title),
  102. );
  103. }
  104. @override
  105. void didChangeAppLifecycleState(AppLifecycleState state) {
  106. if (state == AppLifecycleState.resumed) {
  107. _sync();
  108. }
  109. }
  110. }