ente/lib/app.dart

130 lines
3.9 KiB
Dart
Raw Normal View History

import 'package:background_fetch/background_fetch.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:flutter_localizations/flutter_localizations.dart';
import 'package:logging/logging.dart';
2021-11-24 07:26:07 +00:00
import 'package:photos/core/network.dart';
import 'package:photos/l10n/l10n.dart';
import 'package:photos/services/app_lifecycle_service.dart';
import 'package:photos/services/sync_service.dart';
import 'package:photos/ui/home_widget.dart';
final themeData = ThemeData(
fontFamily: 'Ubuntu',
brightness: Brightness.dark,
hintColor: Colors.grey,
accentColor: Color.fromRGBO(45, 194, 98, 0.2),
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
buttonTheme: ButtonThemeData().copyWith(
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
),
toggleableActiveColor: Colors.green[400],
scaffoldBackgroundColor: Colors.black,
backgroundColor: Colors.black,
appBarTheme: AppBarTheme().copyWith(
color: Color.fromRGBO(10, 20, 20, 1.0),
),
cardColor: Color.fromRGBO(10, 15, 15, 1.0),
dialogTheme: DialogTheme().copyWith(
backgroundColor: Color.fromRGBO(10, 15, 15, 1.0),
),
textSelectionTheme: TextSelectionThemeData().copyWith(
cursorColor: Colors.white.withOpacity(0.5),
),
inputDecorationTheme: InputDecorationTheme().copyWith(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromRGBO(45, 194, 98, 1.0),
),
),
),
);
class EnteApp extends StatefulWidget {
static const _homeWidget = HomeWidget();
final Future<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
EnteApp(
this.runBackgroundTask,
this.killBackgroundTask, {
Key key,
}) : super(key: key);
2021-11-24 07:26:07 +00:00
@override
_EnteAppState createState() => _EnteAppState();
}
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() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_configureBackgroundFetch();
2021-11-24 07:26:07 +00:00
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "ente",
theme: themeData,
home: EnteApp._homeWidget,
debugShowCheckedModeBanner: false,
navigatorKey: Network.instance.getAlice().getNavigatorKey(),
builder: EasyLoading.init(),
supportedLocales: L10n.all,
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
);
}
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) {
if (state == AppLifecycleState.resumed) {
AppLifecycleService.instance.onAppInForeground();
SyncService.instance.sync();
} else {
AppLifecycleService.instance.onAppInBackground();
}
}
void _configureBackgroundFetch() {
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 {
await widget.runBackgroundTask(taskId);
}, (taskId) {
_logger.info("BG task timeout");
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
}