wonders/lib/logic/app_logic.dart

134 lines
4.6 KiB
Dart
Raw Normal View History

2022-08-29 20:38:28 -06:00
import 'dart:async';
import 'dart:ui';
2022-08-29 20:38:28 -06:00
2022-10-24 15:42:27 -06:00
import 'package:desktop_window/desktop_window.dart';
import 'package:flutter/foundation.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';
import 'package:wonders/ui/common/modals/fullscreen_video_viewer.dart';
2022-08-29 20:38:28 -06:00
import 'package:wonders/ui/common/utils/page_routes.dart';
class AppLogic {
Size _appSize = Size.zero;
2022-08-29 20:38:28 -06:00
/// 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;
/// Indicates which orientations the app will allow be default. Affects Android/iOS devices only.
/// Defaults to both landscape (hz) and portrait (vt)
List<Axis> supportedOrientations = [Axis.vertical, Axis.horizontal];
/// Allow a view to override the currently supported orientations. For example, [FullscreenVideoViewer] always wants to enable both landscape and portrait.
/// If a view sets this override, they are responsible for setting it back to null when finished.
List<Axis>? _supportedOrientationsOverride;
set supportedOrientationsOverride(List<Axis>? value) {
if (_supportedOrientationsOverride != value) {
_supportedOrientationsOverride = value;
_updateSystemOrientation();
}
}
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 {
debugPrint('bootstrap start...');
2022-10-24 15:42:27 -06:00
// Set min-sizes for desktop apps
if (PlatformInfo.isDesktop) {
await DesktopWindow.setMinWindowSize($styles.sizes.minAppSize);
}
2023-02-18 19:20:11 -07:00
if (kIsWeb) {
// SB: This is intentionally not a debugPrint, as it's a message for users who open the console on web.
print(
2023-12-05 11:05:46 -07:00
'''Thanks for checking out Wonderous on the web!
If you encounter any issues please report them at https://github.com/gskinnerTeam/flutter-wonderous-app/issues.''',
);
// Required on web to automatically enable accessibility features
WidgetsFlutterBinding.ensureInitialized().ensureSemantics();
}
2022-08-29 20:38:28 -06:00
// Load any bitmaps the views might need
await AppBitmaps.init();
// Set preferred refresh rate to the max possible (the OS may ignore this)
if (!kIsWeb && 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
collectiblesLogic.init();
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
}
}
Future<T?> showFullscreenDialogRoute<T>(BuildContext context, Widget child, {bool transparent = false}) async {
return await Navigator.of(context).push<T>(
PageRoutes.dialog<T>(child, duration: $styles.times.pageTransition),
);
}
/// Called from the UI layer once a MediaQuery has been obtained
void handleAppSizeChanged(Size appSize) {
/// Disable landscape layout on smaller form factors
bool isSmall = display.size.shortestSide / display.devicePixelRatio < 600;
supportedOrientations = isSmall ? [Axis.vertical] : [Axis.vertical, Axis.horizontal];
_updateSystemOrientation();
_appSize = appSize;
}
Display get display => PlatformDispatcher.instance.displays.first;
bool shouldUseNavRail() => _appSize.width > _appSize.height && _appSize.height > 250;
void _updateSystemOrientation() {
final axisList = _supportedOrientationsOverride ?? supportedOrientations;
2023-04-18 10:41:46 -06:00
//debugPrint('updateDeviceOrientation, supportedAxis: $axisList');
2022-08-29 20:38:28 -06:00
final orientations = <DeviceOrientation>[];
if (axisList.contains(Axis.vertical)) {
2022-08-29 20:38:28 -06:00
orientations.addAll([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
if (axisList.contains(Axis.horizontal)) {
2022-08-29 20:38:28 -06:00
orientations.addAll([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
SystemChrome.setPreferredOrientations(orientations);
}
}
2023-02-18 19:20:11 -07:00
class AppImageCache extends WidgetsFlutterBinding {
@override
ImageCache createImageCache() {
this.imageCache.maximumSizeBytes = 250 << 20; // 250mb
return super.createImageCache();
}
}