From a9d919fef7e013c3890936ff7ed75ef48513e62c Mon Sep 17 00:00:00 2001 From: VINISTAN Date: Thu, 6 Feb 2025 12:50:28 +0530 Subject: [PATCH] Internet connection setup --- lib/config/my_router.dart | 2 +- .../InternetConnectivityService.dart | 110 +++++++++--- lib/main.dart | 170 ++++++++++-------- macos/Flutter/GeneratedPluginRegistrant.swift | 2 +- pubspec.lock | 8 +- pubspec.yaml | 2 +- 6 files changed, 192 insertions(+), 102 deletions(-) diff --git a/lib/config/my_router.dart b/lib/config/my_router.dart index 6449fff3..8273d3ab 100644 --- a/lib/config/my_router.dart +++ b/lib/config/my_router.dart @@ -328,7 +328,7 @@ final GoRouter router = GoRouter( GoRoute( path: '/', //builder: (context, state) => LoginRoute(), - builder: (context, state) => LoginRoute(), + builder: (context, state) => MyHomePage(), ), GoRoute( path: '/internetcheck', diff --git a/lib/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart b/lib/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart index d175feb2..54f62dd9 100644 --- a/lib/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart +++ b/lib/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart @@ -1,32 +1,98 @@ +// // import 'package:flutter_riverpod/flutter_riverpod.dart'; +// // import 'package:connectivity_plus/connectivity_plus.dart'; +// // +// // class ConnectivityService { +// // final Connectivity _connectivity = Connectivity(); +// // +// // // This will hold the connectivity status +// // Stream> get connectivityStream => +// // _connectivity.onConnectivityChanged; +// // } +// // +// // // The provider that will provide the connectivity service +// // final connectivityServiceProvider = Provider((ref) { +// // return ConnectivityService(); +// // }); +// // +// // // StateNotifier to manage connectivity state +// // class ConnectivityNotifier extends StateNotifier { +// // final ConnectivityService _connectivityService; +// // +// // ConnectivityNotifier(this._connectivityService) : super(true) { +// // _connectivityService.connectivityStream.listen((result) { +// // state = result != ConnectivityResult.none; +// // }); +// // } +// // } +// // +// // // The provider for the connectivity state +// // final connectivityProvider = StateNotifierProvider((ref) { +// // final connectivityService = ref.read(connectivityServiceProvider); +// // return ConnectivityNotifier(connectivityService); +// // }); +// +// +// import 'package:flutter_riverpod/flutter_riverpod.dart'; +// import 'package:connectivity_plus/connectivity_plus.dart'; +// +// final connectivityProvider = StateNotifierProvider((ref) { +// return ConnectivityNotifier(); +// }); +// +// class ConnectivityNotifier extends StateNotifier { +// ConnectivityNotifier() : super(true) { +// _checkInitialConnection(); +// _listenToConnectivityChanges(); +// } +// +// void _checkInitialConnection() async { +// final result = await Connectivity().checkConnectivity(); +// state = _hasInternet(result); +// } +// +// void _listenToConnectivityChanges() { +// Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { +// state = _hasInternet(result); +// }); +// } +// +// bool _hasInternet(ConnectivityResult result) { +// return result == ConnectivityResult.mobile || result == ConnectivityResult.wifi; +// } +// } +// +// + import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:connectivity_plus/connectivity_plus.dart'; -class ConnectivityService { - final Connectivity _connectivity = Connectivity(); - - // This will hold the connectivity status - Stream> get connectivityStream => - _connectivity.onConnectivityChanged; -} - -// The provider that will provide the connectivity service -final connectivityServiceProvider = Provider((ref) { - return ConnectivityService(); +final connectivityProvider = StateNotifierProvider((ref) { + return ConnectivityNotifier(); }); -// StateNotifier to manage connectivity state class ConnectivityNotifier extends StateNotifier { - final ConnectivityService _connectivityService; + ConnectivityNotifier() : super(true) { + _checkInitialConnection(); + _listenToConnectivityChanges(); + } - ConnectivityNotifier(this._connectivityService) : super(true) { - _connectivityService.connectivityStream.listen((result) { - state = result != ConnectivityResult.none; + void _checkInitialConnection() async { + final result = await Connectivity().checkConnectivity(); + state = _hasInternet(result); + } + + void _listenToConnectivityChanges() { + // For mobile platforms, use the connectivity plugin + + // Listen for connectivity changes on web + Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { + state = _hasInternet(result); }); } -} -// The provider for the connectivity state -final connectivityProvider = StateNotifierProvider((ref) { - final connectivityService = ref.read(connectivityServiceProvider); - return ConnectivityNotifier(connectivityService); -}); + + + bool _hasInternet(ConnectivityResult result) { + return result == ConnectivityResult.mobile || result == ConnectivityResult.wifi; + } +} diff --git a/lib/main.dart b/lib/main.dart index 4500ffd5..44b819a5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,20 +2,95 @@ import 'package:external_repos/external_repos.dart'; import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.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/my_theme.dart'; import 'package:uae_stat/config/old_theme.dart'; import 'package:uae_stat/domain/entities/language_locale.dart'; import 'package:uae_stat/domain/use_cases/auth_use_case.dart'; +import 'package:uae_stat/infrastructure/services/InternetConnectivity_Service/InternetConnectivityService.dart'; +import 'package:uae_stat/presentation/Screens/online_offline_verification/internet_check.dart'; import 'package:uae_stat/presentation/Screens/profilepage.dart'; import 'package:uae_stat/presentation/Screens/auth_verification/registration.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, +// ); +// } +// } + void main() async { WidgetsFlutterBinding.ensureInitialized(); - // await (await SharedPreferences.getInstance()).clear(); configureDependencies(); runApp( const ProviderScope( @@ -31,86 +106,35 @@ class MainApp extends ConsumerWidget { Widget build(BuildContext context, WidgetRef ref) { final locale = ref.watch(localeProvider); + ref.listen(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,// Use the GoRouter instance from app_router.dart + 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'); + } + } } -// class MainApp extends StatelessWidget { -// const MainApp({super.key}); -// -// @override -// Widget build(BuildContext context) { -// return MaterialApp( -// home: RegisterScreen(), -// //home: ProfileScreen(), -// debugShowCheckedModeBanner: false, -// ); -// } -// } - -// class MainApp extends ConsumerWidget { -// const MainApp({super.key}); -// -// @override -// Widget build( -// BuildContext context, -// WidgetRef ref, -// ) { -// const loading = Material( -// child: Center( -// child: CircularProgressIndicator(), -// ), -// ); -// // waits to load preferences (language) -// // from local storage -// final languageFinalizedWidget = ref -// .watch( -// routerProvider, -// ) -// .when( -// loading: () => loading, -// error: Error.throwWithStackTrace, -// data: (data) { -// final materialApp = MaterialApp.router( -// debugShowCheckedModeBanner: false, -// routerConfig: data, -// supportedLocales: LanguageLocale.values.map( -// (e) => e.toLocale(), -// ), -// localizationsDelegates: GlobalMaterialLocalizations.delegates, -// theme: ThemeData.from( -// colorScheme: ColorScheme.fromSeed( -// seedColor: MyTheme.topicColor( -// IndicatorTopic.economy, -// ), -// ), -// textTheme: OldTheme.textTheme, -// ), -// ); -// return ColoredBox( -// color: Colors.white, -// child: materialApp, -// ); -// }, -// ); -// // waits to load auth from local storage -// final authFinalizedWidget = ref -// .watch( -// authUseCaseProvider, -// ) -// .when( -// loading: () => loading, -// error: Error.throwWithStackTrace, -// data: (data) => languageFinalizedWidget, -// ); -// return authFinalizedWidget; -// } -// } diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 1c9c8dd7..c5426f77 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -16,7 +16,7 @@ import video_player_avfoundation import webview_flutter_wkwebview func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { - ConnectivityPlusPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlusPlugin")) + ConnectivityPlugin.register(with: registry.registrar(forPlugin: "ConnectivityPlugin")) FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) diff --git a/pubspec.lock b/pubspec.lock index f310f720..c5834e23 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -194,18 +194,18 @@ packages: dependency: "direct main" description: name: connectivity_plus - sha256: "8a68739d3ee113e51ad35583fdf9ab82c55d09d693d3c39da1aebab87c938412" + sha256: "224a77051d52a11fbad53dd57827594d3bd24f945af28bd70bab376d68d437f0" url: "https://pub.dev" source: hosted - version: "6.1.2" + version: "5.0.2" connectivity_plus_platform_interface: dependency: transitive description: name: connectivity_plus_platform_interface - sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204" + sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a url: "https://pub.dev" source: hosted - version: "2.0.1" + version: "1.2.4" convert: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 0145a0ed..3194705a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -60,7 +60,7 @@ dependencies: material_charts: ^0.0.23 flutter_staggered_grid_view: ^0.7.0 syncfusion_flutter_charts: ^28.1.39 - connectivity_plus: ^6.1.2 + connectivity_plus: ^5.0.1 dependency_overrides: fading_edge_scrollview: ^4.1.1