This repository has been archived on 2026-06-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FabledApp/lib/data/repositories/chat_repository.dart
T
bvandeusen fe10067761 feat(offline): tier 2 phase 2 — extend cache to tasks/projects/milestones/events/conversations
Phase 1 cached only notes. Phase 2 brings the read-through pattern to every
domain the app reads in bulk:

- Drift schema v2: 5 new cached_* tables + onUpgrade migration. Calendar
  events use range-scoped read/write (replaceEventsInRange) so disjoint
  month fetches don't clobber each other; milestones are per-project.
- Wrap TasksRepository, ProjectsRepository, MilestonesRepository, and
  ChatRepository (conversation list only) with NetworkException fallback.
  Writes hit the API then sync the cache.
- New EventsRepository wrapping EventsApi; calendar_provider and
  event_form_sheet repointed at the repository.
- OfflineBanner now surfaces getLatestSync() — most-recent timestamp
  across all domains — so the hint reads sensibly regardless of screen.

flutter analyze clean; existing tests pass. Phase 3 (offline write queue)
and Phase 4 (read-only UI indicators) still to come.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-28 21:22:06 -04:00

47 lines
1.5 KiB
Dart

import '../../core/exceptions.dart';
import '../api/chat_api.dart';
export '../api/chat_api.dart'
show ChatStreamEvent, ChatTextChunk, ChatStatusUpdate, ChatToolCall;
import '../local/database.dart';
import '../models/conversation.dart';
import '../models/message.dart';
/// Chat repository with read-through caching for the conversation list only.
/// Messages and the live SSE stream are intentionally not cached — they are
/// per-conversation, large, and require a live network anyway.
class ChatRepository {
final ChatApi _api;
final FabledDatabase _db;
const ChatRepository(this._api, this._db);
Future<List<Conversation>> getConversations() async {
try {
final conversations = await _api.getConversations();
await _db.replaceAllConversations(conversations);
return conversations;
} on NetworkException {
final cached = await _db.getAllConversations();
if (cached.isEmpty) rethrow;
return cached;
}
}
Future<Conversation> createConversation(String title) =>
_api.createConversation(title);
Future<void> deleteConversation(int id) async {
await _api.deleteConversation(id);
await _db.deleteConversation(id);
}
Future<(Conversation, List<Message>)> getMessages(int conversationId) =>
_api.getMessages(conversationId);
Future<void> sendMessage(int conversationId, String content) =>
_api.sendMessage(conversationId, content);
Stream<ChatStreamEvent> streamGeneration(int conversationId) =>
_api.streamGeneration(conversationId);
}