Repository: OutdatedGuy/internet_connection_checker_plus

Issue:
Feature request: Ensure all customCheckOptions are available
I'd like to only count it as a "working internet connection" when all of our backend servers can be reached.


Discussion: Hmmm... This is key man!

Hi, will try to work on this Feature Request when I get some free time. PRs are always welcomed.

Fix patch:
diff --git a/lib/src/internet_connection.dart b/lib/src/internet_connection.dart
index dbce81a..1619a4b 100644
--- a/lib/src/internet_connection.dart
+++ b/lib/src/internet_connection.dart
@@ -74,6 +74,7 @@ class InternetConnection {
     Duration? checkInterval,
     List<InternetCheckOption>? customCheckOptions,
     bool useDefaultOptions = true,
+    this.enableStrictCheck = false,
   })  : _checkInterval = checkInterval ?? _defaultCheckInterval,
         assert(
           useDefaultOptions || customCheckOptions?.isNotEmpty == true,
@@ -116,6 +117,15 @@ class InternetConnection {
   /// Defaults to [_defaultCheckInterval].
   Duration _checkInterval;
 
+  /// If `true`, all checks must be successful to consider the internet as
+  /// connected.
+  ///
+  /// If `false`, only one successful check is required to consider the internet
+  /// as connected.
+  ///
+  /// Defaults to `false`.
+  final bool enableStrictCheck;
+
   /// The last known internet connection status result.
   InternetStatus? _lastStatus;
 
@@ -164,18 +174,28 @@ class InternetConnection {
   /// whether internet access is available or not.
   Future<bool> get hasInternetAccess async {
     final completer = Completer<bool>();
-    int length = _internetCheckOptions.length;
+    int remainingChecks = _internetCheckOptions.length;
+    int successCount = 0;
 
     for (final option in _internetCheckOptions) {
       unawaited(
         _checkReachabilityFor(option).then((result) {
-          length -= 1;
+          if (result.isSuccess) {
+            successCount += 1;
+          }
+
+          remainingChecks -= 1;
 
           if (completer.isCompleted) return;
 
-          if (result.isSuccess) {
+          if (!enableStrictCheck && result.isSuccess) {
+            // Return true immediately if not in strict mode and a success is found.
             completer.complete(true);
-          } else if (length == 0) {
+          } else if (enableStrictCheck && remainingChecks == 0) {
+            // In strict mode, complete only when all checks are done.
+            completer.complete(successCount == _internetCheckOptions.length);
+          } else if (!enableStrictCheck && remainingChecks == 0) {
+            // In non-strict mode, complete as false if no success is found.
             completer.complete(false);
           }
         }),


Test patch:
diff --git a/test/internet_connection_test.dart b/test/internet_connection_test.dart
index 3ea10f0..e216403 100644
--- a/test/internet_connection_test.dart
+++ b/test/internet_connection_test.dart
@@ -72,6 +72,27 @@ void main() {
       });
     });
 
+    group('enableStrictCheck', () {
+      test('returns true when all URIs are reachable', () async {
+        final checker = InternetConnection.createInstance(
+          enableStrictCheck: true,
+        );
+        expect(await checker.hasInternetAccess, true);
+      });
+
+      test('returns false when any URI is unreachable', () async {
+        final checker = InternetConnection.createInstance(
+          enableStrictCheck: true,
+          customCheckOptions: [
+            InternetCheckOption(
+              uri: Uri.parse('https://www.example.com/nonexistent-page'),
+            ),
+          ],
+        );
+        expect(await checker.hasInternetAccess, false);
+      });
+    });
+
     group('checkInterval', () {
       setUp(() {
         TestWidgetsFlutterBinding.ensureInitialized();