setUp method
Implementation
Future<void> setUp({Duration? checkInterval, Duration? timeout}) async {
if (timeout != null) {
_stealthInternetChecker.timeout = timeout;
}
if (checkInterval != null) {
_stealthInternetChecker.checkInterval = checkInterval;
}
// Cancel any existing subscriptions to prevent duplicate event delivery
await _hardwareSubscription?.cancel();
await _probeSubscription?.cancel();
// 1. Initial Hardware & Reachability Check
try {
final initialResults = await _connectivity.checkConnectivity();
final resolution = resolveModes(initialResults);
_currentMode = resolution.primary;
_activeModes = resolution.active;
bool hasInternet = false;
if (_currentMode != ConnectionMode.none) {
hasInternet = await _stealthInternetChecker.getCurrentStatus();
} else {
// Double check in case platform channel returns none prematurely during app startup
hasInternet = await _stealthInternetChecker.getCurrentStatus();
if (hasInternet) {
_currentMode = ConnectionMode.wifi;
_activeModes = [ConnectionMode.wifi];
}
}
_isInitialCheck = false;
if (hasInternet) {
_stealthInternetChecker.notifyOnline();
_statusController.add(true);
ZoConnectivityWatcher().updateStatusAndMode(
status: ConnectivityWatcherStatus.connected,
mode: _currentMode,
activeModes: _activeModes,
);
} else {
_stealthInternetChecker.notifyOffline();
_statusController.add(false);
ZoConnectivityWatcher().updateStatusAndMode(
status: ConnectivityWatcherStatus.disconnected,
mode: _currentMode,
activeModes: _activeModes,
);
}
} catch (e) {
_isInitialCheck = false;
debugPrint('ZoConnectivityController: hardware check error: $e');
}
// 2. Hardware Change Stream (Airplane mode, Wi-Fi on/off, Cellular handover)
_hardwareSubscription =
_connectivity.onConnectivityChanged.listen((results) {
_handleHardwareChange(results);
});
// 3. Active Probe Stream (internet reachability)
_probeSubscription =
_stealthInternetChecker.onStatusChange.listen((hasInternet) {
void applyStatus() {
_statusController.add(hasInternet);
final status = hasInternet
? ConnectivityWatcherStatus.connected
: ConnectivityWatcherStatus.disconnected;
ZoConnectivityWatcher().updateStatusAndMode(
status: status,
mode: _currentMode,
activeModes: _activeModes,
);
}
if (_isInitialCheck) {
_isInitialCheck = false;
applyStatus();
} else {
_debouncer(applyStatus);
}
});
}