feat(app): offline-кэш на остальных экранах — фаза 5
accounts, cashflow, categories, goals, income, portfolio (+instrument), rebalance, tax, rules переведены на Cached<T> по контракту docs/ai/offline-cache.md. Общие/второстепенные селекторы (scopesProvider, categoriesListProvider и т.п.) оставлены как есть — не основной контент экрана. sync_page.dart и settings_page.dart не тронуты (первый — заменён health-page отдельно, второй — чистые действия без списка для баннера).
This commit is contained in:
@@ -11,6 +11,7 @@ library;
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../../core/cache/cached.dart';
|
||||
import '../../../core/utils/json.dart';
|
||||
|
||||
class Goal {
|
||||
@@ -125,11 +126,14 @@ class GoalsApi {
|
||||
|
||||
static const _base = '/api/v1/goals';
|
||||
|
||||
Future<List<Goal>> list() async {
|
||||
/// See `docs/ai/offline-cache.md`: this hand-written client returns `Cached<T>` directly
|
||||
/// (there is no generated `Response<T>` for `GoalsPage` to unwrap `r.cached` from).
|
||||
Future<Cached<List<Goal>>> list() async {
|
||||
final r = await _dio.get<List<dynamic>>(_base);
|
||||
return (r.data ?? const [])
|
||||
final goals = (r.data ?? const [])
|
||||
.map((e) => Goal.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList();
|
||||
return Cached(goals, fetchedAt: r.extra['fetchedAt'] as DateTime?);
|
||||
}
|
||||
|
||||
Future<Goal> create(Goal goal) async {
|
||||
@@ -144,8 +148,11 @@ class GoalsApi {
|
||||
|
||||
Future<void> delete(int id) => _dio.delete<void>('$_base/$id');
|
||||
|
||||
Future<GoalProgress> progress(int id) async {
|
||||
Future<Cached<GoalProgress>> progress(int id) async {
|
||||
final r = await _dio.get<Map<String, dynamic>>('$_base/$id/progress');
|
||||
return GoalProgress.fromJson(r.data ?? const {});
|
||||
return Cached(
|
||||
GoalProgress.fromJson(r.data ?? const {}),
|
||||
fetchedAt: r.extra['fetchedAt'] as DateTime?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ class GoalCard extends ConsumerWidget {
|
||||
AsyncValueView(
|
||||
value: progress,
|
||||
onRetry: () => ref.invalidate(goalProgressProvider(goal.id)),
|
||||
data: (p) => GoalProgressView(goal: goal, progress: p),
|
||||
data: (cached) => GoalProgressView(goal: goal, progress: cached.data),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/cache/cached.dart';
|
||||
import '../../core/utils/json.dart';
|
||||
import '../../core/widgets/async_value_view.dart';
|
||||
import '../../core/widgets/empty_state.dart';
|
||||
import '../../core/widgets/stale_banner.dart';
|
||||
import 'data/goals_api.dart';
|
||||
import 'goal_card.dart';
|
||||
import 'goal_edit_dialog.dart';
|
||||
@@ -78,6 +80,13 @@ class _GoalsPageState extends ConsumerState<GoalsPage> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final goals = ref.watch(goalsProvider);
|
||||
// Every card's own progress fetch counts toward the one banner too — a fresh list with a
|
||||
// stale progress card is still an offline dashboard, just not a visibly empty one.
|
||||
final goalIds = [for (final g in goals.valueOrNull?.data ?? const <Goal>[]) g.id];
|
||||
final stale = oldestFetch([
|
||||
goals.valueOrNull?.fetchedAt,
|
||||
for (final id in goalIds) ref.watch(goalProgressProvider(id)).valueOrNull?.fetchedAt,
|
||||
]);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -105,12 +114,14 @@ class _GoalsPageState extends ConsumerState<GoalsPage> {
|
||||
child: AsyncValueView(
|
||||
value: goals,
|
||||
onRetry: () => ref.invalidate(goalsProvider),
|
||||
data: (all) {
|
||||
data: (cached) {
|
||||
final all = cached.data;
|
||||
final rows = _showArchived ? all : all.where((g) => !g.archived).toList();
|
||||
if (rows.isEmpty) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
if (stale != null) StaleBanner(fetchedAt: stale),
|
||||
const SizedBox(height: 48),
|
||||
EmptyState(
|
||||
icon: Icons.flag_outlined,
|
||||
@@ -125,6 +136,7 @@ class _GoalsPageState extends ConsumerState<GoalsPage> {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 88),
|
||||
children: [
|
||||
if (stale != null) StaleBanner(fetchedAt: stale),
|
||||
for (final g in rows)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/api/api_client.dart';
|
||||
import '../../core/cache/cached.dart';
|
||||
import 'data/goals_api.dart';
|
||||
|
||||
final goalsApiProvider = Provider<GoalsApi>((ref) => GoalsApi(ref.watch(apiProvider).dio));
|
||||
|
||||
/// See `docs/ai/offline-cache.md`.
|
||||
final goalsProvider =
|
||||
FutureProvider.autoDispose<List<Goal>>((ref) => ref.watch(goalsApiProvider).list());
|
||||
FutureProvider.autoDispose<Cached<List<Goal>>>((ref) => ref.watch(goalsApiProvider).list());
|
||||
|
||||
/// Progress is computed server-side and refetched per goal — the client never projects
|
||||
/// anything itself.
|
||||
final goalProgressProvider =
|
||||
FutureProvider.autoDispose.family<GoalProgress, int>((ref, id) async {
|
||||
FutureProvider.autoDispose.family<Cached<GoalProgress>, int>((ref, id) async {
|
||||
return ref.watch(goalsApiProvider).progress(id);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user