59 lines
1.8 KiB
Dart
59 lines
1.8 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));
|
|
});
|
|
|
|
class _IntNotifier extends Notifier<int> {
|
|
_IntNotifier(this._initial);
|
|
final int _initial;
|
|
@override
|
|
int build() => _initial;
|
|
|
|
void increment() {
|
|
state = state + 1;
|
|
}
|
|
|
|
void decrement() {
|
|
state = state - 1;
|
|
}
|
|
|
|
void reset() {
|
|
state = 0;
|
|
}
|
|
}
|
|
|
|
final mealPlanWeekOffsetProvider =
|
|
NotifierProvider<_IntNotifier, int>(() => _IntNotifier(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,
|
|
);
|
|
});
|
|
}); |