Refactor code structure for improved readability and maintainability
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -11,7 +11,15 @@ final mealPlanRepositoryProvider = Provider<MealPlanRepository>((ref) {
|
||||
return MealPlanRepository(ref.watch(apiClientProvider));
|
||||
});
|
||||
|
||||
final mealPlanWeekOffsetProvider = StateProvider<int>((ref) => 0);
|
||||
class _IntNotifier extends Notifier<int> {
|
||||
_IntNotifier(this._initial);
|
||||
final int _initial;
|
||||
@override
|
||||
int build() => _initial;
|
||||
}
|
||||
|
||||
final mealPlanWeekOffsetProvider =
|
||||
NotifierProvider<_IntNotifier, int>(() => _IntNotifier(0));
|
||||
|
||||
final mealPlanWeekProvider = Provider<MealPlanWeek>((ref) {
|
||||
final offset = ref.watch(mealPlanWeekOffsetProvider);
|
||||
|
||||
@@ -12,14 +12,14 @@ class MealPlanRepository {
|
||||
|
||||
Future<List<MealPlanEntry>> fetchEntries(String from, String to, {String? token}) async {
|
||||
try {
|
||||
final data = await _api.getJson(MealPlanApiPaths.listByRange(from, to), token: token);
|
||||
final dynamic data = await _api.getJson(MealPlanApiPaths.listByRange(from, to), token: token);
|
||||
if (data is! List) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown,
|
||||
message: 'Ogiltigt svar från servern.',
|
||||
);
|
||||
}
|
||||
return data
|
||||
return (data as List)
|
||||
.map((item) => MealPlanEntry.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on ApiException {
|
||||
@@ -34,14 +34,14 @@ class MealPlanRepository {
|
||||
|
||||
Future<List<ShoppingItem>> fetchShoppingList(String from, String to, {String? token}) async {
|
||||
try {
|
||||
final data = await _api.getJson(MealPlanApiPaths.shoppingList(from, to), token: token);
|
||||
final dynamic data = await _api.getJson(MealPlanApiPaths.shoppingList(from, to), token: token);
|
||||
if (data is! List) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown,
|
||||
message: 'Ogiltigt svar från servern.',
|
||||
);
|
||||
}
|
||||
return data
|
||||
return (data as List)
|
||||
.map((item) => ShoppingItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on ApiException {
|
||||
@@ -56,14 +56,14 @@ class MealPlanRepository {
|
||||
|
||||
Future<List<InventoryCompareItem>> fetchInventoryCompare(String from, String to, {String? token}) async {
|
||||
try {
|
||||
final data = await _api.getJson(MealPlanApiPaths.inventoryCompare(from, to), token: token);
|
||||
final dynamic data = await _api.getJson(MealPlanApiPaths.inventoryCompare(from, to), token: token);
|
||||
if (data is! List) {
|
||||
throw const ApiException(
|
||||
type: ApiErrorType.unknown,
|
||||
message: 'Ogiltigt svar från servern.',
|
||||
);
|
||||
}
|
||||
return data
|
||||
return (data as List)
|
||||
.map((item) => InventoryCompareItem.fromJson(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
} on ApiException {
|
||||
|
||||
@@ -87,8 +87,8 @@ class _MealPlanScreenState extends ConsumerState<MealPlanScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
final recipes = recipesAsync.valueOrNull ?? const <Recipe>[];
|
||||
final dashboard = dashboardAsync.valueOrNull ??
|
||||
final recipes = recipesAsync.maybeWhen(data: (d) => d, orElse: () => null) ?? const <Recipe>[];
|
||||
final dashboard = dashboardAsync.maybeWhen(data: (d) => d, orElse: () => null) ??
|
||||
const MealPlanDashboard(
|
||||
entries: [],
|
||||
shoppingItems: [],
|
||||
|
||||
Reference in New Issue
Block a user