wonders/lib/logic/app_logic.dart

99 lines
3.0 KiB
Dart
Raw Normal View History

2022-08-29 20:38:28 -06:00
import 'dart:async';
2022-10-24 15:42:27 -06:00
import 'package:desktop_window/desktop_window.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/common_libs.dart';
import 'package:wonders/logic/common/platform_info.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/ui/common/utils/page_routes.dart';
class AppLogic {
/// Indicates to the rest of the app that bootstrap has not completed.
/// The router will use this to prevent redirects while bootstrapping.
bool isBootstrapComplete = false;
2022-12-19 13:43:30 -07:00
bool get isLandscapeEnabled =>
PlatformInfo.isDesktopOrWeb || deviceSize.shortestSide > 500;
/// Support portrait and landscape on desktop, web and tablets. Stick to portrait for phones.
/// A return value of null indicated both orientations are supported.
2022-12-19 13:43:30 -07:00
Axis? get supportedOrientations => isLandscapeEnabled ? null : Axis.vertical;
Size get deviceSize {
final w = WidgetsBinding.instance.platformDispatcher.views.first;
return w.physicalSize / w.devicePixelRatio;
}
2022-08-29 20:38:28 -06:00
/// Initialize the app and all main actors.
/// Loads settings, sets up services etc.
Future<void> bootstrap() async {
2022-12-19 13:43:30 -07:00
debugPrint(
'bootstrap app, deviceSize: $deviceSize, isTablet: $isLandscapeEnabled');
2022-08-29 20:38:28 -06:00
2022-10-24 15:42:27 -06:00
// Set min-sizes for desktop apps
if (PlatformInfo.isDesktop) {
await DesktopWindow.setMinWindowSize($styles.sizes.minAppSize);
}
2022-10-24 15:42:27 -06:00
2022-08-29 20:38:28 -06:00
// Load any bitmaps the views might need
await AppBitmaps.init();
// Set the initial supported orientations
setDeviceOrientation(supportedOrientations);
2022-08-29 20:38:28 -06:00
// Set preferred refresh rate to the max possible (the OS may ignore this)
if (PlatformInfo.isAndroid) {
2022-09-06 13:35:47 -06:00
await FlutterDisplayMode.setHighRefreshRate();
}
// Settings
await settingsLogic.load();
// Localizations
2022-08-29 20:38:28 -06:00
await localeLogic.load();
// Wonders Data
wondersLogic.init();
// Events
timelineLogic.init();
// Collectibles
2022-08-29 20:38:28 -06:00
await collectiblesLogic.load();
// Flag bootStrap as complete
2022-08-29 20:38:28 -06:00
isBootstrapComplete = true;
// Load initial view (replace empty initial view which is covered by a native splash screen)
bool showIntro = settingsLogic.hasCompletedOnboarding.value == false;
2022-12-01 23:12:15 -07:00
if (showIntro) {
2022-08-29 20:38:28 -06:00
appRouter.go(ScreenPaths.intro);
} else {
appRouter.go(ScreenPaths.home);
2022-08-29 20:38:28 -06:00
}
}
void setDeviceOrientation(Axis? axis) {
final orientations = <DeviceOrientation>[];
if (axis == null || axis == Axis.vertical) {
orientations.addAll([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
if (axis == null || axis == Axis.horizontal) {
orientations.addAll([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
SystemChrome.setPreferredOrientations(orientations);
}
2022-12-19 13:43:30 -07:00
Future<T?> showFullscreenDialogRoute<T>(BuildContext context, Widget child,
{bool transparent = false}) async {
2022-08-29 20:38:28 -06:00
return await Navigator.of(context).push<T>(
2022-12-19 13:43:30 -07:00
PageRoutes.dialog<T>(child, duration: $styles.times.pageTransition),
2022-08-29 20:38:28 -06:00
);
}
}