Implement ActivityRepository with optimistic insert pattern
epic-quick-activity-registration-data-infrastructure-task-005 — Implement ActivityRepository as the central data access object. The core method is insertActivity(ActivityRecord draft) → Stream<ActivityRecord>: immediately emit a locally-constructed ActivityRecord with a temporary 'local_' UUID and SyncStatus.pending, then perform the Supabase insert asynchronously and emit the server-confirmed record (with real ID) on success, or emit with SyncStatus.failed on error. Also provide getActivities(String peerId) and deleteActivity(String id). This optimistic pattern is the architectural foundation consumed by all higher layers.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Implementation Notes
Use a StreamController
Keep SyncStatus as an enum defined in the domain layer, not in this file. For getActivities and deleteActivity, simple Future-returning methods are correct — the optimistic pattern applies only to inserts because reads and deletes are idempotent from a UI perspective. Error wrapping: catch PostgrestException and rethrow as a domain-level ActivityRepositoryException.
Testing Requirements
Unit tests using flutter_test with mocked SupabaseActivityClient (mocktail preferred). Required test cases: (1) success path stream emits pending then synced with correct IDs, (2) failure path stream emits pending then failed and retains local ID, (3) stream closes after second emission in both paths, (4) getActivities delegates to client with correct peerId, (5) deleteActivity delegates to client and re-throws domain exception on error. Use StreamMatcher (emitsInOrder, emitsDone) for stream assertions. Aim for 100% branch coverage on ActivityRepository.
No integration tests in this task — those belong to a separate integration test suite.
The optimistic insert pattern requires reconciling temporary local IDs with server-assigned IDs after the async Supabase write completes. If reconciliation logic is incorrect, the UI may display stale records, duplicate entries may appear, or subsequent operations (edit, delete) may target the wrong record ID, corrupting data integrity.
Mitigation & Contingency
Mitigation: Define a clear contract for temporary ID generation (e.g., UUID prefixed with 'local-') and implement a dedicated reconciliation method in ActivityRepository that atomically swaps the temporary ID. Write integration tests that simulate the full optimistic → confirm cycle.
Contingency: If reconciliation proves too complex, fall back to a simpler non-optimistic insert with a loading spinner for the network round-trip. The UX degrades slightly but correctness is preserved. Re-introduce optimistic behaviour once the pattern is stable.
Supabase row-level security policies on the activities table may not be configured to match the access patterns required by the client. If RLS blocks inserts or selects for the authenticated peer mentor session, all activity registration operations will silently fail or return empty results, which is difficult to diagnose in production.
Mitigation & Contingency
Mitigation: Define and test RLS policies in a dedicated Supabase migration script as part of this epic. Create integration tests that execute against a local Supabase instance with RLS enabled, covering insert, select by peer mentor ID, and denial of cross-mentor access.
Contingency: Maintain a fallback service-role client path (server-side only) that can be activated via a feature flag if client-side RLS is blocking legitimate operations while policies are corrected.
SharedPreferences on Flutter can become corrupted if the app crashes mid-write or if the device runs out of storage. A corrupted last-used activity type preference would cause the defaults manager to return null or an invalid ID, breaking the zero-interaction happy path.
Mitigation & Contingency
Mitigation: Wrap all LocalStorageAdapter reads in try/catch with typed safe defaults. Validate the retrieved activity type ID against the known list before returning it. Use atomic write operations where the platform supports them.
Contingency: If the preference store is corrupted, silently reset to the hardcoded default (first activity type alphabetically or 'general') and log a warning. The user loses their last-used preference but the app remains functional.