From 9252b43a02b6ca3a62fdaa8ebee8a903b60b4698 Mon Sep 17 00:00:00 2001 From: Nicola Verbeeck Date: Thu, 8 Apr 2021 09:50:36 +0200 Subject: [PATCH] Added connection changed monitor --- lib/src/util/connectivity/connectivity.dart | 7 +++++ test/util/connectivity/connectivity_test.dart | 30 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/src/util/connectivity/connectivity.dart b/lib/src/util/connectivity/connectivity.dart index e505036..d348f44 100644 --- a/lib/src/util/connectivity/connectivity.dart +++ b/lib/src/util/connectivity/connectivity.dart @@ -15,4 +15,11 @@ class ConnectivityHelper { .checkConnectivity() != ConnectivityResult.none; } + + /// Returns a stream that monitors the connectivity state of the device + Stream monitorConnection() { + return (_connectivityProvider?.call() ?? Connectivity()) + .onConnectivityChanged + .map((event) => event != ConnectivityResult.none); + } } diff --git a/test/util/connectivity/connectivity_test.dart b/test/util/connectivity/connectivity_test.dart index 811482c..174bc6f 100644 --- a/test/util/connectivity/connectivity_test.dart +++ b/test/util/connectivity/connectivity_test.dart @@ -1,8 +1,9 @@ import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:mockito/annotations.dart'; import 'package:icapps_architecture/icapps_architecture.dart'; +import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; + import 'connectivity_test.mocks.dart'; @GenerateMocks([Connectivity]) @@ -38,5 +39,32 @@ void main() { .hasConnection(), false); }); + test('Test connectivity stream none', () async { + when(connectivity.onConnectivityChanged) + .thenAnswer((_) => Stream.value(ConnectivityResult.none)); + expect( + await ConnectivityHelper(connectivityProvider: () => connectivity) + .monitorConnection() + .first, + false); + }); + test('Test connectivity stream wifi', () async { + when(connectivity.onConnectivityChanged) + .thenAnswer((_) => Stream.value(ConnectivityResult.wifi)); + expect( + await ConnectivityHelper(connectivityProvider: () => connectivity) + .monitorConnection() + .first, + true); + }); + test('Test connectivity stream mobile', () async { + when(connectivity.onConnectivityChanged) + .thenAnswer((_) => Stream.value(ConnectivityResult.mobile)); + expect( + await ConnectivityHelper(connectivityProvider: () => connectivity) + .monitorConnection() + .first, + true); + }); }); }