feat: add meal planning feature with API integration

- Introduced MealPlanApiPaths for handling meal plan related API endpoints.
- Added MealPlanScreen for displaying and managing meal plans.
- Implemented MealPlanRepository for fetching and updating meal plan data.
- Created data models: MealPlanEntry, MealPlanRecipe, InventoryCompareItem, ShoppingItem, and MealPlanDashboard.
- Integrated meal plan functionality into the app router and UI.
- Updated localization files for meal plan related strings in English and Swedish.
- Added state management for meal plan using Riverpod.
This commit is contained in:
Nils-Johan Gynther
2026-04-22 19:51:33 +02:00
parent b8627d0b7f
commit e495a4b38e
14 changed files with 1098 additions and 0 deletions
@@ -0,0 +1,42 @@
class InventoryCompareItem {
final int productId;
final String name;
final double requiredQuantity;
final String unit;
final double availableQuantity;
final double missingQuantity;
final String status;
const InventoryCompareItem({
required this.productId,
required this.name,
required this.requiredQuantity,
required this.unit,
required this.availableQuantity,
required this.missingQuantity,
required this.status,
});
factory InventoryCompareItem.fromJson(Map<String, dynamic> json) {
final rawId = json['productId'];
final rawRequired = json['required'];
final rawAvailable = json['available'];
final rawMissing = json['missing'];
return InventoryCompareItem(
productId: rawId is num ? rawId.toInt() : int.parse(rawId.toString()),
name: json['name'].toString(),
requiredQuantity: rawRequired is num
? rawRequired.toDouble()
: double.parse(rawRequired.toString()),
unit: json['unit'].toString(),
availableQuantity: rawAvailable is num
? rawAvailable.toDouble()
: double.parse(rawAvailable.toString()),
missingQuantity: rawMissing is num
? rawMissing.toDouble()
: double.parse(rawMissing.toString()),
status: json['status'].toString(),
);
}
}
@@ -0,0 +1,26 @@
import 'inventory_compare_item.dart';
import 'meal_plan_entry.dart';
import 'shopping_item.dart';
class MealPlanDashboard {
final List<MealPlanEntry> entries;
final List<ShoppingItem> shoppingItems;
final List<InventoryCompareItem> inventoryCompareItems;
const MealPlanDashboard({
required this.entries,
required this.shoppingItems,
required this.inventoryCompareItems,
});
MealPlanEntry? entryForDate(DateTime date) {
final normalized = DateTime(date.year, date.month, date.day);
for (final entry in entries) {
final entryDate = DateTime(entry.date.year, entry.date.month, entry.date.day);
if (entryDate == normalized) {
return entry;
}
}
return null;
}
}
@@ -0,0 +1,31 @@
import 'meal_plan_recipe.dart';
class MealPlanEntry {
final int id;
final DateTime date;
final int? servings;
final MealPlanRecipe recipe;
const MealPlanEntry({
required this.id,
required this.date,
required this.recipe,
this.servings,
});
factory MealPlanEntry.fromJson(Map<String, dynamic> json) {
final rawId = json['id'];
final rawServings = json['servings'];
return MealPlanEntry(
id: rawId is num ? rawId.toInt() : int.parse(rawId.toString()),
date: DateTime.parse(json['date'].toString()),
servings: rawServings == null
? null
: rawServings is num
? rawServings.toInt()
: int.tryParse(rawServings.toString()),
recipe: MealPlanRecipe.fromJson(json['recipe'] as Map<String, dynamic>),
);
}
}
@@ -0,0 +1,31 @@
class MealPlanRecipe {
final int id;
final String title;
final String? imageUrl;
final int? servings;
const MealPlanRecipe({
required this.id,
required this.title,
this.imageUrl,
this.servings,
});
factory MealPlanRecipe.fromJson(Map<String, dynamic> json) {
final rawId = json['id'];
final rawTitle = json['title'] ?? json['name'];
final rawServings = json['servings'];
final rawImageUrl = json['imageUrl']?.toString().trim();
return MealPlanRecipe(
id: rawId is num ? rawId.toInt() : int.parse(rawId.toString()),
title: (rawTitle ?? '').toString(),
imageUrl: rawImageUrl == null || rawImageUrl.isEmpty ? null : rawImageUrl,
servings: rawServings == null
? null
: rawServings is num
? rawServings.toInt()
: int.tryParse(rawServings.toString()),
);
}
}
@@ -0,0 +1,34 @@
import 'package:intl/intl.dart';
class MealPlanWeek {
final DateTime start;
final List<DateTime> days;
const MealPlanWeek({
required this.start,
required this.days,
});
factory MealPlanWeek.fromOffset(int offset) {
final now = DateTime.now();
final today = DateTime(now.year, now.month, now.day);
final monday = today.subtract(Duration(days: today.weekday - 1)).add(
Duration(days: offset * 7),
);
return MealPlanWeek(
start: monday,
days: List.generate(
7,
(index) => DateTime(monday.year, monday.month, monday.day + index),
),
);
}
DateTime get end => days.last;
String get fromIso => isoDate(start);
String get toIso => isoDate(end);
String isoDate(DateTime date) => DateFormat('yyyy-MM-dd').format(date);
}
@@ -0,0 +1,27 @@
class ShoppingItem {
final int productId;
final String name;
final double quantity;
final String unit;
const ShoppingItem({
required this.productId,
required this.name,
required this.quantity,
required this.unit,
});
factory ShoppingItem.fromJson(Map<String, dynamic> json) {
final rawId = json['productId'];
final rawQuantity = json['quantity'];
return ShoppingItem(
productId: rawId is num ? rawId.toInt() : int.parse(rawId.toString()),
name: json['name'].toString(),
quantity: rawQuantity is num
? rawQuantity.toDouble()
: double.parse(rawQuantity.toString()),
unit: json['unit'].toString(),
);
}
}