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:
@@ -14,7 +14,7 @@ sealed class Conversation with _$Conversation {
|
|||||||
String? model,
|
String? model,
|
||||||
String? systemPrompt,
|
String? systemPrompt,
|
||||||
@Default([]) List<ChatMessage> messages,
|
@Default([]) List<ChatMessage> messages,
|
||||||
@Default({}) Map<String, dynamic> metadata,
|
@Default({}) @_MetadataConverter() Map<String, dynamic> metadata,
|
||||||
@Default(false) bool pinned,
|
@Default(false) bool pinned,
|
||||||
@Default(false) bool archived,
|
@Default(false) bool archived,
|
||||||
String? shareId,
|
String? shareId,
|
||||||
@@ -25,3 +25,22 @@ sealed class Conversation with _$Conversation {
|
|||||||
factory Conversation.fromJson(Map<String, dynamic> json) =>
|
factory Conversation.fromJson(Map<String, dynamic> json) =>
|
||||||
_$ConversationFromJson(json);
|
_$ConversationFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Custom converter to handle Map<dynamic, dynamic> from storage
|
||||||
|
class _MetadataConverter
|
||||||
|
implements JsonConverter<Map<String, dynamic>, Object?> {
|
||||||
|
const _MetadataConverter();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Map<String, dynamic> fromJson(Object? json) {
|
||||||
|
if (json == null) return {};
|
||||||
|
if (json is Map<String, dynamic>) return json;
|
||||||
|
if (json is Map) {
|
||||||
|
return json.map((key, value) => MapEntry(key.toString(), value));
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Object? toJson(Map<String, dynamic> object) => object;
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import 'package:go_router/go_router.dart';
|
|||||||
import '../providers/app_providers.dart';
|
import '../providers/app_providers.dart';
|
||||||
import '../services/connectivity_service.dart';
|
import '../services/connectivity_service.dart';
|
||||||
import '../services/navigation_service.dart';
|
import '../services/navigation_service.dart';
|
||||||
|
import '../services/persistent_streaming_service.dart';
|
||||||
import '../utils/debug_logger.dart';
|
import '../utils/debug_logger.dart';
|
||||||
import '../../features/auth/providers/unified_auth_providers.dart';
|
import '../../features/auth/providers/unified_auth_providers.dart';
|
||||||
import '../../features/auth/views/authentication_page.dart';
|
import '../../features/auth/views/authentication_page.dart';
|
||||||
@@ -103,12 +104,16 @@ class RouterNotifier extends ChangeNotifier {
|
|||||||
// 1. Not in reviewer mode
|
// 1. Not in reviewer mode
|
||||||
// 2. Connectivity is explicitly offline
|
// 2. Connectivity is explicitly offline
|
||||||
// 3. Auth is authenticated (don't interrupt auth flow)
|
// 3. Auth is authenticated (don't interrupt auth flow)
|
||||||
|
// 4. App is in foreground and offline warning isn't suppressed
|
||||||
|
// 5. No active streaming is in progress (avoid interrupting token streams)
|
||||||
|
final hasActiveStreams = PersistentStreamingService().activeStreamCount > 0;
|
||||||
final shouldShowConnectionIssue =
|
final shouldShowConnectionIssue =
|
||||||
!reviewerMode &&
|
!reviewerMode &&
|
||||||
connectivity == ConnectivityStatus.offline &&
|
connectivity == ConnectivityStatus.offline &&
|
||||||
authState == AuthNavigationState.authenticated &&
|
authState == AuthNavigationState.authenticated &&
|
||||||
connectivityService.isAppForeground &&
|
connectivityService.isAppForeground &&
|
||||||
!connectivityService.isOfflineSuppressed;
|
!connectivityService.isOfflineSuppressed &&
|
||||||
|
!hasActiveStreams;
|
||||||
|
|
||||||
if (shouldShowConnectionIssue) {
|
if (shouldShowConnectionIssue) {
|
||||||
return location == Routes.connectionIssue ? null : Routes.connectionIssue;
|
return location == Routes.connectionIssue ? null : Routes.connectionIssue;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import '../auth/api_auth_interceptor.dart';
|
|||||||
import '../error/api_error_interceptor.dart';
|
import '../error/api_error_interceptor.dart';
|
||||||
// Tool-call details are parsed in the UI layer to render collapsible blocks
|
// Tool-call details are parsed in the UI layer to render collapsible blocks
|
||||||
import 'persistent_streaming_service.dart';
|
import 'persistent_streaming_service.dart';
|
||||||
|
import 'connectivity_service.dart';
|
||||||
import '../utils/debug_logger.dart';
|
import '../utils/debug_logger.dart';
|
||||||
import '../utils/openwebui_source_parser.dart';
|
import '../utils/openwebui_source_parser.dart';
|
||||||
|
|
||||||
@@ -90,7 +91,26 @@ class ApiService {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
// 4. Custom debug interceptor to log exactly what we're sending
|
// 4. Success pings to relax offline detection.
|
||||||
|
// Any successful API response indicates recent connectivity; suppress
|
||||||
|
// offline transitions briefly to avoid UI flicker.
|
||||||
|
_dio.interceptors.add(
|
||||||
|
InterceptorsWrapper(
|
||||||
|
onResponse: (response, handler) {
|
||||||
|
try {
|
||||||
|
if ((response.statusCode ?? 0) >= 200 &&
|
||||||
|
(response.statusCode ?? 0) < 400) {
|
||||||
|
ConnectivityService.suppressOfflineGlobally(
|
||||||
|
const Duration(seconds: 4),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (_) {}
|
||||||
|
handler.next(response);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
// 5. Custom debug interceptor to log exactly what we're sending
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
_dio.interceptors.add(
|
_dio.interceptors.add(
|
||||||
InterceptorsWrapper(
|
InterceptorsWrapper(
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ class ConnectivityService with WidgetsBindingObserver {
|
|||||||
ConnectivityStatus _currentStatus = ConnectivityStatus.online;
|
ConnectivityStatus _currentStatus = ConnectivityStatus.online;
|
||||||
bool _hasNetworkInterface = false;
|
bool _hasNetworkInterface = false;
|
||||||
bool _hasConfirmedNetwork = false;
|
bool _hasConfirmedNetwork = false;
|
||||||
bool _hasSuccessfulProbe = false;
|
|
||||||
int _consecutiveFailures = 0;
|
int _consecutiveFailures = 0;
|
||||||
int _lastLatencyMs = -1;
|
int _lastLatencyMs = -1;
|
||||||
|
|
||||||
@@ -149,16 +148,15 @@ class ConnectivityService with WidgetsBindingObserver {
|
|||||||
|
|
||||||
if (isReachable) {
|
if (isReachable) {
|
||||||
_consecutiveFailures = 0;
|
_consecutiveFailures = 0;
|
||||||
_hasSuccessfulProbe = true;
|
|
||||||
_updateStatus(ConnectivityStatus.online);
|
_updateStatus(ConnectivityStatus.online);
|
||||||
} else {
|
} else {
|
||||||
_consecutiveFailures++;
|
_consecutiveFailures++;
|
||||||
// Only surface offline after we've confirmed the server once or we have
|
// Require more consecutive failures to reduce false negatives.
|
||||||
// multiple consecutive failures. This avoids startup flicker where
|
// Switch to offline only after >= 3 consecutive failures.
|
||||||
// authorization or DNS is still settling.
|
if (_consecutiveFailures >= 3) {
|
||||||
if (_hasSuccessfulProbe || _consecutiveFailures >= 2) {
|
|
||||||
_updateStatus(ConnectivityStatus.offline);
|
_updateStatus(ConnectivityStatus.offline);
|
||||||
} else {
|
} else {
|
||||||
|
// Shorter retry when still below threshold.
|
||||||
overrideDelay = const Duration(seconds: 3);
|
overrideDelay = const Duration(seconds: 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,13 +179,13 @@ class ConnectivityService with WidgetsBindingObserver {
|
|||||||
.getUri(
|
.getUri(
|
||||||
healthUri,
|
healthUri,
|
||||||
options: Options(
|
options: Options(
|
||||||
sendTimeout: const Duration(seconds: 2),
|
sendTimeout: const Duration(seconds: 5),
|
||||||
receiveTimeout: const Duration(seconds: 2),
|
receiveTimeout: const Duration(seconds: 5),
|
||||||
followRedirects: false,
|
followRedirects: false,
|
||||||
validateStatus: (status) => status != null && status < 500,
|
validateStatus: (status) => status != null && status < 500,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.timeout(const Duration(seconds: 3));
|
.timeout(const Duration(seconds: 6));
|
||||||
|
|
||||||
final isHealthy = response.statusCode == 200;
|
final isHealthy = response.statusCode == 200;
|
||||||
|
|
||||||
@@ -250,6 +248,11 @@ class ConnectivityService with WidgetsBindingObserver {
|
|||||||
|
|
||||||
bool get _isOfflineSuppressed {
|
bool get _isOfflineSuppressed {
|
||||||
final until = _offlineSuppressedUntil;
|
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) {
|
if (until == null) {
|
||||||
return false;
|
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.
|
/// Manually trigger a connectivity check.
|
||||||
Future<bool> checkNow() async {
|
Future<bool> checkNow() async {
|
||||||
await _checkServerHealth();
|
await _checkServerHealth();
|
||||||
|
|||||||
Reference in New Issue
Block a user