Skip to content

Commit

Permalink
support new connectivity plus mutiple connection type
Browse files Browse the repository at this point in the history
  • Loading branch information
alihassan143 authored and AndriiChemer committed Mar 28, 2024
1 parent 2744828 commit 17b83c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
21 changes: 12 additions & 9 deletions lib/src/connection_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,8 @@ class ConnectionChecker {
Stream<bool> get connectionStream => CombineLatestStream(
[
ConcatStream([
_connectivity
.checkConnectivity()
.asStream()
.map(_connectionTypeMapper.call),
_connectivity.onConnectivityChanged.map(_connectionTypeMapper.call),
_connectivity.checkConnectivity().asStream().map(_connectionType),
_connectivity.onConnectivityChanged.map(_connectionType),
]),
Stream<void>.periodic(_requestInterval),
],
Expand All @@ -204,16 +201,22 @@ class ConnectionChecker {
/// it is more secure to listen for [connectionStream].
Future<bool> get isConnected async {
final result = await _connectivity.checkConnectivity();
final isConnectionTypeOnline =
_connectionTypeMapper.call(result).onlineType;
final isConnectionTypeOnline = _connectionType(result).onlineType;

final reachability = await Future.wait([_hostReachable, _baseUrlReachable]);
final isHostReachable = [isConnectionTypeOnline, ...reachability]
.every((reachable) => reachable == true);
.every((reachable) => reachable);

return isHostReachable;
}

ConnectionType _connectionType(List<ConnectivityResult> results) {
for (final data in results) {
return _connectionTypeMapper.call(data);
}
return ConnectionType.none;
}

/// Returns the current [ConnectionType] of your device.
///
/// Do not use it for determination of your current connection status - this
Expand All @@ -224,7 +227,7 @@ class ConnectionChecker {
/// cellular data connection for example.
Future<ConnectionType> get connectionType async {
final connectivityResult = await _connectivity.checkConnectivity();
return _connectionTypeMapper.call(connectivityResult);
return _connectionType(connectivityResult);
}

/// Resolves as soon as internet connection status get back from offline state.
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ environment:
flutter: ">=3.10.0"

dependencies:
connectivity_plus: ^5.0.2
connectivity_plus: ^6.0.1
flutter:
sdk: flutter
http: ^1.1.2
Expand Down
38 changes: 19 additions & 19 deletions test/connection_checker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
),
).thenAnswer((_) => Future.value(true));
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));
when(() => connectionTypeMapper.call(any()))
.thenAnswer((_) => ConnectionType.wifi);

Expand All @@ -77,7 +77,7 @@ void main() {
),
).thenAnswer((_) => Future.value(true));
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));
when(() => connectionTypeMapper.call(any()))
.thenAnswer((_) => ConnectionType.wifi);

Expand All @@ -100,7 +100,7 @@ void main() {
),
).thenAnswer((_) => Future.value(true));
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.none));
.thenAnswer((_) => Future.value([ConnectivityResult.none]));
when(() => connectionTypeMapper.call(any()))
.thenAnswer((_) => ConnectionType.none);

Expand All @@ -125,7 +125,7 @@ void main() {
when(() => hostReachabilityChecker.canReachAnyHost())
.thenAnswer((_) => Future.value(true));
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));
when(() => connectionTypeMapper.call(any()))
.thenAnswer((_) => ConnectionType.wifi);

Expand All @@ -145,7 +145,7 @@ void main() {
const expected = ConnectionType.wifi;

when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));
when(() => connectionTypeMapper.call(any())).thenAnswer((_) => expected);

final result = await connectionChecker.connectionType;
Expand All @@ -160,7 +160,7 @@ void main() {
group('untilConnects', () {
test('returns the Future once, after the connection is renewed', () async {
final completer = Completer<bool>();
final controller = StreamController<ConnectivityResult>();
final controller = StreamController<List<ConnectivityResult>>();
when(() => hostReachabilityChecker.canReachAnyHost())
.thenAnswer((_) => Future.value(true));
when(
Expand All @@ -175,14 +175,14 @@ void main() {
when(() => connectionTypeMapper.call(ConnectivityResult.none))
.thenAnswer((_) => ConnectionType.none);
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));

connectionChecker.untilConnects().whenComplete(() {
completer.complete(true);
});
controller.add(ConnectivityResult.none);
controller.add([ConnectivityResult.none]);
await Future<void>.delayed(requestInterval);
controller.add(ConnectivityResult.wifi);
controller.add([ConnectivityResult.wifi]);

expect(completer.future, completion(true));

Expand All @@ -194,7 +194,7 @@ void main() {
test(
'returns [true] from Stream while hosts are reachable, base url is reachable and connection type is online',
() async {
final controller = StreamController<ConnectivityResult>();
final controller = StreamController<List<ConnectivityResult>>();
when(() => hostReachabilityChecker.canReachAnyHost())
.thenAnswer((_) => Future.value(true));
when(
Expand All @@ -207,7 +207,7 @@ void main() {
when(() => connectionTypeMapper.call(any()))
.thenAnswer((_) => ConnectionType.wifi);
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));

expectLater(
connectionChecker.connectionStream,
Expand All @@ -219,15 +219,15 @@ void main() {
.called(1);
},
);
controller.add(ConnectivityResult.wifi);
controller.add([ConnectivityResult.wifi]);

controller.close();
});

test(
'returns [true, false] from Stream while hosts are reachable, base url is reachable but connection dropped',
() async {
final controller = StreamController<ConnectivityResult>();
final controller = StreamController<List<ConnectivityResult>>();
when(() => hostReachabilityChecker.canReachAnyHost())
.thenAnswer((_) => Future.value(true));
when(
Expand All @@ -242,7 +242,7 @@ void main() {
when(() => connectionTypeMapper.call(ConnectivityResult.none))
.thenAnswer((_) => ConnectionType.none);
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));

expectLater(
connectionChecker.connectionStream,
Expand All @@ -253,17 +253,17 @@ void main() {
verify(() => hostReachabilityChecker.hostLookup(baseUrl: baseUrl))
.called(1);
});
controller.add(ConnectivityResult.wifi);
controller.add([ConnectivityResult.wifi]);
await Future<void>.delayed(requestInterval);
controller.add(ConnectivityResult.none);
controller.add([ConnectivityResult.none]);

controller.close();
});

test(
'returns [false] from Stream while hosts are not reachable although connection type is online',
() async {
final controller = StreamController<ConnectivityResult>.broadcast();
final controller = StreamController<List<ConnectivityResult>>.broadcast();
when(() => hostReachabilityChecker.canReachAnyHost())
.thenAnswer((_) => Future.value(false));
when(
Expand All @@ -276,7 +276,7 @@ void main() {
when(() => connectionTypeMapper.call(ConnectivityResult.wifi))
.thenAnswer((_) => ConnectionType.wifi);
when(() => connectivity.checkConnectivity())
.thenAnswer((_) => Future.value(ConnectivityResult.wifi));
.thenAnswer((_) => Future.value([ConnectivityResult.wifi]));

expectLater(
connectionChecker.connectionStream,
Expand All @@ -290,7 +290,7 @@ void main() {
);
},
);
controller.add(ConnectivityResult.wifi);
controller.add([ConnectivityResult.wifi]);

controller.close();
});
Expand Down

0 comments on commit 17b83c6

Please sign in to comment.