feat(offline): tier 2 phase 3 — write queue with optimistic UI
Offline writes now queue + apply optimistically instead of throwing. On reconnect, the queue drains automatically; conflicts and rejections surface as one-time SnackBars so the user knows their edit didn't land. - Drift schema v3: `pending_writes` table (verb + payload + baseline + tries + last_error). Negative ids serve as cache placeholders for offline-created rows until replay assigns the server id. - Each write repository (notes, tasks, projects, milestones, events) now catches NetworkException, applies the change to cache (with a fresh `nextTempId()` for creates), and enqueues the API call. - Edits to a still-queued offline-created row coalesce into the original create payload — only one server call per row, in order. - Deletes of still-queued offline-created rows drop the queue entry and the cache row; no server call ever happens. - New WriteQueue service drains the queue oldest-first. Server-wins on `updated_at` baseline check (notes/tasks/projects); last-writer-wins for milestones/events (no getOne available). 4xx → drop + surface as `rejected`; conflict → drop + `overwritten`; 404 on update → drop + `missing`; 5xx/network → keep + retry next online cycle. - Replay fires on AuthStatus → authenticated transitions (cold-start, came-back-online, login). - OfflineBanner shows "Retry (N)" with the pending-writes count. - Distinct ServerException added so 4xx no longer masquerade as NetworkException — the queue can drop them instead of looping. flutter analyze clean; 21 tests pass (3 new for QueueFailure messaging). Phase 4 (read-only UI indicators on cached/temp rows) still ahead. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -3,8 +3,9 @@ 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.
|
||||
/// Milestones repository with read-through caching (Phase 1+2) and
|
||||
/// offline-write queueing (Phase 3). See `notes_repository.dart` for the
|
||||
/// full pattern.
|
||||
class MilestonesRepository {
|
||||
final MilestonesApi _api;
|
||||
final FabledDatabase _db;
|
||||
@@ -40,14 +41,45 @@ class MilestonesRepository {
|
||||
String? description,
|
||||
int orderIndex = 0,
|
||||
}) async {
|
||||
final milestone = await _api.create(
|
||||
projectId,
|
||||
title: title,
|
||||
description: description,
|
||||
orderIndex: orderIndex,
|
||||
);
|
||||
await _db.upsertMilestone(milestone);
|
||||
return milestone;
|
||||
try {
|
||||
final milestone = await _api.create(
|
||||
projectId,
|
||||
title: title,
|
||||
description: description,
|
||||
orderIndex: orderIndex,
|
||||
);
|
||||
await _db.upsertMilestone(milestone);
|
||||
return milestone;
|
||||
} on NetworkException {
|
||||
final tempId = await _db.nextTempId();
|
||||
final now = DateTime.now();
|
||||
final optimistic = Milestone(
|
||||
id: tempId,
|
||||
projectId: projectId,
|
||||
title: title,
|
||||
description: description,
|
||||
status: 'active',
|
||||
orderIndex: orderIndex,
|
||||
total: 0,
|
||||
completed: 0,
|
||||
pct: 0.0,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
await _db.upsertMilestone(optimistic);
|
||||
await _db.enqueuePending(
|
||||
domain: kSyncDomainMilestones,
|
||||
verb: kWriteVerbCreate,
|
||||
tempId: tempId,
|
||||
payload: {
|
||||
'project_id': projectId,
|
||||
'title': title,
|
||||
'description': description,
|
||||
'order_index': orderIndex,
|
||||
},
|
||||
);
|
||||
return optimistic;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Milestone> update(
|
||||
@@ -55,13 +87,78 @@ class MilestonesRepository {
|
||||
int milestoneId,
|
||||
Map<String, dynamic> fields,
|
||||
) async {
|
||||
final updated = await _api.update(projectId, milestoneId, fields);
|
||||
await _db.upsertMilestone(updated);
|
||||
return updated;
|
||||
try {
|
||||
final updated = await _api.update(projectId, milestoneId, fields);
|
||||
await _db.upsertMilestone(updated);
|
||||
return updated;
|
||||
} on NetworkException {
|
||||
final cached = (await _db.getMilestonesForProject(projectId))
|
||||
.where((m) => m.id == milestoneId)
|
||||
.firstOrNull;
|
||||
if (cached == null) rethrow;
|
||||
final optimistic = _applyMilestoneFields(cached, fields);
|
||||
await _db.upsertMilestone(optimistic);
|
||||
if (milestoneId < 0) {
|
||||
final queued =
|
||||
await _db.findQueuedCreate(kSyncDomainMilestones, milestoneId);
|
||||
if (queued != null) {
|
||||
// Carry project_id through coalesced payload — replay needs it.
|
||||
final merged = <String, dynamic>{
|
||||
'project_id': projectId,
|
||||
...fields,
|
||||
};
|
||||
await _db.updatePendingPayload(queued.id, merged);
|
||||
return optimistic;
|
||||
}
|
||||
}
|
||||
await _db.enqueuePending(
|
||||
domain: kSyncDomainMilestones,
|
||||
verb: kWriteVerbUpdate,
|
||||
targetId: milestoneId,
|
||||
payload: <String, dynamic>{'project_id': projectId, ...fields},
|
||||
baselineUpdatedAt: cached.updatedAt,
|
||||
);
|
||||
return optimistic;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> delete(int projectId, int milestoneId) async {
|
||||
await _api.delete(projectId, milestoneId);
|
||||
await _db.deleteMilestone(milestoneId);
|
||||
try {
|
||||
await _api.delete(projectId, milestoneId);
|
||||
await _db.deleteMilestone(milestoneId);
|
||||
} on NetworkException {
|
||||
if (milestoneId < 0) {
|
||||
final queued =
|
||||
await _db.findQueuedCreate(kSyncDomainMilestones, milestoneId);
|
||||
if (queued != null) await _db.deletePending(queued.id);
|
||||
await _db.deleteMilestone(milestoneId);
|
||||
return;
|
||||
}
|
||||
await _db.deleteMilestone(milestoneId);
|
||||
await _db.enqueuePending(
|
||||
domain: kSyncDomainMilestones,
|
||||
verb: kWriteVerbDelete,
|
||||
targetId: milestoneId,
|
||||
payload: {'project_id': projectId},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Milestone _applyMilestoneFields(Milestone m, Map<String, dynamic> f) {
|
||||
return Milestone(
|
||||
id: m.id,
|
||||
projectId: m.projectId,
|
||||
title: f['title'] as String? ?? m.title,
|
||||
description: f.containsKey('description')
|
||||
? f['description'] as String?
|
||||
: m.description,
|
||||
status: f['status'] as String? ?? m.status,
|
||||
orderIndex: f['order_index'] as int? ?? m.orderIndex,
|
||||
total: m.total,
|
||||
completed: m.completed,
|
||||
pct: m.pct,
|
||||
createdAt: m.createdAt,
|
||||
updatedAt: m.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user