selfprivacy.org.app/lib/main.dart

143 lines
4.2 KiB
Dart
Raw Normal View History

2022-02-10 09:50:37 +00:00
import 'package:easy_localization/easy_localization.dart';
2020-11-29 20:07:46 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2021-01-13 16:45:46 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:selfprivacy/config/app_controller/inherited_app_controller.dart';
2024-01-31 05:14:23 +00:00
import 'package:selfprivacy/config/bloc_config.dart';
import 'package:selfprivacy/config/bloc_observer.dart';
2022-05-03 10:45:10 +00:00
import 'package:selfprivacy/config/brand_colors.dart';
2024-01-31 05:14:23 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/config/hive_config.dart';
2024-01-31 05:14:23 +00:00
import 'package:selfprivacy/config/localization.dart';
import 'package:selfprivacy/config/preferences_repository/datasources/preferences_hive_datasource.dart';
import 'package:selfprivacy/config/preferences_repository/inherited_preferences_repository.dart';
import 'package:selfprivacy/ui/pages/errors/failed_to_init_secure_storage.dart';
import 'package:selfprivacy/ui/router/router.dart';
2022-01-25 17:00:47 +00:00
import 'package:timezone/data/latest.dart' as tz;
2021-01-19 12:05:40 +00:00
2021-01-06 17:35:57 +00:00
void main() async {
// await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
2022-04-29 10:45:15 +00:00
// try {
// /// Wakelock support for Linux
// /// desktop is not yet implemented
// await Wakelock.enable();
// } on PlatformException catch (e) {
// print(e);
// }
2022-04-29 10:45:15 +00:00
try {
await Future.wait(
<Future<void>>[
HiveConfig.init(),
EasyLocalization.ensureInitialized(),
],
);
await getItSetup();
} on PlatformException catch (e) {
runApp(
FailedToInitSecureStorageScreen(e: e),
);
}
2020-12-01 12:26:29 +00:00
tz.initializeTimeZones();
2022-05-03 10:45:10 +00:00
Bloc.observer = SimpleBlocObserver();
runApp(
Localization(
child: InheritedPreferencesRepository(
dataSource: PreferencesHiveDataSource(),
child: const InheritedAppController(
child: AppBuilder(),
),
),
),
2022-01-25 17:00:47 +00:00
);
2020-11-29 20:07:46 +00:00
}
class AppBuilder extends StatelessWidget {
const AppBuilder({super.key});
2022-05-03 10:45:10 +00:00
@override
Widget build(final BuildContext context) {
final appController = InheritedAppController.of(context);
2022-05-03 10:45:10 +00:00
if (appController.loaded) {
return const SelfprivacyApp();
}
return const SplashScreen();
}
}
/// Widget to be shown
/// until essential app initialization is completed
class SplashScreen extends StatelessWidget {
const SplashScreen({super.key});
2020-11-29 20:07:46 +00:00
@override
Widget build(final BuildContext context) => const ColoredBox(
color: Colors.white,
child: Center(
child: CircularProgressIndicator.adaptive(
valueColor: AlwaysStoppedAnimation(BrandColors.primary),
2022-03-23 14:07:52 +00:00
),
),
);
2020-11-29 20:07:46 +00:00
}
class SelfprivacyApp extends StatefulWidget {
const SelfprivacyApp({
super.key,
});
@override
State<SelfprivacyApp> createState() => _SelfprivacyAppState();
}
class _SelfprivacyAppState extends State<SelfprivacyApp> {
final appKey = UniqueKey();
final _appRouter = RootRouter(getIt.get<NavigationService>().navigatorKey);
@override
Widget build(final BuildContext context) {
final appController = InheritedAppController.of(context);
return BlocAndProviderConfig(
child: MaterialApp.router(
key: appKey,
title: 'SelfPrivacy',
// routing
routeInformationParser: _appRouter.defaultRouteParser(),
routerDelegate: _appRouter.delegate(),
scaffoldMessengerKey:
getIt.get<NavigationService>().scaffoldMessengerKey,
// localization settings
locale: context.locale,
supportedLocales: context.supportedLocales,
localizationsDelegates: context.localizationDelegates,
// theme settings
themeMode: appController.themeMode,
theme: appController.lightTheme,
darkTheme: appController.darkTheme,
// other preferences
debugShowCheckedModeBanner: false,
scrollBehavior:
const MaterialScrollBehavior().copyWith(scrollbars: false),
builder: _builder,
),
);
}
Widget _builder(final BuildContext context, final Widget? widget) {
Widget error = const Center(child: Text('...rendering error...'));
if (widget is Scaffold || widget is Navigator) {
error = Scaffold(body: error);
}
ErrorWidget.builder = (final FlutterErrorDetails errorDetails) => error;
return widget ?? error;
}
}