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/milestones_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

68 lines
2.0 KiB
Dart

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;
final FabledDatabase _db;
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,
}) 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,
) async {
final updated = await _api.update(projectId, milestoneId, fields);
await _db.upsertMilestone(updated);
return updated;
}
Future<void> delete(int projectId, int milestoneId) async {
await _api.delete(projectId, milestoneId);
await _db.deleteMilestone(milestoneId);
}
}