Files
recipe-app/flutter/lib/features/meal_plan/data/meal_plan_providers.dart
T

39 lines
1.5 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/api/api_providers.dart';
import '../../../core/api/guarded_api_call.dart';
import '../../auth/data/auth_providers.dart';
import '../domain/meal_plan_dashboard.dart';
import '../domain/meal_plan_week.dart';
import 'meal_plan_repository.dart';
final mealPlanRepositoryProvider = Provider<MealPlanRepository>((ref) {
return MealPlanRepository(ref.watch(apiClientProvider));
});
final mealPlanWeekOffsetProvider = StateProvider<int>((ref) => 0);
final mealPlanWeekProvider = Provider<MealPlanWeek>((ref) {
final offset = ref.watch(mealPlanWeekOffsetProvider);
return MealPlanWeek.fromOffset(offset);
});
final mealPlanDashboardProvider = FutureProvider<MealPlanDashboard>((ref) async {
final week = ref.watch(mealPlanWeekProvider);
final token = await ref.watch(authStateProvider.future);
return guardedApiCall(ref, () async {
final repository = ref.read(mealPlanRepositoryProvider);
// Start all three requests in parallel.
final entriesFuture = repository.fetchEntries(week.fromIso, week.toIso, token: token);
final shoppingFuture = repository.fetchShoppingList(week.fromIso, week.toIso, token: token);
final compareFuture = repository.fetchInventoryCompare(week.fromIso, week.toIso, token: token);
return MealPlanDashboard(
entries: await entriesFuture,
shoppingItems: await shoppingFuture,
inventoryCompareItems: await compareFuture,
);
});
});