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>
This commit is contained in:
@@ -1,21 +1,46 @@
|
||||
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;
|
||||
const ChatRepository(this._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<List<Conversation>> getConversations() => _api.getConversations();
|
||||
Future<Conversation> createConversation(String title) =>
|
||||
_api.createConversation(title);
|
||||
Future<void> deleteConversation(int id) => _api.deleteConversation(id);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import '../../core/exceptions.dart';
|
||||
import '../api/events_api.dart';
|
||||
import '../local/database.dart';
|
||||
import '../models/calendar_event.dart';
|
||||
|
||||
/// Calendar events repository with read-through caching for Tier 2 offline
|
||||
/// support. Range-scoped: each `getEvents(from, to)` mirrors that window into
|
||||
/// the cache (events outside the window are left alone) so successive
|
||||
/// disjoint range fetches don't clobber each other.
|
||||
class EventsRepository {
|
||||
final EventsApi _api;
|
||||
final FabledDatabase _db;
|
||||
|
||||
const EventsRepository(this._api, this._db);
|
||||
|
||||
Future<List<CalendarEvent>> getEvents(DateTime from, DateTime to) async {
|
||||
try {
|
||||
final events = await _api.getEvents(from, to);
|
||||
await _db.replaceEventsInRange(from, to, events);
|
||||
return events;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getEventsInRange(from, to);
|
||||
if (cached.isEmpty) rethrow;
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<CalendarEvent> createEvent(Map<String, dynamic> payload) async {
|
||||
final event = await _api.createEvent(payload);
|
||||
await _db.upsertEvent(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
Future<CalendarEvent> updateEvent(
|
||||
int id, Map<String, dynamic> fields) async {
|
||||
final updated = await _api.updateEvent(id, fields);
|
||||
await _db.upsertEvent(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> deleteEvent(int id) async {
|
||||
await _api.deleteEvent(id);
|
||||
await _db.deleteEvent(id);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,67 @@
|
||||
import '../../core/exceptions.dart';
|
||||
import '../api/milestones_api.dart';
|
||||
import '../local/database.dart';
|
||||
import '../models/milestone.dart';
|
||||
|
||||
/// Milestones repository with read-through caching for Tier 2 offline support.
|
||||
/// Mirrors the pattern in NotesRepository — see that file for full notes.
|
||||
class MilestonesRepository {
|
||||
final MilestonesApi _api;
|
||||
const MilestonesRepository(this._api);
|
||||
final FabledDatabase _db;
|
||||
|
||||
Future<List<Milestone>> getAll(int projectId, {String? status}) =>
|
||||
_api.getAll(projectId, status: status);
|
||||
const MilestonesRepository(this._api, this._db);
|
||||
|
||||
Future<List<Milestone>> getAll(int projectId, {String? status}) async {
|
||||
try {
|
||||
final milestones = await _api.getAll(projectId, status: status);
|
||||
// Only mirror the full per-project list to cache on the unfiltered
|
||||
// fetch; a status filter would otherwise drop entries from cache.
|
||||
if (status == null) {
|
||||
await _db.replaceMilestonesForProject(projectId, milestones);
|
||||
} else {
|
||||
for (final m in milestones) {
|
||||
await _db.upsertMilestone(m);
|
||||
}
|
||||
}
|
||||
return milestones;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getMilestonesForProject(projectId);
|
||||
if (cached.isEmpty) rethrow;
|
||||
if (status != null) {
|
||||
return cached.where((m) => m.status == status).toList();
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Milestone> create(
|
||||
int projectId, {
|
||||
required String title,
|
||||
String? description,
|
||||
int orderIndex = 0,
|
||||
}) =>
|
||||
_api.create(projectId,
|
||||
title: title, description: description, orderIndex: orderIndex);
|
||||
}) async {
|
||||
final milestone = await _api.create(
|
||||
projectId,
|
||||
title: title,
|
||||
description: description,
|
||||
orderIndex: orderIndex,
|
||||
);
|
||||
await _db.upsertMilestone(milestone);
|
||||
return milestone;
|
||||
}
|
||||
|
||||
Future<Milestone> update(
|
||||
int projectId, int milestoneId, Map<String, dynamic> fields) =>
|
||||
_api.update(projectId, milestoneId, fields);
|
||||
int projectId,
|
||||
int milestoneId,
|
||||
Map<String, dynamic> fields,
|
||||
) async {
|
||||
final updated = await _api.update(projectId, milestoneId, fields);
|
||||
await _db.upsertMilestone(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> delete(int projectId, int milestoneId) =>
|
||||
_api.delete(projectId, milestoneId);
|
||||
Future<void> delete(int projectId, int milestoneId) async {
|
||||
await _api.delete(projectId, milestoneId);
|
||||
await _db.deleteMilestone(milestoneId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,9 +90,4 @@ class NotesRepository {
|
||||
await _api.delete(id);
|
||||
await _db.deleteNote(id);
|
||||
}
|
||||
|
||||
/// Last successful `getAll` sync timestamp. Surfaced by the OfflineBanner
|
||||
/// when the network is unreachable so the user knows how stale the
|
||||
/// cached notes list is.
|
||||
Future<DateTime?> lastSyncedAt() => _db.getLastSync(kSyncDomainNotes);
|
||||
}
|
||||
|
||||
@@ -1,31 +1,87 @@
|
||||
import '../../core/exceptions.dart';
|
||||
import '../api/projects_api.dart';
|
||||
import '../local/database.dart';
|
||||
import '../models/project.dart';
|
||||
|
||||
/// Projects repository with read-through caching for Tier 2 offline support.
|
||||
/// Mirrors the pattern in NotesRepository — see that file for full notes.
|
||||
///
|
||||
/// `getAll`'s sort/order/status query parameters are server-side filters; the
|
||||
/// cache stores the unfiltered list as last seen. Offline fallback returns
|
||||
/// the full cache regardless of the requested filters — close enough for a
|
||||
/// "view what you had" experience while disconnected.
|
||||
class ProjectsRepository {
|
||||
final ProjectsApi _api;
|
||||
const ProjectsRepository(this._api);
|
||||
final FabledDatabase _db;
|
||||
|
||||
const ProjectsRepository(this._api, this._db);
|
||||
|
||||
Future<List<Project>> getAll({
|
||||
String? status,
|
||||
String sort = 'updated_at',
|
||||
String order = 'desc',
|
||||
}) =>
|
||||
_api.getAll(status: status, sort: sort, order: order);
|
||||
Future<Project> getOne(int id) => _api.getOne(id);
|
||||
}) async {
|
||||
try {
|
||||
final projects =
|
||||
await _api.getAll(status: status, sort: sort, order: order);
|
||||
// Only refresh the cache on the unfiltered default fetch — otherwise a
|
||||
// status=archived call would clobber the active-projects cache.
|
||||
if (status == null) {
|
||||
await _db.replaceAllProjects(projects);
|
||||
} else {
|
||||
for (final p in projects) {
|
||||
await _db.upsertProject(p);
|
||||
}
|
||||
}
|
||||
return projects;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getAllProjects();
|
||||
if (cached.isEmpty) rethrow;
|
||||
if (status != null) {
|
||||
return cached.where((p) => p.status == status).toList();
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Project> getOne(int id) async {
|
||||
try {
|
||||
final project = await _api.getOne(id);
|
||||
await _db.upsertProject(project);
|
||||
return project;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getProject(id);
|
||||
if (cached == null) rethrow;
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Project> create({
|
||||
required String title,
|
||||
String? description,
|
||||
String? goal,
|
||||
String? color,
|
||||
String status = 'active',
|
||||
}) =>
|
||||
_api.create(
|
||||
title: title,
|
||||
description: description,
|
||||
goal: goal,
|
||||
color: color,
|
||||
status: status);
|
||||
Future<Project> update(int id, Map<String, dynamic> fields) =>
|
||||
_api.update(id, fields);
|
||||
Future<void> delete(int id) => _api.delete(id);
|
||||
}) async {
|
||||
final project = await _api.create(
|
||||
title: title,
|
||||
description: description,
|
||||
goal: goal,
|
||||
color: color,
|
||||
status: status,
|
||||
);
|
||||
await _db.upsertProject(project);
|
||||
return project;
|
||||
}
|
||||
|
||||
Future<Project> update(int id, Map<String, dynamic> fields) async {
|
||||
final updated = await _api.update(id, fields);
|
||||
await _db.upsertProject(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete(id);
|
||||
await _db.deleteProject(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,63 @@
|
||||
import '../../core/exceptions.dart';
|
||||
import '../api/tasks_api.dart';
|
||||
import '../local/database.dart';
|
||||
import '../models/task.dart';
|
||||
|
||||
/// Tasks repository with read-through caching for Tier 2 offline support.
|
||||
/// Mirrors the pattern in NotesRepository — see that file for full notes.
|
||||
class TasksRepository {
|
||||
final TasksApi _api;
|
||||
const TasksRepository(this._api);
|
||||
final FabledDatabase _db;
|
||||
|
||||
Future<List<Task>> getAll() => _api.getAll();
|
||||
Future<Task> getOne(int id) => _api.getOne(id);
|
||||
const TasksRepository(this._api, this._db);
|
||||
|
||||
Future<List<Task>> getAll() async {
|
||||
try {
|
||||
final tasks = await _api.getAll();
|
||||
await _db.replaceAllTasks(tasks);
|
||||
return tasks;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getAllTasks();
|
||||
if (cached.isEmpty) rethrow;
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Task> getOne(int id) async {
|
||||
try {
|
||||
final task = await _api.getOne(id);
|
||||
await _db.upsertTask(task);
|
||||
return task;
|
||||
} on NetworkException {
|
||||
final cached = await _db.getTask(id);
|
||||
if (cached == null) rethrow;
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Task>> getByProject(int projectId) async {
|
||||
try {
|
||||
final tasks = await _api.getByProject(projectId);
|
||||
for (final t in tasks) {
|
||||
await _db.upsertTask(t);
|
||||
}
|
||||
return tasks;
|
||||
} on NetworkException {
|
||||
return _db.getTasksByProject(projectId);
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Task>> getSubTasks(int parentId) async {
|
||||
try {
|
||||
final tasks = await _api.getSubTasks(parentId);
|
||||
for (final t in tasks) {
|
||||
await _db.upsertTask(t);
|
||||
}
|
||||
return tasks;
|
||||
} on NetworkException {
|
||||
return _db.getSubTasks(parentId);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Task> create({
|
||||
required String title,
|
||||
@@ -16,22 +67,28 @@ class TasksRepository {
|
||||
DateTime? dueDate,
|
||||
int? projectId,
|
||||
int? parentId,
|
||||
}) =>
|
||||
_api.create(
|
||||
title: title,
|
||||
description: description,
|
||||
status: status,
|
||||
priority: priority,
|
||||
dueDate: dueDate,
|
||||
projectId: projectId,
|
||||
parentId: parentId,
|
||||
);
|
||||
}) async {
|
||||
final task = await _api.create(
|
||||
title: title,
|
||||
description: description,
|
||||
status: status,
|
||||
priority: priority,
|
||||
dueDate: dueDate,
|
||||
projectId: projectId,
|
||||
parentId: parentId,
|
||||
);
|
||||
await _db.upsertTask(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
Future<List<Task>> getByProject(int projectId) => _api.getByProject(projectId);
|
||||
Future<List<Task>> getSubTasks(int parentId) => _api.getSubTasks(parentId);
|
||||
Future<Task> update(int id, Map<String, dynamic> fields) async {
|
||||
final updated = await _api.update(id, fields);
|
||||
await _db.upsertTask(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<Task> update(int id, Map<String, dynamic> fields) =>
|
||||
_api.update(id, fields);
|
||||
|
||||
Future<void> delete(int id) => _api.delete(id);
|
||||
Future<void> delete(int id) async {
|
||||
await _api.delete(id);
|
||||
await _db.deleteTask(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user