Implement Badge BLoC
epic-achievement-badges-ui-task-006 — Implement the badge-bloc BLoC that loads earned badges for the current peer mentor, caches them in memory, reacts to badge-earned domain events to trigger the celebration overlay state, and manages badge-detail-modal open/close state. Include proper loading, error, and empty states. Wire up to badge-repository and badge-definition-repository.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 1 - 540 tasks
Can start after Tier 0 completes
Implementation Notes
Extend Bloc
Merge badge definitions from BadgeDefinitionRepository into the earned list at load time so the UI always receives fully resolved BadgeCardModels without further repository calls. Avoid emitting intermediate states during cache-hit loads to prevent UI flicker. The celebration state should carry the full BadgeDefinition (name, image, description) to allow the overlay widget to render without additional async calls.
Testing Requirements
Use bloc_test package for all BLoC tests. Test sequences: (1) LoadBadges → BadgeLoading → BadgeLoaded; (2) LoadBadges with empty result → BadgeEmpty; (3) LoadBadges with repository error → BadgeError; (4) BadgeEarnedEvent received → BadgeCelebration → (acknowledge) → BadgeLoaded with updated list; (5) second LoadBadges within lifecycle → cache hit, repository not called. Mock both repositories with Mockito or mocktail. Assert stream subscriptions are cancelled in tearDown.
Minimum 95% line coverage.
The badge-earned-celebration overlay must appear within 2 seconds of the triggering activity being saved, but badge evaluation runs server-side in an edge function triggered by a database webhook. Network latency, edge function cold start, and Supabase Realtime delivery delays could cause the overlay to appear late or not at all, breaking the motivational loop.
Mitigation & Contingency
Mitigation: Implement an optimistic UI path: after activity save, badge-bloc immediately checks whether any badge thresholds are crossed client-side using cached stats and badge definitions, showing the overlay speculatively before server confirmation. The server result then reconciles. Subscribe to Supabase Realtime on the earned_badges table for authoritative confirmation.
Contingency: If Realtime delivery is unreliable in production, add a polling fallback: badge-bloc polls for new earned badges 3 seconds after an activity save and shows the overlay if a new record is detected, accepting up to 5-second latency as a fallback SLA.
The celebration overlay uses animation for positive reinforcement, but motion sensitivity (prefers-reduced-motion) and screen reader users require a non-animated or text-only alternative. Failing to handle this risks excluding Blindeforbundet users or triggering vestibular discomfort for motion-sensitive volunteers.
Mitigation & Contingency
Mitigation: Check MediaQuery.disableAnimations in badge-earned-celebration-overlay and skip animation entirely when true, showing a static card instead. Add an ExcludeSemantics wrapper around the decorative animation widget and a separate Semantics node with a live region announcement of the badge name and congratulatory message.
Contingency: If accessibility issues are identified in TestFlight testing with Blindeforbundet's test group, fast-track a patch that defaults to the static card path and gates the animation behind a user preference setting in notification preferences.