Add network retry and error state handling to ContactDetailService
epic-contact-detail-and-edit-service-layer-task-004 — Wrap all repository calls in ContactDetailService with retry logic (up to 3 attempts with exponential back-off). Emit typed error states distinguishing network failure, not-found, and permission-denied scenarios so the UI can render the appropriate feedback message without containing branching logic.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Implement a generic retryAsync
Using fake_async in tests is critical here — do not use real delays. If the codebase already has a retry utility in utils/, reuse it rather than creating a duplicate.
Testing Requirements
Unit tests using flutter_test with fake_async: (1) Verify that a transient network error triggers exactly 3 total attempts before emitting ContactDetailError(networkFailure). (2) Verify that delays between retries are approximately 1s and 2s respectively. (3) Verify that a 404/notFound response emits ContactDetailError(notFound) on the first attempt with no retries. (4) Verify that a 403/permissionDenied response emits ContactDetailError(permissionDenied) immediately.
(5) Verify that a successful response on the second attempt (after one transient failure) emits ContactDetailLoaded without an error state. (6) Verify the loading state is maintained throughout the retry cycle.
Parallel fetching of profile, activity history, and assignment status from contact-detail-service may produce race conditions where partial state is emitted to the UI before all fetches complete, resulting in flickering or incorrect loading indicators.
Mitigation & Contingency
Mitigation: Use Future.wait or a single composed BLoC event that only emits a loaded state once all three futures resolve. Define a strict state machine: initial → loading → loaded/error with no intermediate partial-loaded states emitted to the UI.
Contingency: If parallelism proves unreliable in testing, fall back to sequential fetching with a combined loading indicator. The 500ms target may need to be renegotiated with stakeholders if sequential fetching exceeds it on slow connections.
The partial-field update pattern in contact-edit-service assumes the contact record has not changed between when the edit screen was loaded and when the save is submitted. Concurrent edits by another coordinator could cause the earlier editor's save to silently overwrite the later one.
Mitigation & Contingency
Mitigation: Include an updated_at timestamp in the PATCH request and configure Supabase to reject updates where the server-side timestamp differs from the client's version. Return a 409-equivalent error that the service maps to a user-readable conflict message.
Contingency: If optimistic locking is too complex for initial delivery, implement a simple 'reload and retry' flow: on save error, reload the contact detail and prompt the coordinator to re-apply their changes manually.