ente/lib/app.dart

162 lines
5 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';
import 'package:media_extension/media_extension_action_types.dart';
2022-04-08 05:59:20 +00:00
import 'package:photos/ente_theme_data.dart';
import "package:photos/generated/l10n.dart";
import "package:photos/l10n/l10n.dart";
2021-11-24 07:26:07 +00:00
import 'package:photos/services/app_lifecycle_service.dart';
import 'package:photos/services/sync_service.dart';
2023-06-06 09:57:17 +00:00
import 'package:photos/ui/tabs/home_widget.dart';
import "package:photos/ui/viewer/actions/file_viewer.dart";
import "package:photos/utils/intent_util.dart";
2021-11-24 07:26:07 +00:00
class EnteApp extends StatefulWidget {
final Future<void> Function(String) runBackgroundTask;
final Future<void> Function(String) killBackgroundTask;
final AdaptiveThemeMode? savedThemeMode;
2023-04-10 11:26:44 +00:00
final Locale locale;
const EnteApp(
this.runBackgroundTask,
this.killBackgroundTask,
2023-04-10 11:26:44 +00:00
this.locale,
this.savedThemeMode, {
Key? key,
}) : super(key: key);
2021-11-24 07:26:07 +00:00
2023-04-10 11:26:44 +00:00
static void setLocale(BuildContext context, Locale newLocale) {
final state = context.findAncestorStateOfType<_EnteAppState>()!;
2023-04-10 11:26:44 +00:00
state.setLocale(newLocale);
}
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");
2023-04-10 11:26:44 +00:00
late Locale locale;
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();
2023-04-10 11:26:44 +00:00
locale = widget.locale;
setupIntentAction();
2021-11-24 07:26:07 +00:00
WidgetsBinding.instance.addObserver(this);
}
2023-04-10 11:26:44 +00:00
setLocale(Locale newLocale) {
setState(() {
locale = newLocale;
});
}
void setupIntentAction() async {
final mediaExtentionAction = Platform.isAndroid
? await initIntentAction()
: MediaExtentionAction(action: IntentAction.main);
AppLifecycleService.instance.setMediaExtensionAction(mediaExtentionAction);
if (mediaExtentionAction.action == IntentAction.main) {
_configureBackgroundFetch();
}
2021-11-24 07:26:07 +00:00
}
@override
Widget build(BuildContext context) {
if (Platform.isAndroid || kDebugMode) {
return AdaptiveTheme(
light: lightThemeData,
dark: darkThemeData,
initial: widget.savedThemeMode ?? AdaptiveThemeMode.system,
builder: (lightTheme, dartTheme) => MaterialApp(
title: "ente",
themeMode: ThemeMode.system,
theme: lightTheme,
darkTheme: dartTheme,
home: AppLifecycleService.instance.mediaExtensionAction.action ==
IntentAction.view
? const FileViewer()
: const HomeWidget(),
debugShowCheckedModeBanner: false,
builder: EasyLoading.init(),
2023-04-10 11:26:44 +00:00
locale: locale,
supportedLocales: appSupportedLocales,
2023-04-04 14:28:20 +00:00
localeListResolutionCallback: localResolutionCallBack,
localizationsDelegates: const [
...AppLocalizations.localizationsDelegates,
S.delegate
],
),
2022-06-01 08:39:31 +00:00
);
} else {
return MaterialApp(
2023-04-17 07:14:37 +00:00
title: "ente",
themeMode: ThemeMode.system,
theme: lightThemeData,
darkTheme: darkThemeData,
home: const HomeWidget(),
debugShowCheckedModeBanner: false,
builder: EasyLoading.init(),
2023-04-10 11:26:44 +00:00
locale: locale,
2023-04-17 07:14:37 +00:00
supportedLocales: appSupportedLocales,
localeListResolutionCallback: localResolutionCallBack,
localizationsDelegates: const [
...AppLocalizations.localizationsDelegates,
S.delegate
],
);
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
}