Импорт (/imports, /instruments/pending) и фаза 4 (/goals, /income, /rebalance, /tax, аналитика-хаб с benchmarks_card) — по docs/ai/import-contract.md и docs/ai/phase4-contract.md. file_picker для загрузки отчёта.
74 lines
3.0 KiB
Dart
74 lines
3.0 KiB
Dart
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:<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');
|
|
|
|
final scopesProvider = FutureProvider.autoDispose<List<ScopeOut>>((ref) async {
|
|
final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsScopes();
|
|
return r.data ?? const [];
|
|
});
|
|
|
|
final portfolioSummaryProvider = FutureProvider.autoDispose<SummaryOut>((ref) async {
|
|
final scope = ref.watch(scopeProvider);
|
|
final r = await ref.watch(apiProvider).getAnalyticsApi().analyticsSummary(scope: scope);
|
|
return r.data!;
|
|
});
|
|
|
|
final holdingsProvider = FutureProvider.autoDispose<List<HoldingOut>>((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<List<ReturnsOut>>((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<List<AllocationBucket>>((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<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 r.data ?? const [];
|
|
});
|
|
|
|
final instrumentProvider =
|
|
FutureProvider.autoDispose.family<InstrumentDetail, int>((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);
|
|
}
|