Skip to content

Commit e04ab72

Browse files
feat: adds the ability to provide a custom connectivity check function (#86)
* feat: adds the ability to provide a custom connectivity check function * implements PR feedback * style: formatted code to follow 80 char per line rule * style: format tests * style: format readme --------- Co-authored-by: OutdatedGuy <everythingoutdated@gmail.com>
1 parent f587a24 commit e04ab72

3 files changed

Lines changed: 109 additions & 1 deletion

File tree

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,41 @@ final connection = InternetConnection.createInstance(
149149
);
150150
```
151151

152-
### 7. Pause and Resume on App Lifecycle Changes
152+
### 7. Using a custom connectivity check method
153+
154+
For advanced use cases, you can completely customize how connectivity checks are performed by providing your own connectivity checker:
155+
156+
```dart
157+
final connection = InternetConnection.createInstance(
158+
customConnectivityCheck: (option) async {
159+
// Example: Use the Dio http client
160+
try {
161+
final dio = Dio();
162+
final response = await dio.head(
163+
option.uri,
164+
options: Options(headers: option.headers, receiveTimeout: option.timeout, validateStatus: (_) => true),
165+
);
166+
167+
return InternetCheckResult(
168+
option: option,
169+
isSuccess: response.statusCode == 200,
170+
);
171+
} catch (_) {
172+
return InternetCheckResult(option: option, isSuccess: false);
173+
}
174+
},
175+
);
176+
```
177+
178+
This customization gives you full control over the connectivity detection process, allowing you to:
179+
180+
- Implement platform-specific network detection
181+
- Use alternate connectivity checking strategies
182+
- Implement custom fallback mechanisms
183+
- Add detailed logging or metrics for connectivity checks
184+
- Integrate with other network monitoring tools
185+
186+
### 8. Pause and Resume on App Lifecycle Changes
153187

154188
For situation where you want to pause any network requests when the app goes
155189
into the background and resume them when the app comes back into the foreground

lib/src/internet_connection.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
part of '../internet_connection_checker_plus.dart';
22

3+
/// A callback function for checking if a specific internet endpoint is
4+
/// reachable.
5+
///
6+
/// Takes a single [InternetCheckOption] and returns a
7+
/// [Future] that completes with an [InternetCheckResult].
8+
///
9+
/// This allows for complete customization of how connectivity is checked for
10+
/// each endpoint.
11+
typedef ConnectivityCheckCallback = Future<InternetCheckResult> Function(
12+
InternetCheckOption option,
13+
);
14+
315
/// A utility class for checking internet connectivity status.
416
///
517
/// This class provides functionality to monitor and verify internet
@@ -70,11 +82,17 @@ class InternetConnection {
7082
///
7183
/// - If [useDefaultOptions] is `false`, you must provide a non-empty
7284
/// [customCheckOptions] list.
85+
///
86+
/// The [customConnectivityCheck] allows you to provide a custom method for
87+
/// checking endpoint reachability. If provided, it will be used for all
88+
/// connectivity checks instead of the default HTTP HEAD request
89+
/// implementation.
7390
InternetConnection.createInstance({
7491
Duration? checkInterval,
7592
List<InternetCheckOption>? customCheckOptions,
7693
bool useDefaultOptions = true,
7794
this.enableStrictCheck = false,
95+
this.customConnectivityCheck,
7896
}) : _checkInterval = checkInterval ?? _defaultCheckInterval,
7997
assert(
8098
useDefaultOptions || customCheckOptions?.isNotEmpty == true,
@@ -132,6 +150,12 @@ class InternetConnection {
132150
/// outages.
133151
final bool enableStrictCheck;
134152

153+
/// Function to check reachability of a single network endpoint.
154+
///
155+
/// This can be customized to allow for different ways of checking
156+
/// connectivity.
157+
final ConnectivityCheckCallback? customConnectivityCheck;
158+
135159
/// The last known internet connection status result.
136160
InternetStatus? _lastStatus;
137161

@@ -146,6 +170,10 @@ class InternetConnection {
146170
InternetCheckOption option,
147171
) async {
148172
try {
173+
if (customConnectivityCheck != null) {
174+
return customConnectivityCheck!.call(option);
175+
}
176+
149177
final response = await http
150178
.head(option.uri, headers: option.headers)
151179
.timeout(option.timeout);

test/internet_connection_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,52 @@ void main() {
7070
expect(await checker.hasInternetAccess, expectedStatus);
7171
});
7272
});
73+
74+
test(
75+
'creates and uses default HTTP client when none is provided',
76+
() async {
77+
// This test verifies default behavior, which is hard to test directly
78+
// So we test that the checker works without explicitly providing a client
79+
final checker = InternetConnection.createInstance(
80+
customCheckOptions: [
81+
InternetCheckOption(uri: Uri.parse('https://www.example.com')),
82+
],
83+
useDefaultOptions: false,
84+
);
85+
86+
// Since we can't mock the global HTTP client in this test,
87+
// we just verify that no exception is thrown when executing this
88+
// This inherently tests that a default client was created and used
89+
// Successfully checking internet access means the internal client works
90+
expect(checker.hasInternetAccess, isA<Future<bool>>());
91+
},
92+
);
93+
94+
test('uses custom reachability checker when provided', () async {
95+
var reachabilityCheckerCalled = false;
96+
97+
customReachabilityChecker(InternetCheckOption option) async {
98+
reachabilityCheckerCalled = true;
99+
expect(option.uri.host, 'example.com');
100+
// Return success for this test
101+
return InternetCheckResult(
102+
option: option,
103+
isSuccess: true,
104+
);
105+
}
106+
107+
final checker = InternetConnection.createInstance(
108+
customCheckOptions: [
109+
InternetCheckOption(uri: Uri.parse('https://example.com')),
110+
],
111+
useDefaultOptions: false,
112+
customConnectivityCheck: customReachabilityChecker,
113+
);
114+
115+
final result = await checker.hasInternetAccess;
116+
expect(reachabilityCheckerCalled, true);
117+
expect(result, true);
118+
});
73119
});
74120

75121
group('enableStrictCheck', () {

0 commit comments

Comments
 (0)