ente/lib/app.dart

325 lines
11 KiB
Dart
Raw Normal View History

2022-03-12 14:03:30 +00:00
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';
2022-03-12 14:03:30 +00:00
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:flutter_localizations/flutter_localizations.dart';
import 'package:logging/logging.dart';
2021-11-24 07:26:07 +00:00
import 'package:photos/core/network.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/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';
2022-03-05 20:52:00 +00:00
final lightThemeData = ThemeData(
2022-03-08 16:34:26 +00:00
fontFamily: 'Inter',
2022-03-05 20:52:00 +00:00
brightness: Brightness.light,
hintColor: Colors.grey,
2022-03-12 14:03:30 +00:00
primaryColor: Colors.deepOrangeAccent,
primaryColorLight: Colors.black54,
iconTheme: IconThemeData(color: Colors.black),
2022-03-05 20:52:00 +00:00
primaryIconTheme: IconThemeData(color: Colors.red, opacity: 1.0, size: 50.0),
2022-05-12 12:54:19 +00:00
colorScheme: ColorScheme.light(
primary: Colors.black, secondary: Color.fromARGB(255, 163, 163, 163)),
accentColor: Color.fromRGBO(0, 0, 0, 0.6),
2022-03-05 20:52:00 +00:00
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
2022-04-08 05:59:20 +00:00
outlinedButtonTheme: buildOutlinedButtonThemeData(
2022-04-08 05:42:49 +00:00
bgDisabled: Colors.grey.shade500,
bgEnabled: Colors.black,
fgDisabled: Colors.white,
fgEnabled: Colors.white,
2022-03-05 20:52:00 +00:00
),
2022-04-08 05:59:20 +00:00
elevatedButtonTheme: buildElevatedButtonThemeData(
onPrimary: Colors.white, primary: Colors.black),
2022-05-16 20:38:11 +00:00
toggleableActiveColor: Colors.green[400],
2022-03-05 20:52:00 +00:00
scaffoldBackgroundColor: Colors.white,
bottomAppBarColor: Color.fromRGBO(196, 196, 196, 0.5),
2022-03-05 20:52:00 +00:00
backgroundColor: Colors.white,
2022-03-12 14:03:30 +00:00
appBarTheme: AppBarTheme().copyWith(
backgroundColor: Colors.white,
foregroundColor: Colors.black,
2022-03-12 14:03:30 +00:00
iconTheme: IconThemeData(color: Colors.black)),
//https://api.flutter.dev/flutter/material/TextTheme-class.html
2022-04-08 05:42:49 +00:00
textTheme: _buildTextTheme(Colors.black),
2022-03-05 20:52:00 +00:00
primaryTextTheme: TextTheme().copyWith(
bodyText2: TextStyle(color: Colors.yellow),
bodyText1: TextStyle(color: Colors.orange)),
cardColor: Color.fromRGBO(250, 250, 250, 1.0),
2022-03-05 20:52:00 +00:00
dialogTheme: DialogTheme().copyWith(
2022-03-27 04:42:26 +00:00
backgroundColor: Color.fromRGBO(250, 250, 250, 1.0), //
titleTextStyle: TextStyle(
color: Colors.black, fontSize: 32, fontWeight: FontWeight.w600),
contentTextStyle: TextStyle(
fontFamily: 'Inter-Medium',
color: Colors.black,
fontSize: 14,
fontWeight: FontWeight.w500),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))),
// textSelectionTheme: TextSelectionThemeData().copyWith(
// cursorColor: Colors.black.withOpacity(0.5),
// ),
2022-03-05 20:52:00 +00:00
inputDecorationTheme: InputDecorationTheme().copyWith(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromRGBO(45, 194, 98, 1.0),
),
),
),
checkboxTheme: CheckboxThemeData(
side: BorderSide(
color: Colors.black,
width: 2,
),
fillColor: MaterialStateProperty.resolveWith((states) {
2022-04-08 05:42:49 +00:00
return states.contains(MaterialState.selected)
? Colors.black
: Colors.white;
}),
checkColor: MaterialStateProperty.resolveWith((states) {
2022-04-08 05:42:49 +00:00
return states.contains(MaterialState.selected)
? Colors.white
: Colors.black;
}),
),
2022-03-05 20:52:00 +00:00
);
2022-03-05 10:42:55 +00:00
final darkThemeData = ThemeData(
2022-03-08 16:34:26 +00:00
fontFamily: 'Inter',
2021-11-24 07:26:07 +00:00
brightness: Brightness.dark,
primaryColorLight: Colors.white70,
2022-03-05 20:52:00 +00:00
iconTheme: IconThemeData(color: Colors.white),
primaryIconTheme: IconThemeData(color: Colors.red, opacity: 1.0, size: 50.0),
2021-11-24 07:26:07 +00:00
hintColor: Colors.grey,
bottomAppBarColor: Color.fromRGBO(196, 196, 196, 0.5),
2022-03-08 21:10:47 +00:00
colorScheme: ColorScheme.dark(primary: Colors.white, secondary: Color.fromARGB(255, 92, 92, 92)),
2022-05-12 12:54:19 +00:00
accentColor: Color.fromRGBO(255, 255, 255, 0.6),
2021-11-24 07:26:07 +00:00
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
buttonTheme: ButtonThemeData().copyWith(
buttonColor: Color.fromRGBO(45, 194, 98, 1.0),
),
2022-03-05 20:52:00 +00:00
// primaryColor: Colors.red,
2022-04-08 05:42:49 +00:00
textTheme: _buildTextTheme(Colors.white),
2021-11-24 07:26:07 +00:00
toggleableActiveColor: Colors.green[400],
2022-04-08 05:59:20 +00:00
outlinedButtonTheme: buildOutlinedButtonThemeData(
2022-04-08 05:42:49 +00:00
bgDisabled: Colors.grey.shade500,
bgEnabled: Colors.white,
fgDisabled: Colors.white,
fgEnabled: Colors.black),
2022-04-08 05:59:20 +00:00
elevatedButtonTheme: buildElevatedButtonThemeData(
onPrimary: Colors.black, primary: Colors.white),
2021-11-24 07:26:07 +00:00
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(
2022-03-27 04:42:26 +00:00
backgroundColor: Color.fromRGBO(10, 15, 15, 1.0),
titleTextStyle: TextStyle(
color: Colors.white, fontSize: 32, fontWeight: FontWeight.w600),
contentTextStyle: TextStyle(
fontFamily: 'Inter-Medium',
color: Colors.white,
fontSize: 14,
fontWeight: FontWeight.w500),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8))),
// textSelectionTheme: TextSelectionThemeData().copyWith(
// cursorColor: Colors.white.withOpacity(0.5),
// ),
2021-11-24 07:26:07 +00:00
inputDecorationTheme: InputDecorationTheme().copyWith(
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Color.fromRGBO(45, 194, 98, 1.0),
),
),
),
checkboxTheme: CheckboxThemeData(
side: BorderSide(
color: Colors.grey,
width: 2,
),
fillColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return Colors.grey;
} else {
return Colors.black;
}
}),
checkColor: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
return Colors.black;
} else {
return Colors.grey;
}
}),
),
2021-11-24 07:26:07 +00:00
);
2022-04-08 05:42:49 +00:00
TextTheme _buildTextTheme(Color textColor) {
return TextTheme().copyWith(
headline4: TextStyle(
color: textColor,
fontSize: 32,
fontWeight: FontWeight.w600,
fontFamily: 'Inter',
2022-04-08 05:42:49 +00:00
),
headline5: TextStyle(
color: textColor,
fontSize: 24,
fontWeight: FontWeight.w600,
fontFamily: 'Inter',
2022-04-08 05:42:49 +00:00
),
headline6: TextStyle(
color: textColor,
fontSize: 18,
fontFamily: 'Inter',
2022-04-08 05:42:49 +00:00
fontWeight: FontWeight.w600),
subtitle1: TextStyle(
color: textColor,
fontFamily: 'Inter',
2022-04-08 05:42:49 +00:00
fontSize: 16,
fontWeight: FontWeight.w500),
2022-05-03 06:29:09 +00:00
subtitle2: TextStyle(
color: textColor,
fontFamily: 'Inter',
2022-05-03 06:29:09 +00:00
fontSize: 14,
fontWeight: FontWeight.w500),
2022-04-08 05:42:49 +00:00
bodyText1: TextStyle(
fontFamily: 'Inter',
2022-04-08 05:42:49 +00:00
color: textColor,
fontSize: 16,
fontWeight: FontWeight.w400),
caption: TextStyle(
color: textColor.withOpacity(0.6),
fontSize: 14,
),
overline: TextStyle(
color: textColor.withOpacity(0.8),
fontSize: 12,
));
}
2021-11-24 07:26:07 +00:00
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() {
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
}
2022-03-12 14:03:30 +00:00
Widget debugBuild(BuildContext context) {
return MaterialApp(
title: "ente",
2022-04-05 11:14:03 +00:00
themeMode: ThemeMode.system,
2022-03-12 14:03:30 +00:00
theme: lightThemeData,
darkTheme: darkThemeData,
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,
],
);
}
2021-11-24 07:26:07 +00:00
@override
Widget build(BuildContext context) {
2022-03-12 14:03:30 +00:00
if (kDebugMode && Platform.isIOS) {
return debugBuild(context);
}
2022-03-09 05:57:27 +00:00
return AdaptiveTheme(
light: lightThemeData,
dark: darkThemeData,
initial: AdaptiveThemeMode.dark,
builder: (lightTheme, dartTheme) => MaterialApp(
title: "ente",
2022-03-12 14:03:30 +00:00
themeMode: ThemeMode.system,
2022-03-09 05:57:27 +00:00
theme: lightTheme,
darkTheme: dartTheme,
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,
],
));
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: false,
requiresCharging: false,
requiresStorageNotLow: false,
requiresDeviceIdle: false,
requiredNetworkType: NetworkType.NONE,
), (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
}