From 9057f41baa6dac7668000eba036fc8715ca5a4aa Mon Sep 17 00:00:00 2001 From: Satyam Jha Date: Sun, 22 Dec 2024 01:40:48 +0530 Subject: [PATCH] upgrade connectivity_plus from 5.0.2 to 6.0.5 with necessary adjustments --- .../connectivity_service.dart | 17 ++--- lib/view_model/connectivity_view_model.dart | 17 +++-- pubspec.lock | 8 +-- pubspec.yaml | 2 +- .../connectivity_service_test.dart | 66 +++++++++++++------ .../connectivity_view_model_test.dart | 8 +-- 6 files changed, 74 insertions(+), 44 deletions(-) diff --git a/lib/services/third_party_service/connectivity_service.dart b/lib/services/third_party_service/connectivity_service.dart index d1e95ff5d..f88944a7b 100644 --- a/lib/services/third_party_service/connectivity_service.dart +++ b/lib/services/third_party_service/connectivity_service.dart @@ -15,10 +15,10 @@ import 'package:talawa/locator.dart'; /// * Checking if the device has any type of network connection - [hasConnection] class ConnectivityService { /// Stream controller for network status changes. - late StreamController connectionStatusController; + late StreamController> connectionStatusController; /// Getter for the stream of connection status changes. - Stream get connectionStream => + Stream> get connectionStream => connectionStatusController.stream; /// Checks the current internet connectivity status of the device. @@ -27,8 +27,8 @@ class ConnectivityService { /// None /// /// **returns**: - /// * `Future`: indicates if the url is reachable. - Future getConnectionType() async { + /// * `Future>`: indicates if the url is reachable. + Future> getConnectionType() async { final result = await connectivity.checkConnectivity(); return result; } @@ -45,7 +45,7 @@ class ConnectivityService { /// None Future initConnectivity({required http.Client client}) async { _client = client; - connectionStatusController = StreamController(); + connectionStatusController = StreamController>(); /// Listen for future changes in connectivity enableSubscription(); @@ -60,7 +60,7 @@ class ConnectivityService { /// None Future enableSubscription() async { connectivity.onConnectivityChanged.listen( - (ConnectivityResult result) { + (List result) { print(result); connectionStatusController.add(result); }, @@ -107,8 +107,9 @@ class ConnectivityService { /// * `Future`: indicating whether the device has a network connection. Future hasConnection() async { try { - final result = await getConnectionType(); - return result != ConnectivityResult.none; + final results = await getConnectionType(); + return results.isNotEmpty && + results.any((result) => result != ConnectivityResult.none); } catch (e) { return false; } diff --git a/lib/view_model/connectivity_view_model.dart b/lib/view_model/connectivity_view_model.dart index 4b9bec469..fbec74e0b 100644 --- a/lib/view_model/connectivity_view_model.dart +++ b/lib/view_model/connectivity_view_model.dart @@ -20,7 +20,7 @@ import 'package:talawa/view_model/base_view_model.dart'; /// * Triggers the snackbar UI to show online status.: [showSnackbar] class AppConnectivity extends BaseModel { /// Stream from [ConnectivityService]. - late final Stream connectivityStream; + late final Stream> connectivityStream; /// Subscription of the [connectivityStream] StreamSubscription? _subscription; @@ -51,7 +51,8 @@ class AppConnectivity extends BaseModel { /// None void enableSubscription() { try { - _subscription = connectivityStream.listen((ConnectivityResult result) { + _subscription = + connectivityStream.listen((List result) { handleConnection(result); }); } catch (e) { @@ -59,16 +60,18 @@ class AppConnectivity extends BaseModel { } } - /// This function handles the device's connectivity status based on the provided [ConnectivityResult]. + /// This function handles the device's connectivity status based on the provided [List]. /// /// **params**: - /// * `result`: A [ConnectivityResult] indicating the current connectivity status. + /// * `result`: A [List] indicating the current connectivity status. /// /// **returns**: /// None - Future handleConnection(ConnectivityResult result) async { - if (![ConnectivityResult.none, ConnectivityResult.bluetooth] - .contains(result)) { + Future handleConnection(List result) async { + if (result.any( + (r) => + ![ConnectivityResult.none, ConnectivityResult.bluetooth].contains(r), + )) { handleOnline(); } else { handleOffline(); diff --git a/pubspec.lock b/pubspec.lock index 89c74198c..e5c57ee4a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -205,18 +205,18 @@ packages: dependency: "direct main" description: name: connectivity_plus - sha256: "224a77051d52a11fbad53dd57827594d3bd24f945af28bd70bab376d68d437f0" + sha256: e0817759ec6d2d8e57eb234e6e57d2173931367a865850c7acea40d4b4f9c27d url: "https://pub.dev" source: hosted - version: "5.0.2" + version: "6.0.5" connectivity_plus_platform_interface: dependency: transitive description: name: connectivity_plus_platform_interface - sha256: cf1d1c28f4416f8c654d7dc3cd638ec586076255d407cef3ddbdaf178272a71a + sha256: "42657c1715d48b167930d5f34d00222ac100475f73d10162ddf43e714932f204" url: "https://pub.dev" source: hosted - version: "1.2.4" + version: "2.0.1" contained_tab_bar_view: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 9a87a9a75..87c43a80f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: auto_size_text: ^3.0.0 cached_network_image: ^3.4.1 clock: ^1.1.1 - connectivity_plus: ^5.0.2 + connectivity_plus: ^6.0.5 contained_tab_bar_view: ^0.8.0 crypto: ^3.0.5 diff --git a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart index 0bca2232a..3acc2b5ad 100644 --- a/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart +++ b/test/service_tests/third_party_service_test.dart/connectivity_service_test.dart @@ -18,11 +18,11 @@ bool internetAccessible = true; class MockConnectivityService extends Mock with MockPlatformInterfaceMixin implements ConnectivityService { - final controller = StreamController(); + final controller = StreamController>(); @override // TODO: implement connectionStatusController - StreamController get connectionStatusController => + StreamController> get connectionStatusController => controller; @override @@ -32,34 +32,58 @@ class MockConnectivityService extends Mock } @override - Stream get connectionStream => controller.stream; + Stream> get connectionStream => controller.stream; @override - Future getConnectionType() { - return Future.value(connectivityStatus); + Future> getConnectionType() { + return Future.value([connectivityStatus!]); } @override - Future isReachable({http.Client? client, String? uriString}) { - return Future.value(internetAccessible); + Future hasConnection() async { + try { + final results = await getConnectionType(); + return results.isNotEmpty && + results.any((result) => result != ConnectivityResult.none); + } catch (e) { + return false; + } + } + + @override + Future isReachable({ + http.Client? client, + String? uriString, + }) async { + try { + await client! + .get(Uri.parse(uriString ?? graphqlConfig.httpLink.uri.toString())) + .timeout(const Duration(seconds: 30)); + return true; + } catch (e) { + print('Timeout while checking reachability: $e'); + return false; + } } } class MockConnectivity extends Mock implements Connectivity { - final controller = StreamController(); + final controller = StreamController>(); - StreamController get connectivityController => controller; + StreamController> get connectivityController => + controller; @override - Stream get onConnectivityChanged => controller.stream; + Stream> get onConnectivityChanged => + controller.stream; @override - Future checkConnectivity() async { + Future> checkConnectivity() async { // TODO: implement checkConnectivity if (connectivityStatus == null) { throw const SocketException('socket exception'); } - return connectivityStatus!; + return [connectivityStatus!]; } } @@ -75,13 +99,13 @@ class MockClient extends Mock implements http.Client { void main() { late MockClient mockClient; - late ConnectivityService service; + late MockConnectivityService service; setUpAll(() { TestWidgetsFlutterBinding.ensureInitialized(); mockClient = MockClient(); getAndRegisterConnectivity(); connectivityStatus = ConnectivityResult.mobile; - service = ConnectivityService(); + service = MockConnectivityService(); locator.registerSingleton(service); connectivityService.initConnectivity(client: http.Client()); }); @@ -90,17 +114,17 @@ void main() { test( 'connectionStream getter', () async { - expect(connectivityService, isA()); + expect(service, isA()); expect( - connectivityService.connectionStream, - isA>(), + service.connectionStream, + isA>>(), ); }, ); test('listener', () async { final mockConnectivity = testgetit.connectivity as MockConnectivity; - mockConnectivity.connectivityController.add(ConnectivityResult.mobile); + mockConnectivity.connectivityController.add([ConnectivityResult.mobile]); mockConnectivity.connectivityController .addError(Exception("Something went wrong!")); @@ -115,8 +139,10 @@ void main() { }); test('isReachable', () async { - final reached = - await service.isReachable(uriString: 'https://google.com'); + final reached = await service.isReachable( + client: mockClient, + uriString: 'https://google.com', + ); expect(reached, true); }); diff --git a/test/view_model_tests/connectivity_view_model_test.dart b/test/view_model_tests/connectivity_view_model_test.dart index 66a25b228..04a2475e5 100644 --- a/test/view_model_tests/connectivity_view_model_test.dart +++ b/test/view_model_tests/connectivity_view_model_test.dart @@ -74,12 +74,12 @@ void main() { group('test connectivity view model', () { test('handleConnection when demoMode', () { MainScreenViewModel.demoMode = true; - model.handleConnection(ConnectivityResult.mobile); + model.handleConnection([ConnectivityResult.mobile]); }); test('handleConnection when offline', () { internetAccessible = false; - model.handleConnection(ConnectivityResult.none); + model.handleConnection([ConnectivityResult.none]); }); test('handleConnection when online', () async { MainScreenViewModel.demoMode = false; @@ -95,7 +95,7 @@ void main() { ); print(cacheService.offlineActionQueue.getActions()); - model.handleConnection(ConnectivityResult.mobile); + model.handleConnection([ConnectivityResult.mobile]); }); testWidgets('showSnackbar when online', (tester) async { @@ -114,7 +114,7 @@ void main() { test('check enableSubscription body', () { connectivityService.connectionStatusController - .add(ConnectivityResult.none); + .add([ConnectivityResult.none]); }); test('enableSubscirption exception', () async {