Files
fin-tracker/app/lib/features/portfolio/providers.dart
T
Dmitry 62d36aa3e8 feat(app): новый визуальный язык, верхняя навигация, портфели, создание счетов и ручные события
Тема и общие виджеты (AssetIcon, ServiceMark, HelpTip, глоссарий), верхняя панель TopNav и вкладки Аналитики вместо NavSidebar, иконки PWA. Экраны: портфели, создание брокерского счёта, ручное событие, правка инструмента, Обзор карточками по скоупам и таблица активов, пересчёт метрик с опросом /metrics/status. design-system.md обновлён.
2026-09-19 22:14:21 +03:00

115 lines
3.9 KiB
Dart

import 'package:fintracker_api/fintracker_api.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../core/api/api_client.dart';
import '../../core/cache/cached.dart';
import 'benchmarks_card.dart' show benchmarkRowsProvider;
/// The reporting unit every portfolio screen is scoped to: `all`, `account:<id>` or
/// `portfolio:<id>`. 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<String>((ref) => 'all');
/// A small cross-screen selector (Портфель, Доходы, Налоги, the goal dialog), not a screen's
/// own primary read — left un-cached per `docs/ai/offline-cache.md`. The underlying `GET`
/// still gets served from the shared cache transparently on a network failure; this provider
/// just does not surface `fetchedAt` for a banner.
final scopesProvider = FutureProvider.autoDispose<List<ScopeOut>>((ref) async {
final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsScopes();
return r.data ?? const [];
});
/// See `docs/ai/offline-cache.md`.
final portfolioSummaryProvider = FutureProvider.autoDispose<Cached<SummaryOut>>(
(ref) async {
final scope = ref.watch(scopeProvider);
final r = await ref
.watch(apiProvider)
.getAnalyticsApi()
.analyticsSummary(scope: scope);
return r.cached;
},
);
final holdingsProvider = FutureProvider.autoDispose<Cached<List<HoldingOut>>>((
ref,
) async {
final scope = ref.watch(scopeProvider);
final r = await ref
.watch(apiProvider)
.getAnalyticsApi()
.analyticsHoldings(scope: scope);
return Cached(
r.data ?? const [],
fetchedAt: r.extra['fetchedAt'] as DateTime?,
);
});
final portfolioReturnsProvider =
FutureProvider.autoDispose<Cached<List<ReturnsOut>>>((ref) async {
final scope = ref.watch(scopeProvider);
final r = await ref
.watch(apiProvider)
.getAnalyticsApi()
.analyticsReturns(scope: scope);
return Cached(
r.data ?? const [],
fetchedAt: r.extra['fetchedAt'] as DateTime?,
);
});
final allocationProvider =
FutureProvider.autoDispose<Cached<List<AllocationBucket>>>((ref) async {
final scope = ref.watch(scopeProvider);
final r = await ref
.watch(apiProvider)
.getAnalyticsApi()
.analyticsAllocation(scope: scope);
return Cached(
r.data ?? const [],
fetchedAt: r.extra['fetchedAt'] as DateTime?,
);
});
/// Daily portfolio value for the last year, for the chart on Позиции.
final valueSeriesProvider = FutureProvider.autoDispose<Cached<List<ValueDay>>>((
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 Cached(
r.data ?? const [],
fetchedAt: r.extra['fetchedAt'] as DateTime?,
);
});
final instrumentProvider = FutureProvider.autoDispose
.family<Cached<InstrumentDetail>, int>((ref, instrumentId) async {
final scope = ref.watch(scopeProvider);
final r = await ref
.watch(apiProvider)
.getInstrumentsApi()
.instrumentsGet(instrumentId: instrumentId, scope: scope);
return r.cached;
});
/// 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);
}