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

@@ -14,7 +14,7 @@ sealed class Conversation with _$Conversation {
String? model,
String? systemPrompt,
@Default([]) List<ChatMessage> messages,
@Default({}) Map<String, dynamic> metadata,
@Default({}) @_MetadataConverter() Map<String, dynamic> metadata,
@Default(false) bool pinned,
@Default(false) bool archived,
String? shareId,
@@ -25,3 +25,22 @@ sealed class Conversation with _$Conversation {
factory Conversation.fromJson(Map<String, dynamic> 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;
}