ente/lib/app.dart

121 lines
3.6 KiB
Dart
Raw Normal View History

import 'dart:io';
2022-03-09 05:57:27 +00:00
import 'package:adaptive_theme/adaptive_theme.dart';
import 'package:background_fetch/background_fetch.dart';
import 'package:flutter/foundation.dart';
2021-11-24 07:26:07 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:logging/logging.dart';
2022-04-08 05:59:20 +00:00
import 'package:photos/ente_theme_data.dart';
2021-11-24 07:26:07 +00:00
import 'package:photos/services/app_lifecycle_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/home_widget.dart';
class EnteApp extends StatefulWidget {
static const _homeWidget = HomeWidget();
final Future<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
const EnteApp(
this.runBackgroundTask,
this.killBackgroundTask, {
Key? key,
}) : super(key: key);
2021-11-24 07:26:07 +00:00
@override
2022-07-03 09:45:00 +00:00
State<EnteApp> createState() => _EnteAppState();
2021-11-24 07:26:07 +00:00
}
class _EnteAppState extends State<EnteApp> with WidgetsBindingObserver {
2021-11-28 05:18:00 +00:00
final _logger = Logger("EnteAppState");
2021-11-24 07:26:07 +00:00
@override
void initState() {
2022-03-08 07:17:19 +00:00
_logger.info('init App');
2021-11-24 07:26:07 +00:00
super.initState();
WidgetsBinding.instance.addObserver(this);
_configureBackgroundFetch();
2021-11-24 07:26:07 +00:00
}
@override
Widget build(BuildContext context) {
if (Platform.isAndroid || kDebugMode) {
2022-06-01 08:39:31 +00:00
return AdaptiveTheme(
2022-05-30 12:34:03 +00:00
light: lightThemeData,
dark: darkThemeData,
2022-06-01 08:39:31 +00:00
initial: AdaptiveThemeMode.system,
2022-05-30 12:34:03 +00:00
builder: (lightTheme, dartTheme) => MaterialApp(
2022-06-01 08:39:31 +00:00
title: "ente",
themeMode: ThemeMode.system,
theme: lightTheme,
darkTheme: dartTheme,
home: EnteApp._homeWidget,
debugShowCheckedModeBanner: false,
builder: EasyLoading.init(),
2022-09-28 03:46:21 +00:00
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: AppLocalizations.localizationsDelegates,
2022-06-01 08:39:31 +00:00
),
);
} else {
return MaterialApp(
title: "ente",
themeMode: ThemeMode.system,
theme: lightThemeData,
darkTheme: darkThemeData,
home: EnteApp._homeWidget,
debugShowCheckedModeBanner: false,
builder: EasyLoading.init(),
2022-09-28 03:46:21 +00:00
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: AppLocalizations.localizationsDelegates,
2022-06-01 08:39:31 +00:00
);
}
2021-11-24 07:26:07 +00:00
}
2022-03-07 17:33:41 +00:00
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
2021-11-24 07:26:07 +00:00
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
2022-03-07 20:18:50 +00:00
final String stateChangeReason = 'app -> $state';
2021-11-24 07:26:07 +00:00
if (state == AppLifecycleState.resumed) {
2022-03-08 07:17:19 +00:00
AppLifecycleService.instance
.onAppInForeground(stateChangeReason + ': sync now');
2021-11-24 07:26:07 +00:00
SyncService.instance.sync();
} else {
2022-03-07 20:18:50 +00:00
AppLifecycleService.instance.onAppInBackground(stateChangeReason);
2021-11-24 07:26:07 +00:00
}
}
void _configureBackgroundFetch() {
BackgroundFetch.configure(
BackgroundFetchConfig(
minimumFetchInterval: 15,
forceAlarmManager: false,
stopOnTerminate: false,
startOnBoot: true,
enableHeadless: true,
requiresBatteryNotLow: true,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.ANY,
), (String taskId) async {
await widget.runBackgroundTask(taskId);
}, (taskId) {
2022-03-07 20:18:50 +00:00
_logger.info("BG task timeout taskID: $taskId");
widget.killBackgroundTask(taskId);
}).then((int status) {
_logger.info('[BackgroundFetch] configure success: $status');
}).catchError((e) {
_logger.info('[BackgroundFetch] configure ERROR: $e');
});
}
2021-11-24 07:26:07 +00:00
}