import 'package:fintracker_api/fintracker_api.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../core/api/api_client.dart'; import 'benchmarks_card.dart' show benchmarkRowsProvider; /// The reporting unit every portfolio screen is scoped to: `all`, `account:` or /// `portfolio:`. Held in one place so switching it on Позиции also switches Аллокация /// and the instrument card — three screens showing different scopes would be a trap. final scopeProvider = StateProvider((ref) => 'all'); final scopesProvider = FutureProvider.autoDispose>((ref) async { final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsScopes(); return r.data ?? const []; }); final portfolioSummaryProvider = FutureProvider.autoDispose((ref) async { final scope = ref.watch(scopeProvider); final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsSummary(scope: scope); return r.data!; }); final holdingsProvider = FutureProvider.autoDispose>((ref) async { final scope = ref.watch(scopeProvider); final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsHoldings(scope: scope); return r.data ?? const []; }); final portfolioReturnsProvider = FutureProvider.autoDispose>((ref) async { final scope = ref.watch(scopeProvider); final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsReturns(scope: scope); return r.data ?? const []; }); final allocationProvider = FutureProvider.autoDispose>((ref) async { final scope = ref.watch(scopeProvider); final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsAllocation(scope: scope); return r.data ?? const []; }); /// Daily portfolio value for the last year, for the chart on Позиции. final valueSeriesProvider = FutureProvider.autoDispose>((ref) async { final scope = ref.watch(scopeProvider); final now = DateTime.now(); final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsValueSeries( scope: scope, from: now.subtract(const Duration(days: 365)), to: now, ); return r.data ?? const []; }); final instrumentProvider = FutureProvider.autoDispose.family((ref, instrumentId) async { final scope = ref.watch(scopeProvider); final r = await ref .watch(apiProvider) .getInstrumentsApi() .instrumentsGet(instrumentId: instrumentId, scope: scope); return r.data!; }); /// Every portfolio provider, refreshed together after a metrics rebuild or a pull-to-refresh. void invalidatePortfolioProviders(WidgetRef ref) { ref.invalidate(portfolioSummaryProvider); ref.invalidate(holdingsProvider); ref.invalidate(portfolioReturnsProvider); ref.invalidate(allocationProvider); ref.invalidate(valueSeriesProvider); ref.invalidate(instrumentProvider); // the benchmark block lives on Позиции and is scoped the same way ref.invalidate(benchmarkRowsProvider); }