import 'dart:io'; // import 'package:firebase_core/firebase_core.dart'; // import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:appsflyer_sdk/appsflyer_sdk.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import 'package:uae_stat/config/injector.dart'; import 'package:uae_stat/config/my_router.dart'; import 'package:uae_stat/config/theme/theme_provider.dart'; import 'package:uae_stat/presentation/components/indicators/locale_provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // void main() async { // WidgetsFlutterBinding.ensureInitialized(); // // await (await SharedPreferences.getInstance()).clear(); // configureDependencies(); // runApp( // const ProviderScope( // child: MainApp(), // ), // ); // } // // class MainApp extends ConsumerWidget { // const MainApp({super.key}); // // @override // Widget build(BuildContext context, WidgetRef ref) { // final locale = ref.watch(localeProvider); // // return MaterialApp.router( // debugShowCheckedModeBanner: false, // routerConfig: router, // locale: locale, // localizationsDelegates: AppLocalizations.localizationsDelegates, // supportedLocales: AppLocalizations.supportedLocales,// Use the GoRouter instance from app_router.dart // ); // } // } // void main() async { // WidgetsFlutterBinding.ensureInitialized(); // // await (await SharedPreferences.getInstance()).clear(); // configureDependencies(); // runApp( // const ProviderScope( // child: MainApp(), // ), // ); // } // // class MainApp extends ConsumerWidget { // const MainApp({super.key}); // // @override // Widget build(BuildContext context, WidgetRef ref) { // final locale = ref.watch(localeProvider); // // // Listen to connectivity changes at the app level // ref.listen(connectivityProvider, (previous, hasInternet) { // if (previous != null && !hasInternet) { // // When no internet, navigate to the NoInternetScreen // print('No Internet Connection'); // context.go('/internetcheck'); // // } else if (previous != null && hasInternet) { // // If internet is back, navigate back to the HomeScreen // print("You have an Internet connection"); // context.go('/myhomepage'); // // } // }); // // return MaterialApp.router( // debugShowCheckedModeBanner: false, // routerConfig: router, // Assuming you are using GoRouter // locale: locale, // localizationsDelegates: AppLocalizations.localizationsDelegates, // supportedLocales: AppLocalizations.supportedLocales, // ); // } // } // Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // // Handle background messages // print("Handling background message: ${message.messageId}"); // } Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); print("Handling background message: ${message.messageId}"); } void main() async { WidgetsFlutterBinding.ensureInitialized(); if(kIsWeb){ await Firebase.initializeApp( options: const FirebaseOptions( apiKey: 'AIzaSyB-QQbnq366f7rrKLVVFJKnSZgR9LcDWFI', appId: '1:62132243005:android:18b7ca94608afaeb7c7854', messagingSenderId: '62132243005', projectId: 'fcsc-a161c', ), ); } else { await Firebase.initializeApp(); } FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); // await triggerLocalNetworkAccess(); // if (Firebase.apps.isEmpty) { // await Firebase.initializeApp( // options: const FirebaseOptions( // apiKey: 'AIzaSyB-QQbnq366f7rrKLVVFJKnSZgR9LcDWFI', // appId: '1:62132243005:android:18b7ca94608afaeb7c7854', // messagingSenderId: '62132243005', // projectId: 'fcsc-a161c', // ), // ); // } // // FirebaseMessaging messaging = FirebaseMessaging.instance; // FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); FirebaseMessaging messaging = FirebaseMessaging.instance; // NotificationSettings settings = await messaging.requestPermission(); NotificationSettings settings = await FirebaseMessaging.instance.requestPermission( alert: true, badge: true, sound: true, ); print('User granted permission: ${settings.authorizationStatus}'); FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Foreground Message: ${message.notification?.title}'); }); FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { print('User tapped on notification'); }); // final themeProvider = ThemeProvider(); // themeProvider.setUserId("USER_ID_FROM_LOGIN"); // set this properly // await themeProvider.loadThemeFromPocketBase(); // // Request permission for notifications // NotificationSettings settings = await messaging.requestPermission( // alert: true, // badge: true, // sound: true, // ); // // print('User granted permission: ${settings.authorizationStatus}'); configureDependencies(); runApp( const ProviderScope( child: MainApp(), ), ); } // Future triggerLocalNetworkAccess() async { // try { // await Socket.connect('127.0.0.1', 9).timeout(const Duration(seconds: 1)); // } catch (e) { // // Ignore errors – this is just to trigger the local network prompt // } // } class MainApp extends ConsumerStatefulWidget { const MainApp({super.key}); @override ConsumerState createState() => _MainAppState(); } class _MainAppState extends ConsumerState { late AppsflyerSdk appsflyerSdk; @override void initState() { super.initState(); appsflyerSdk=AppsflyerSdk( AppsFlyerOptions( afDevKey: '7TwqHnPoWcAUuiFGpuCVtF', appId: 'ae.gov.fcsc.frontend', showDebug: true, timeToWaitForATTUserAuthorization: 0, ), ); appsflyerSdk.initSdk( registerOnDeepLinkingCallback: true, ); appsflyerSdk.onDeepLinking((DeepLinkResult result){ final status=result.status; final deeplinkData =result.deepLink; if((status==Status.FOUND) && deeplinkData !=null){ final data=deeplinkData.clickEvent; final deepLinkValue = data['deep_link_sub1']as String?; }else{ print( "Deeplink status is ${status.toShortString()} OR data is null"); } }); } Widget build(BuildContext context) { final locale = ref.watch(localeProvider); final connectivityProvider = Provider((ref) { return true; // Replace with actual logic }); ref.listen(connectivityProvider, (previous, hasInternet) { print('Previous Internet Status: $previous'); print('Current Internet Status: $hasInternet'); if (previous != null && hasInternet != previous) { handleConnectivityChange(context, hasInternet); } }); final themeMode = ref.watch(themeProvider); return MaterialApp.router( debugShowCheckedModeBanner: false, routerConfig: router, locale: locale, theme: ThemeData.light(), darkTheme: ThemeData.dark(), themeMode: themeMode, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, ); } void handleConnectivityChange(BuildContext context, bool hasInternet) { //final currentPath = GoRouter.of(context).location; final currentPath = GoRouter.of(context).routerDelegate.currentConfiguration.fullPath; if (!hasInternet && currentPath != '/internetcheck') { print("Status : Not connected"); context.go('/internetcheck'); } else if (hasInternet && currentPath != '/login') { print("Status : Internet connected"); context.go('/login'); } } }