Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.2.1 #49

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install environment
uses: subosito/[email protected]
with:
flutter-version: 3.10.6
flutter-version: 3.16.9

- name: Get dependencies
run: flutter pub get
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1
- Support new connectivity plus multiple connection type
- Fix crash on iphone.

## 2.2.0

- Removed InternetAddress constraint on IO platforms (when creating a socket connection check)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A plugin that wraps [connectivity_plus](https://pub.dev/packages/connectivity_pl
- [ConnectionChecker init](#connectionchecker-init)
- [connectionStream](#get-connectionstream)
- [isConnected](#get-isconnected)
- [connectionType](#get-connectiontype)
- [connectionTypes](#get-connectiontypes)
- [untilConnects](#untilconnects)

## Motivation
Expand Down Expand Up @@ -127,7 +127,7 @@ if (hasInternetConnection) {
}
```

### get connectionType
### get connectionTypes

Returns the current connection type.

Expand All @@ -136,8 +136,8 @@ Example:
```dart
final connecteo = ConnectionChecker();

final type = await connecteo.connectionType;
if (type == ConnectionType.mobile) {
final types = await connecteo.connectionTypes;
if (types.contains(ConnectionType.mobile)) {
// Handle the logic when the application uses cellular data
}
```
Expand Down
8 changes: 4 additions & 4 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class _ConnecteoExamplePageState extends State<ConnecteoExamplePage> {
late StreamSubscription<bool> _streamSubscription;

Color? _backgroundColor;
ConnectionType? _connectionType;
List<ConnectionType>? _connectionTypes;

@override
void initState() {
Expand Down Expand Up @@ -67,14 +67,14 @@ class _ConnecteoExamplePageState extends State<ConnecteoExamplePage> {
children: [
OutlinedButton(
onPressed: () async {
final type = await _connecteo.connectionType;
final types = await _connecteo.connectionTypes;
setState(() {
_connectionType = type;
_connectionTypes = types;
});
},
child: const Text('Check connection type'),
),
Text('Connection type value: $_connectionType'),
Text('Connection type value: $_connectionTypes'),
],
),
)
Expand Down
25 changes: 14 additions & 11 deletions lib/src/connection_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ class ConnectionChecker {
int? failureAttempts,
Connectivity? connectivity,
HostReachabilityChecker? hostReachabilityChecker,
Mapper<ConnectivityResult, ConnectionType>? connectionTypeMapper,
Mapper<List<ConnectivityResult>, List<ConnectionType>>?
connectionTypeMapper,
}) : _checkHostReachability = checkHostReachability,
_hostReachabilityTimeout = hostReachabilityTimeout,
_baseUrlLookupAddress = baseUrlLookupAddress,
Expand All @@ -124,7 +125,8 @@ class ConnectionChecker {
factory ConnectionChecker.test({
required Connectivity connectivity,
required HostReachabilityChecker hostReachabilityChecker,
required Mapper<ConnectivityResult, ConnectionType> connectionTypeMapper,
required Mapper<List<ConnectivityResult>, List<ConnectionType>>
connectionTypeMapper,
bool checkHostReachability = true,
List<ConnectionEntry>? checkConnectionEntriesNative,
List<ConnectionEntry>? checkConnectionEntriesWeb,
Expand All @@ -148,7 +150,8 @@ class ConnectionChecker {
}

final Connectivity _connectivity;
final Mapper<ConnectivityResult, ConnectionType> _connectionTypeMapper;
final Mapper<List<ConnectivityResult>, List<ConnectionType>>
_connectionTypeMapper;

final List<ConnectionEntry>? _checkConnectionEntries;
final Duration? _hostReachabilityTimeout;
Expand Down Expand Up @@ -186,7 +189,7 @@ class ConnectionChecker {
Stream<void>.periodic(_requestInterval),
],
// ignore: cast_nullable_to_non_nullable
(events) => events.first as ConnectionType,
(events) => events.first as List<ConnectionType>,
)
.flatMap(_isConnectionTypeReachableStream)
.scan<int>(_accumulateFailures, 0)
Expand All @@ -204,12 +207,12 @@ 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 isConnectionTypesContainsOnline =
_connectionTypeMapper.call(result).containsOnline;

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

return isHostReachable;
}
Expand All @@ -222,7 +225,7 @@ class ConnectionChecker {
/// [ConnectionType.vpn] does not guarantee you have got internet connection.
/// However, it may be helpful when you want to implement specific logic for
/// cellular data connection for example.
Future<ConnectionType> get connectionType async {
Future<List<ConnectionType>> get connectionTypes async {
final connectivityResult = await _connectivity.checkConnectivity();
return _connectionTypeMapper.call(connectivityResult);
}
Expand All @@ -233,13 +236,13 @@ class ConnectionChecker {
}

Stream<bool> _isConnectionTypeReachableStream(
ConnectionType connectionType,
List<ConnectionType> connectionTypeList,
) async* {
final isHostReachable = await _hostReachable;
if (!isHostReachable) {
yield false;
} else {
if (connectionType.onlineType) {
if (connectionTypeList.containsOnline) {
yield true;
} else {
yield false;
Expand Down
14 changes: 11 additions & 3 deletions lib/src/connection_type_mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ abstract class Mapper<I, O> {
}

class ConnectionTypeMapper
implements Mapper<ConnectivityResult, ConnectionType> {
implements Mapper<List<ConnectivityResult>, List<ConnectionType>> {
@override
ConnectionType call(ConnectivityResult input) {
switch (input) {
List<ConnectionType> call(List<ConnectivityResult> input) {
return input.map(_map).toList();
}

ConnectionType _map(ConnectivityResult connectivityResult) {
switch (connectivityResult) {
case ConnectivityResult.bluetooth:
return ConnectionType.bluetooth;
case ConnectivityResult.ethernet:
Expand All @@ -27,3 +31,7 @@ class ConnectionTypeMapper
}
}
}

extension ListConnectionTypeExt on List<ConnectionType> {
bool get containsOnline => isNotEmpty && any((value) => value.onlineType);
}
4 changes: 2 additions & 2 deletions lib/src/host_reachability_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DefaultHostReachabilityChecker implements HostReachabilityChecker {
),
);

return connectionResults.any((result) => result == true);
return connectionResults.any((result) => result);
}

Future<bool> _canIoReachHost({
Expand Down Expand Up @@ -117,7 +117,7 @@ class WebHostReachabilityChecker implements HostReachabilityChecker {
),
);

return connectionResults.any((result) => result == true);
return connectionResults.any((result) => result);
}

Future<bool> _canWebReachHost({
Expand Down
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: connecteo
description: A plugin that wraps connectivity_plus and adds some additional data connection checks.
version: 2.2.0
version: 2.2.1
repository: https://github.com/Iteo/connecteo
issue_tracker: https://github.com/Iteo/connecteo/issues
homepage: https://github.com/Iteo/connecteo
Expand All @@ -11,10 +11,10 @@ environment:
flutter: ">=3.10.0"

dependencies:
connectivity_plus: ^5.0.2
connectivity_plus: ^6.0.1
flutter:
sdk: flutter
http: ^1.1.0
http: ^1.1.2
rxdart: ^0.27.7

dev_dependencies:
Expand Down
Loading