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:
2026-04-28 21:22:06 -04:00
parent 6ef658558a
commit fe10067761
12 changed files with 5376 additions and 104 deletions
+76 -19
View File
@@ -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);
}
}