Service Layer medium complexity mobile
2
Dependencies
3
Dependents
0
Entities
0
Integrations

Description

Riverpod AsyncNotifier that owns the stats loading lifecycle for a given role and time window. Triggers an initial fetch on construction and re-fetches whenever the time window parameter changes or a new activity insert event is received. Exposes AsyncValue<StatsSnapshot> to the UI layer.

Feature: Activity Statistics Dashboard

stats-notifier

Summaries

The Stats Async Notifier is the engine that ensures users always see up-to-date performance metrics without manual refreshes, directly contributing to a seamless and trustworthy product experience. By automatically re-fetching statistics whenever a user logs new activity or changes their selected time window, the component eliminates stale data that would erode user confidence in the platform's accuracy. For products where engagement metrics drive user motivation and habit formation — such as fitness, learning, or productivity apps — real-time data freshness is a critical retention lever. This component reduces the operational cost of user-reported data discrepancies and supports a premium, polished user experience that justifies subscription pricing and competitive differentiation.

The Stats Async Notifier is a medium-complexity Riverpod AsyncNotifier with two upstream dependencies — the stats repository and the chapter scope resolver — both of which are on the critical path for this component's delivery. Project scheduling should account for these dependencies being completed and stable before integration testing can begin. The reactive re-fetch logic (on time window change and on activity insert events) introduces multiple state transition paths that require thorough unit testing, including edge cases like rapid window changes and concurrent insert events. The ref.listen and WidgetRef.refresh patterns must be validated against the target Riverpod version to avoid deprecation-related rework.

Plan for integration testing alongside the stats repository and budget time for state management edge case coverage.

StatsAsyncNotifier extends Riverpod's AsyncNotifier, parameterized by TimeWindow, and manages the full AsyncValue lifecycle including loading, data, and error states. The build method triggers the initial fetch by delegating to the stats repository, with chapter scope resolved via chapter-scope-resolver to ensure role-appropriate data scoping. changeTimeWindow invalidates and rebuilds the notifier with the new parameter, triggering a fresh fetch. onActivityInserted responds to event bus or provider listener signals to refresh state reactively without full rebuilds where possible.

The refresh method provides an explicit escape hatch for navigation-triggered refreshes via ref.listen or WidgetRef.refresh. Error states must be surfaced to the UI as AsyncValue.error — avoid swallowing exceptions. Ensure the notifier is family-scoped by TimeWindow to prevent cross-window state contamination.

Responsibilities

  • Manage AsyncValue state for stats loading, data, and error
  • Re-fetch on time window change and on new activity insert events
  • Refresh on navigation using ref.listen or WidgetRef.refresh pattern
  • Delegate data fetching to stats repository

Interfaces

build(TimeWindow window) Future<StatsSnapshot>
refresh()
onActivityInserted()
changeTimeWindow(TimeWindow window)
getSnapshot() AsyncValue<StatsSnapshot>

Relationships

Dependencies (2)

Components this component depends on

Dependents (3)

Components that depend on this component

API Contract

View full contract →
REST /api/v1/stats-notifications 6 endpoints
GET /api/v1/stats-notifications
GET /api/v1/stats-notifications/:notificationId
POST /api/v1/stats-notifications
PUT /api/v1/stats-notifications/:notificationId
DELETE /api/v1/stats-notifications/:notificationId
POST /api/v1/stats-notifications/:notificationId/refresh