@@ -6,13 +6,15 @@ import '../database.dart' as db;
66import '../../core/api_client.dart' ;
77import '../mappers/incident_mapper.dart' ;
88import '../../services/p2p_service.dart' ;
9+ import '../../services/polygon_cache_service.dart' ;
910
1011class IncidentRepository {
1112 final db.AppDatabase _db;
1213 final ApiClient _apiClient;
1314 final P2PService _p2pService;
15+ final PolygonCacheService _polygonCache;
1416
15- IncidentRepository (this ._db, this ._apiClient, this ._p2pService) {
17+ IncidentRepository (this ._db, this ._apiClient, this ._p2pService, this ._polygonCache ) {
1618 // Day-16: Listen for incoming incident_create messages
1719 _p2pService.incomingIncidents.listen ((dto) {
1820 _handleIncomingP2PIncident (dto);
@@ -66,6 +68,9 @@ class IncidentRepository {
6668 );
6769 _p2pService.broadcastIncident (incidentToBroadcast);
6870
71+ // Day 27: Generate and cache polygon for newly created incident
72+ _polygonCache.updateFromIncident (incidentToBroadcast);
73+
6974 await _db.transaction (() async {
7075 await _db.into (_db.incidents).insert (
7176 db.IncidentsCompanion .insert (
@@ -214,11 +219,17 @@ class IncidentRepository {
214219 clientId: Value (dto.clientId),
215220 ),
216221 );
222+
223+ // Day 27: Refresh polygon cache after CRDT merge update
224+ debugPrint ('[IncidentRepo] CRDT_MERGE_COMPLETED: $incidentId ' );
225+ final updatedIncident = await getIncident (incidentId);
226+ _polygonCache.updateFromIncident (updatedIncident);
217227 } else {
218228 debugPrint (
219229 '[IncidentRepo] CRDT_MERGE_APPLIED: Local state kept for $incidentId ' );
220230 debugPrint ('[IncidentRepo] CONFLICT_RESOLVED: local wins' );
221231 }
232+ debugPrint ('[IncidentRepo] P2P_INCIDENT_RECEIVED: $incidentId ' );
222233 return ;
223234 }
224235
@@ -240,6 +251,11 @@ class IncidentRepository {
240251
241252 debugPrint (
242253 '[IncidentRepo] P2P incident inserted: $incidentId ' );
254+ debugPrint ('[IncidentRepo] P2P_INCIDENT_RECEIVED: $incidentId ' );
255+
256+ // Day 27: Generate and cache polygon after P2P insert
257+ final insertedIncident = await getIncident (incidentId);
258+ _polygonCache.updateFromIncident (insertedIncident);
243259 }
244260
245261 // ─── Day-17: State Synchronization Handlers ────────────────────────────
0 commit comments