209 lines
6.7 KiB
Dart
209 lines
6.7 KiB
Dart
import 'dart:io';
|
||
// import 'package:firebase_core/firebase_core.dart';
|
||
// import 'package:firebase_messaging/firebase_messaging.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/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<bool>(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<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||
// // Handle background messages
|
||
// print("Handling background message: ${message.messageId}");
|
||
// }
|
||
|
||
Future<void> _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');
|
||
});
|
||
|
||
|
||
//
|
||
// 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<void> 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 ConsumerWidget {
|
||
const MainApp({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context, WidgetRef ref) {
|
||
final locale = ref.watch(localeProvider);
|
||
|
||
final connectivityProvider = Provider<bool>((ref) {
|
||
return true; // Replace with actual logic
|
||
});
|
||
|
||
ref.listen<bool>(connectivityProvider, (previous, hasInternet) {
|
||
print('Previous Internet Status: $previous');
|
||
print('Current Internet Status: $hasInternet');
|
||
if (previous != null && hasInternet != previous) {
|
||
handleConnectivityChange(context, hasInternet);
|
||
}
|
||
});
|
||
|
||
return MaterialApp.router(
|
||
debugShowCheckedModeBanner: false,
|
||
routerConfig: router,
|
||
locale: locale,
|
||
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');
|
||
}
|
||
}
|
||
}
|