ente/lib/main.dart

188 lines
5.6 KiB
Dart
Raw Normal View History

2020-05-02 17:16:59 +05:30
import 'dart:async';
2021-01-13 23:09:45 +05:30
import 'package:background_fetch/background_fetch.dart';
import 'package:flutter/foundation.dart';
2020-03-25 01:29:36 +05:30
import 'package:flutter/material.dart';
2021-01-08 22:41:32 +05:30
import 'package:in_app_purchase/in_app_purchase.dart';
2020-05-02 21:58:54 +05:30
import 'package:path_provider/path_provider.dart';
import 'package:photos/core/constants.dart';
import 'package:photos/core/configuration.dart';
2021-01-08 12:08:45 +05:30
import 'package:photos/services/billing_service.dart';
import 'package:photos/services/collections_service.dart';
2020-10-03 23:26:18 +05:30
import 'package:photos/services/memories_service.dart';
2020-10-03 23:28:26 +05:30
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/home_widget.dart';
import 'package:photos/utils/crypto_util.dart';
2020-05-02 17:16:59 +05:30
import 'package:sentry/sentry.dart';
import 'package:super_logging/super_logging.dart';
2020-05-02 21:58:54 +05:30
import 'package:logging/logging.dart';
2020-08-22 04:58:52 +05:30
final _logger = Logger("main");
2021-02-26 19:39:45 +05:30
AppInitState _state = AppInitState.pending;
2020-03-25 01:29:36 +05:30
void main() async {
WidgetsFlutterBinding.ensureInitialized();
2021-01-13 23:09:45 +05:30
await _runWithLogs(_main);
2020-05-02 21:58:54 +05:30
}
2021-02-26 17:19:35 +05:30
void _main() async {
final SentryClient sentry =
new SentryClient(dsn: kDebugMode ? SENTRY_DEBUG_DSN : SENTRY_DSN);
2020-05-02 17:16:59 +05:30
FlutterError.onError = (FlutterErrorDetails details) async {
FlutterError.dumpErrorToConsole(details, forceReport: true);
_sendErrorToSentry(sentry, details.exception, details.stack);
};
2021-03-02 00:56:35 +05:30
runZonedGuarded(
2021-02-09 13:44:39 +05:30
() async {
2021-02-26 19:39:45 +05:30
if (_state == AppInitState.pending) {
await _init();
}
if (_state == AppInitState.initialized) {
_sync();
}
2021-01-13 23:09:45 +05:30
runApp(MyApp());
2021-02-26 17:19:35 +05:30
BackgroundFetch.registerHeadlessTask(_onBackgroundTaskReceived);
2021-01-13 23:09:45 +05:30
},
2021-03-02 00:56:35 +05:30
(Object error, StackTrace stackTrace) {
2021-01-13 23:09:45 +05:30
_sendErrorToSentry(sentry, error, stackTrace);
},
);
}
Future _init() async {
2021-02-26 19:39:45 +05:30
_state = AppInitState.initialzing;
_logger.info("Initializing...", Error(), StackTrace.current);
2021-01-13 23:09:45 +05:30
InAppPurchaseConnection.enablePendingPurchases();
CryptoUtil.init();
2021-01-13 23:09:45 +05:30
await Configuration.instance.init();
await BillingService.instance.init();
2021-01-13 23:09:45 +05:30
await CollectionsService.instance.init();
await SyncService.instance.init();
await MemoriesService.instance.init();
2021-02-26 19:39:45 +05:30
_state = AppInitState.initialized;
2021-02-06 21:41:27 +05:30
_logger.info("Initialization done");
2020-05-02 17:16:59 +05:30
}
2021-01-13 23:09:45 +05:30
Future<void> _sync({bool isAppInBackground = false}) async {
2021-02-26 15:20:52 +05:30
if (SyncService.instance.isSyncInProgress()) {
return;
}
2021-02-26 19:39:45 +05:30
if (isAppInBackground) {
_logger.info("Syncing in background");
}
2021-01-13 23:09:45 +05:30
try {
await SyncService.instance.sync(isAppInBackground: isAppInBackground);
} catch (e, s) {
2020-10-13 02:04:34 +05:30
_logger.severe("Sync error", e, s);
2021-01-13 23:09:45 +05:30
}
}
2021-02-26 17:19:35 +05:30
Future _onBackgroundTaskReceived(String taskId) async {
2021-02-26 19:39:45 +05:30
if (_state == AppInitState.pending) {
await _runWithLogs(() async {
2021-02-26 19:39:45 +05:30
_logger.info("[BackgroundFetch] Event received: $taskId");
2021-01-13 23:09:45 +05:30
await _init();
await _sync(isAppInBackground: true);
});
2021-02-26 19:39:45 +05:30
} else if (_state == AppInitState.initialized) {
_logger.info("[BackgroundFetch] Skipping init");
2021-01-13 23:09:45 +05:30
await _sync(isAppInBackground: true);
2021-02-26 19:39:45 +05:30
} else {
_logger.info("[BackgroundFetch] Skipping init and sync");
}
2021-01-13 23:09:45 +05:30
BackgroundFetch.finish(taskId);
}
Future _runWithLogs(Function() function) async {
await SuperLogging.main(LogConfig(
body: function,
logDirPath: (await getTemporaryDirectory()).path + "/logs",
enableInDebugMode: true,
maxLogFiles: 5,
));
}
2020-05-02 21:58:54 +05:30
void _sendErrorToSentry(SentryClient sentry, Object error, StackTrace stack) {
2020-08-22 04:58:52 +05:30
_logger.shout("Uncaught error", error, stack);
2020-05-02 17:16:59 +05:30
try {
sentry.captureException(
exception: error,
2020-05-02 21:58:54 +05:30
stackTrace: stack,
2020-05-02 17:16:59 +05:30
);
2021-02-26 16:35:54 +05:30
_logger.info('Error sent to sentry.io: $error');
2020-05-02 17:16:59 +05:30
} catch (e) {
2021-02-26 16:35:54 +05:30
_logger.info('Sending report to sentry.io failed: $e');
_logger.info('Original error: $error');
2020-05-02 17:16:59 +05:30
}
2020-03-25 01:29:36 +05:30
}
2020-03-27 21:37:55 +05:30
2020-04-27 18:32:29 +05:30
class MyApp extends StatelessWidget with WidgetsBindingObserver {
2020-08-07 21:28:15 +05:30
final _title = 'ente';
2020-04-05 14:01:01 +05:30
@override
Widget build(BuildContext context) {
2020-04-27 18:32:29 +05:30
WidgetsBinding.instance.addObserver(this);
2021-01-13 23:09:45 +05:30
// Configure BackgroundFetch.
BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: false,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.NONE,
), (String taskId) async {
2021-02-26 17:19:35 +05:30
await _onBackgroundTaskReceived(taskId);
2021-01-13 23:09:45 +05:30
}).then((int status) {
2021-02-26 16:35:54 +05:30
_logger.info('[BackgroundFetch] configure success: $status');
2021-01-13 23:09:45 +05:30
}).catchError((e) {
2021-02-26 16:35:54 +05:30
_logger.info('[BackgroundFetch] configure ERROR: $e');
2021-01-13 23:09:45 +05:30
});
2020-04-14 21:06:18 +05:30
return MaterialApp(
title: _title,
2020-12-04 01:06:18 +05:30
theme: ThemeData(
2020-12-12 06:01:06 +05:30
fontFamily: 'Ubuntu',
2020-12-04 01:06:18 +05:30
brightness: Brightness.dark,
2020-08-25 11:30:19 +05:30
hintColor: Colors.grey,
2021-02-05 16:37:29 +05:30
accentColor: Color.fromRGBO(45, 194, 98, 1.0),
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
2020-10-10 05:16:51 +05:30
buttonTheme: ButtonThemeData().copyWith(
2021-02-05 16:37:29 +05:30
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
2020-10-10 05:16:51 +05:30
),
2021-02-05 16:37:29 +05:30
toggleableActiveColor: Colors.green[400],
2021-02-01 21:27:40 +05:30
scaffoldBackgroundColor: Colors.black,
2021-02-05 16:37:29 +05:30
backgroundColor: Colors.black,
appBarTheme: AppBarTheme().copyWith(
2021-02-07 01:18:29 +05:30
color: Color.fromRGBO(10, 20, 20, 1.0),
2021-02-05 16:37:29 +05:30
),
cardColor: Color.fromRGBO(25, 25, 25, 1.0),
2021-02-06 01:18:20 +05:30
dialogTheme: DialogTheme().copyWith(
backgroundColor: Color.fromRGBO(20, 20, 20, 1.0),
),
2020-08-25 11:30:19 +05:30
),
2020-05-05 01:33:06 +05:30
home: HomeWidget(_title),
2020-04-05 14:01:01 +05:30
);
}
2020-04-27 18:32:29 +05:30
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
2020-11-30 16:17:06 +05:30
_logger.info("App resumed");
_sync();
2020-04-27 18:32:29 +05:30
}
}
2020-04-05 14:01:01 +05:30
}
2021-02-26 19:39:45 +05:30
enum AppInitState {
pending,
initialzing,
initialized,
}