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 ' ;
2022-09-01 09:17:35 -06:00
import ' package:flutter_displaymode/flutter_displaymode.dart ' ;
2022-08-29 20:38:28 -06:00
import ' package:wonders/common_libs.dart ' ;
2022-11-05 02:06:51 -06:00
import ' package:wonders/logic/common/platform_info.dart ' ;
2023-02-13 23:24:07 -07:00
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 {
/// 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 ;
2023-02-13 23:24:07 -07:00
/// 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-11-05 02:06:51 -06:00
}
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 {
2023-02-13 23:24:07 -07:00
debugPrint ( ' bootstrap start... ' ) ;
2022-10-24 15:42:27 -06:00
// Set min-sizes for desktop apps
2022-11-05 02:06:51 -06:00
if ( PlatformInfo . isDesktop ) {
await DesktopWindow . setMinWindowSize ( $styles . sizes . minAppSize ) ;
}
2023-02-18 19:20:11 -07:00
2022-08-29 20:38:28 -06:00
// Load any bitmaps the views might need
await AppBitmaps . init ( ) ;
2022-09-01 20:46:15 -06:00
// Set preferred refresh rate to the max possible (the OS may ignore this)
2022-11-05 02:06:51 -06:00
if ( PlatformInfo . isAndroid ) {
2022-09-06 13:35:47 -06:00
await FlutterDisplayMode . setHighRefreshRate ( ) ;
}
2022-09-01 09:17:35 -06:00
2022-09-08 12:14:28 -06:00
// Settings
await settingsLogic . load ( ) ;
2022-09-01 20:46:15 -06:00
// Localizations
2022-08-29 20:38:28 -06:00
await localeLogic . load ( ) ;
2022-09-01 20:46:15 -06:00
2022-09-08 12:14:28 -06:00
// Wonders Data
2022-09-01 14:45:55 -06:00
wondersLogic . init ( ) ;
2022-09-08 12:14:28 -06:00
// Events
2022-09-01 14:45:55 -06:00
timelineLogic . init ( ) ;
2022-09-01 20:46:15 -06:00
// Collectibles
2022-08-29 20:38:28 -06:00
await collectiblesLogic . load ( ) ;
2022-09-08 12:14:28 -06:00
// Flag bootStrap as complete
2022-08-29 20:38:28 -06:00
isBootstrapComplete = true ;
2022-09-08 12:14:28 -06:00
// Load initial view (replace empty initial view which is covered by a native splash screen)
2022-09-01 20:46:15 -06:00
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 ) ;
2022-09-01 20:46:15 -06:00
} else {
appRouter . go ( ScreenPaths . home ) ;
2022-08-29 20:38:28 -06:00
}
}
2023-02-13 23:24:07 -07: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 size ) {
/// Disable landscape layout on smaller form factors
bool isSmall = size . shortestSide < 500 & & size ! = Size . zero ;
supportedOrientations = isSmall ? [ Axis . vertical ] : [ Axis . vertical , Axis . horizontal ] ;
_updateSystemOrientation ( ) ;
}
2023-04-27 11:22:16 -06:00
bool shouldUseNavRail ( Size size ) = > size . width > size . height & & size . height > 250 ;
2023-02-13 23:24:07 -07:00
/// Enable landscape, portrait or both. Views can call this method to override the default settings.
/// For example, the [FullscreenVideoViewer] always wants to enable both landscape and portrait.
/// If a view overrides this, it is responsible for setting it back to [supportedOrientations] when disposed.
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 > [ ] ;
2023-02-13 23:24:07 -07:00
if ( axisList . contains ( Axis . vertical ) ) {
2022-08-29 20:38:28 -06:00
orientations . addAll ( [
DeviceOrientation . portraitUp ,
DeviceOrientation . portraitDown ,
] ) ;
}
2023-02-13 23:24:07 -07:00
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 ( ) ;
}
}