2021-04-10 06:28:12 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
2021-07-25 07:00:38 +02:00
|
|
|
import 'package:nc_photos/app_localizations.dart';
|
2021-04-17 11:04:46 +02:00
|
|
|
import 'package:nc_photos/event/event.dart';
|
2021-06-23 10:15:25 +02:00
|
|
|
import 'package:nc_photos/language_util.dart' as language_util;
|
2021-04-17 11:04:46 +02:00
|
|
|
import 'package:nc_photos/pref.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/snack_bar_manager.dart';
|
|
|
|
import 'package:nc_photos/theme.dart';
|
2021-06-29 11:44:35 +02:00
|
|
|
import 'package:nc_photos/widget/album_dir_picker.dart';
|
2021-07-01 13:32:22 +02:00
|
|
|
import 'package:nc_photos/widget/album_importer.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/widget/album_viewer.dart';
|
2021-07-31 19:02:41 +02:00
|
|
|
import 'package:nc_photos/widget/archive_browser.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/widget/connect.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/widget/dynamic_album_viewer.dart';
|
2021-04-10 06:28:12 +02:00
|
|
|
import 'package:nc_photos/widget/home.dart';
|
|
|
|
import 'package:nc_photos/widget/root_picker.dart';
|
|
|
|
import 'package:nc_photos/widget/settings.dart';
|
|
|
|
import 'package:nc_photos/widget/setup.dart';
|
|
|
|
import 'package:nc_photos/widget/sign_in.dart';
|
|
|
|
import 'package:nc_photos/widget/splash.dart';
|
|
|
|
import 'package:nc_photos/widget/viewer.dart';
|
|
|
|
|
2021-04-17 11:04:46 +02:00
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
createState() => _MyAppState();
|
2021-07-11 06:48:50 +02:00
|
|
|
|
|
|
|
static RouteObserver get routeObserver => _routeObserver;
|
|
|
|
|
|
|
|
static final _routeObserver = RouteObserver<PageRoute>();
|
2021-04-17 11:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> implements SnackBarHandler {
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2021-04-10 06:28:12 +02:00
|
|
|
SnackBarManager().registerHandler(this);
|
2021-04-17 11:04:46 +02:00
|
|
|
_themeChangedListener =
|
|
|
|
AppEventListener<ThemeChangedEvent>(_onThemeChangedEvent)..begin();
|
2021-06-23 10:15:25 +02:00
|
|
|
_langChangedListener =
|
|
|
|
AppEventListener<LanguageChangedEvent>(_onLangChangedEvent)..begin();
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
2021-07-25 07:00:38 +02:00
|
|
|
onGenerateTitle: (context) => L10n.of(context).appTitle,
|
2021-04-10 06:28:12 +02:00
|
|
|
theme: _getLightTheme(),
|
2021-04-17 10:59:16 +02:00
|
|
|
darkTheme: _getDarkTheme(),
|
2021-07-23 22:05:57 +02:00
|
|
|
themeMode:
|
|
|
|
Pref.inst().isDarkThemeOr(false) ? ThemeMode.dark : ThemeMode.light,
|
2021-04-10 06:28:12 +02:00
|
|
|
initialRoute: Splash.routeName,
|
|
|
|
onGenerateRoute: _onGenerateRoute,
|
2021-07-11 06:48:50 +02:00
|
|
|
navigatorObservers: <NavigatorObserver>[MyApp.routeObserver],
|
2021-04-10 06:28:12 +02:00
|
|
|
scaffoldMessengerKey: _scaffoldMessengerKey,
|
2021-06-23 10:15:25 +02:00
|
|
|
locale: language_util.getSelectedLocale(context),
|
2021-04-10 06:28:12 +02:00
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:04:46 +02:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
SnackBarManager().unregisterHandler(this);
|
|
|
|
_themeChangedListener.end();
|
2021-06-23 10:15:25 +02:00
|
|
|
_langChangedListener.end();
|
2021-04-17 11:04:46 +02:00
|
|
|
}
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
@override
|
|
|
|
showSnackBar(SnackBar snackBar) =>
|
|
|
|
_scaffoldMessengerKey.currentState?.showSnackBar(snackBar);
|
|
|
|
|
|
|
|
ThemeData _getLightTheme() => ThemeData(
|
|
|
|
brightness: Brightness.light,
|
|
|
|
primarySwatch: AppTheme.primarySwatchLight,
|
|
|
|
);
|
|
|
|
|
2021-04-17 10:59:16 +02:00
|
|
|
ThemeData _getDarkTheme() => ThemeData(
|
|
|
|
brightness: Brightness.dark,
|
|
|
|
primarySwatch: AppTheme.primarySwatchDark,
|
|
|
|
);
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
Map<String, WidgetBuilder> _getRouter() => {
|
|
|
|
Setup.routeName: (context) => Setup(),
|
|
|
|
SignIn.routeName: (context) => SignIn(),
|
|
|
|
Splash.routeName: (context) => Splash(),
|
|
|
|
};
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _onGenerateRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
_log.info("[_onGenerateRoute] Route: ${settings.name}");
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? route;
|
2021-04-10 06:28:12 +02:00
|
|
|
route ??= _handleBasicRoute(settings);
|
|
|
|
route ??= _handleViewerRoute(settings);
|
|
|
|
route ??= _handleConnectRoute(settings);
|
|
|
|
route ??= _handleHomeRoute(settings);
|
|
|
|
route ??= _handleRootPickerRoute(settings);
|
|
|
|
route ??= _handleAlbumViewerRoute(settings);
|
|
|
|
route ??= _handleSettingsRoute(settings);
|
2021-07-31 19:02:41 +02:00
|
|
|
route ??= _handleArchiveBrowserRoute(settings);
|
2021-06-26 16:28:21 +02:00
|
|
|
route ??= _handleDynamicAlbumViewerRoute(settings);
|
2021-06-29 11:44:35 +02:00
|
|
|
route ??= _handleAlbumDirPickerRoute(settings);
|
2021-07-01 13:32:22 +02:00
|
|
|
route ??= _handleAlbumImporterRoute(settings);
|
2021-04-10 06:28:12 +02:00
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2021-04-17 11:04:46 +02:00
|
|
|
void _onThemeChangedEvent(ThemeChangedEvent ev) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2021-06-23 10:15:25 +02:00
|
|
|
void _onLangChangedEvent(LanguageChangedEvent ev) {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleBasicRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
for (final e in _getRouter().entries) {
|
|
|
|
if (e.key == settings.name) {
|
|
|
|
return MaterialPageRoute(
|
|
|
|
builder: e.value,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleViewerRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == Viewer.routeName && settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as ViewerArguments;
|
|
|
|
return Viewer.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleViewerRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleConnectRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == Connect.routeName && settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as ConnectArguments;
|
|
|
|
return Connect.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleConnectRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleHomeRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == Home.routeName && settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as HomeArguments;
|
|
|
|
return Home.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleHomeRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleRootPickerRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == RootPicker.routeName && settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as RootPickerArguments;
|
|
|
|
return RootPicker.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleRootPickerRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleAlbumViewerRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == AlbumViewer.routeName &&
|
|
|
|
settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as AlbumViewerArguments;
|
|
|
|
return AlbumViewer.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleAlbumViewerRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleSettingsRoute(RouteSettings settings) {
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == Settings.routeName && settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as SettingsArguments;
|
|
|
|
return Settings.buildRoute(args);
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleSettingsRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-31 19:02:41 +02:00
|
|
|
Route<dynamic>? _handleArchiveBrowserRoute(RouteSettings settings) {
|
2021-05-28 21:44:09 +02:00
|
|
|
try {
|
2021-07-31 19:02:41 +02:00
|
|
|
if (settings.name == ArchiveBrowser.routeName &&
|
2021-05-28 21:44:09 +02:00
|
|
|
settings.arguments != null) {
|
2021-07-31 19:02:41 +02:00
|
|
|
final args = settings.arguments as ArchiveBrowserArguments;
|
|
|
|
return ArchiveBrowser.buildRoute(args);
|
2021-05-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-07-31 19:02:41 +02:00
|
|
|
_log.severe(
|
|
|
|
"[_handleArchiveBrowserRoute] Failed while handling route", e);
|
2021-05-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleDynamicAlbumViewerRoute(RouteSettings settings) {
|
2021-06-26 16:28:21 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == DynamicAlbumViewer.routeName &&
|
|
|
|
settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as DynamicAlbumViewerArguments;
|
|
|
|
return DynamicAlbumViewer.buildRoute(args);
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe(
|
|
|
|
"[_handleDynamicAlbumViewerRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleAlbumDirPickerRoute(RouteSettings settings) {
|
2021-06-29 11:44:35 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == AlbumDirPicker.routeName &&
|
|
|
|
settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as AlbumDirPickerArguments;
|
|
|
|
return AlbumDirPicker.buildRoute(args);
|
2021-06-29 11:44:35 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe(
|
|
|
|
"[_handleAlbumDirPickerRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
Route<dynamic>? _handleAlbumImporterRoute(RouteSettings settings) {
|
2021-07-01 13:32:22 +02:00
|
|
|
try {
|
|
|
|
if (settings.name == AlbumImporter.routeName &&
|
|
|
|
settings.arguments != null) {
|
2021-07-23 22:05:57 +02:00
|
|
|
final args = settings.arguments as AlbumImporterArguments;
|
|
|
|
return AlbumImporter.buildRoute(args);
|
2021-07-01 13:32:22 +02:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
_log.severe("[_handleAlbumImporterRoute] Failed while handling route", e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-04-10 06:28:12 +02:00
|
|
|
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
|
|
|
|
2021-07-23 22:05:57 +02:00
|
|
|
late AppEventListener<ThemeChangedEvent> _themeChangedListener;
|
|
|
|
late AppEventListener<LanguageChangedEvent> _langChangedListener;
|
2021-04-17 11:04:46 +02:00
|
|
|
|
|
|
|
static final _log = Logger("widget.my_app.MyAppState");
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|