import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:material_ui/material_ui.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart' as riverpod; import 'package:fvp/fvp.dart' as fvp; import 'package:spark/src/core/auth/data/repositories/auth_repository.dart'; import 'package:spark/src/core/auth/data/repositories/auth_repository_impl.dart'; import 'package:spark/src/core/di/service_locator.dart'; import 'package:spark/src/core/notifications/push_notification_service.dart'; import 'package:spark/src/core/storage/cache/download_manager_interface.dart'; import 'package:spark/src/core/storage/storage.dart'; import 'package:spark/src/core/ui/theme/data/models/app_theme.dart'; import 'package:spark/src/core/utils/logging/logging.dart'; import 'package:spark/src/core/utils/logging/riverpod_logger.dart'; import 'package:spark/src/sprk_app.dart'; Future main() => runSparkApp(); Future runSparkApp({ LocalStorageInterface? preferencesStorage, LocalStorageInterface? secureStorage, riverpod.ProviderContainer? providerContainer, Future Function()? initializeDownloadManager, Future Function()? initializePushNotifications, }) async { final binding = WidgetsFlutterBinding.ensureInitialized(); binding.deferFirstFrame(); var didAllowFirstFrame = false; void allowFirstFrame() { if (didAllowFirstFrame) { return; } didAllowFirstFrame = true; binding.allowFirstFrame(); } try { await dotenv.load(); await SystemChrome.setPreferredOrientations([ DeviceOrientation.portraitUp, DeviceOrientation.portraitDown, ]); await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); SystemChrome.setSystemUIOverlayStyle(AppTheme.darkSystemUiStyle); fvp.registerWith(); await initServiceLocator( preferencesStorage: preferencesStorage, secureStorage: secureStorage, initializeDownloadManager: initializeDownloadManager, initializePushNotifications: initializePushNotifications, ); // Setup logging for production/debug _setupLogging(); // Initialize auth repository await _initializeAuth(); // Create a ProviderContainer with the Riverpod logger final container = providerContainer ?? riverpod.ProviderContainer(observers: [SparkRiverpodLogger()]); runApp( riverpod.UncontrolledProviderScope( container: container, child: SprkApp(onFirstRouteReady: allowFirstFrame), ), ); } catch (_) { allowFirstFrame(); rethrow; } } /// Setup logging framework based on environment void _setupLogging() { sl().setGlobalLogLevel( kDebugMode ? LogLevel.debug : LogLevel.warning, ); } /// Initialize auth repository and wait for it to be ready Future _initializeAuth() async { final logService = sl(); final logger = logService.getLogger('AppInitialization'); try { final authRepository = sl(); if (authRepository is AuthRepositoryImpl) { await authRepository.initializationComplete; } } catch (e) { logger.e('AuthRepository initialization failed', error: e); } }