feat: Improve offline detection logic

This commit refactors the connectivity service to be more robust and less prone to UI flicker.

Key changes:
- Any successful API response now briefly suppresses the offline warning. This prevents the UI from flashing an offline message between regular connectivity checks.
- The threshold for showing the offline warning is increased from 2 to 3 consecutive failed health checks.
- The timeout for health checks is increased to better handle slow networks.
- The offline warning is now suppressed if there are active data streams to avoid interrupting the user.
- A custom JSON converter is added for conversation metadata to handle potential type mismatches from local storage.
This commit is contained in:
cogwheel0
2025-10-14 22:32:09 +05:30
parent 14af2a100a
commit 762155500b
4 changed files with 72 additions and 12 deletions

View File

@@ -46,7 +46,6 @@ class ConnectivityService with WidgetsBindingObserver {
ConnectivityStatus _currentStatus = ConnectivityStatus.online;
bool _hasNetworkInterface = false;
bool _hasConfirmedNetwork = false;
bool _hasSuccessfulProbe = false;
int _consecutiveFailures = 0;
int _lastLatencyMs = -1;
@@ -149,16 +148,15 @@ class ConnectivityService with WidgetsBindingObserver {
if (isReachable) {
_consecutiveFailures = 0;
_hasSuccessfulProbe = true;
_updateStatus(ConnectivityStatus.online);
} else {
_consecutiveFailures++;
// Only surface offline after we've confirmed the server once or we have
// multiple consecutive failures. This avoids startup flicker where
// authorization or DNS is still settling.
if (_hasSuccessfulProbe || _consecutiveFailures >= 2) {
// Require more consecutive failures to reduce false negatives.
// Switch to offline only after >= 3 consecutive failures.
if (_consecutiveFailures >= 3) {
_updateStatus(ConnectivityStatus.offline);
} else {
// Shorter retry when still below threshold.
overrideDelay = const Duration(seconds: 3);
}
}
@@ -181,13 +179,13 @@ class ConnectivityService with WidgetsBindingObserver {
.getUri(
healthUri,
options: Options(
sendTimeout: const Duration(seconds: 2),
receiveTimeout: const Duration(seconds: 2),
sendTimeout: const Duration(seconds: 5),
receiveTimeout: const Duration(seconds: 5),
followRedirects: false,
validateStatus: (status) => status != null && status < 500,
),
)
.timeout(const Duration(seconds: 3));
.timeout(const Duration(seconds: 6));
final isHealthy = response.statusCode == 200;
@@ -250,6 +248,11 @@ class ConnectivityService with WidgetsBindingObserver {
bool get _isOfflineSuppressed {
final until = _offlineSuppressedUntil;
// Check process-wide suppression window (set by API layer on successes)
final globalUntil = _globalOfflineSuppressedUntil;
if (globalUntil != null && DateTime.now().isBefore(globalUntil)) {
return true;
}
if (until == null) {
return false;
}
@@ -269,6 +272,19 @@ class ConnectivityService with WidgetsBindingObserver {
}
}
// ===== Global suppression signaling (from API layer) =====
static DateTime? _globalOfflineSuppressedUntil;
/// Suppress offline transitions globally for a short window. Useful
/// to avoid flicker after known-good API responses.
static void suppressOfflineGlobally(Duration duration) {
final proposed = DateTime.now().add(duration);
if (_globalOfflineSuppressedUntil == null ||
proposed.isAfter(_globalOfflineSuppressedUntil!)) {
_globalOfflineSuppressedUntil = proposed;
}
}
/// Manually trigger a connectivity check.
Future<bool> checkNow() async {
await _checkServerHealth();