Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Recipe {
- Id id;
- int? supabaseId;
- String title;
- String description;
- int servings;
- List<Ingredient>? ingredients;
- List<RecipeComponent>? components;
- List<Instruction> instructions;
- List<Nutrition> nutritionBreakdown;
- int totalTime;
- int calories;
- String? chefsTip;
- final bool? isPaid;
- final DietaryRestriction dietaryRestriction;
- final MealType mealType;
- final Weekday? weekday;
- Recipe({
- required this.title,
- required this.description,
- this.ingredients,
- required this.instructions,
- required this.nutritionBreakdown,
- required this.totalTime,
- required this.calories,
- required this.dietaryRestriction,
- required this.mealType,
- bool? newIsPaid,
- int? newServings,
- this.weekday,
- this.chefsTip,
- this.id = Isar.autoIncrement,
- this.supabaseId,
- this.components,
- }) : isPaid = newIsPaid ?? true,
- servings = newServings ?? 1;
- factory Recipe.fromJson(Map<String, dynamic> json) {
- try {
- return Recipe(
- supabaseId: json["id"] as int,
- title: json['title'] as String,
- description: json['description'] as String,
- totalTime: json['total_time'] as int,
- calories: json['calories'] as int,
- newIsPaid: json['is_paid'] as bool?, // Defaults to true in constructor
- newServings: json['servings'] as int?,
- dietaryRestriction:
- DietaryRestriction.fromJson(json['dietary_restriction'] as String),
- mealType: MealType.fromJson(json['meal_type'] as String),
- weekday: json['weekday'] != null
- ? Weekday.fromJson(json['weekday'] as String)
- : null,
- ingredients: (json['ingredients'] as List?)
- ?.map((item) => Ingredient.fromJson(item as Map<String, dynamic>))
- .toList(),
- components: (json['components'] as List?)
- ?.map((item) =>
- RecipeComponent.fromJson(item as Map<String, dynamic>))
- .toList(),
- instructions: (json['instructions'] as List? ?? [])
- .map((item) => Instruction.fromJson(item as Map<String, dynamic>))
- .toList(),
- nutritionBreakdown: (json['nutrition_breakdown'] as List? ?? [])
- .map((item) => Nutrition.fromJson(item as Map<String, dynamic>))
- .toList(),
- chefsTip: json['chefs_tip'] as String?,
- );
- } catch (e, stackTrace) {
- log('Error parsing Recipe JSON: $e\n$stackTrace');
- throw FormatException('Invalid Recipe JSON structure: $e');
- }
- }
- int get totalIngredientCount {
- final totalIngredients = ingredients?.length ?? 0;
- final totalComponentsIngredients = components?.fold(
- 0, (sum, component) => sum + component.ingredients.length) ??
- 0;
- return totalIngredients + totalComponentsIngredients;
- }
- bool isValid() {
- return (ingredients?.isNotEmpty == true ||
- components?.isNotEmpty == true) &&
- instructions.isNotEmpty &&
- nutritionBreakdown.isNotEmpty;
- }
- @override
- String toString() {
- return 'Recipe(id: $id, title: $title, components: ${components?.length ?? 0} groups, ingredients: ${ingredients?.length ?? 0} items)';
- }
- }
- class Ingredient {
- String name;
- String? metricAmount;
- String? empiricAmount;
- Ingredient({
- this.name = "NaaN",
- this.metricAmount,
- this.empiricAmount,
- });
- factory Ingredient.fromJson(Map<String, dynamic> json) {
- return Ingredient(
- name: json['name'] as String? ?? "NaaN",
- metricAmount: json['metricAmount'] as String?,
- empiricAmount: json['empiricAmount'] as String?,
- );
- }
- }
- class Instruction {
- String title;
- String body;
- Instruction({
- this.title = "NaaN",
- this.body = "NaaN",
- });
- factory Instruction.fromJson(Map<String, dynamic> json) {
- return Instruction(
- title: json['title'] as String,
- body: json['body'] as String,
- );
- }
- }
- class Nutrition {
- String name;
- String value;
- Nutrition({
- this.name = "NaaN",
- this.value = "NaaN",
- });
- factory Nutrition.fromJson(Map<String, dynamic> json) {
- return Nutrition(
- name: json['name'] as String,
- value: json['value'] as String,
- );
- }
- }
- class RecipeComponent {
- String title;
- List<Ingredient> ingredients;
- String? description;
- RecipeComponent({
- String? newtitle,
- List<Ingredient>? newingredients,
- this.description,
- }) : title = newtitle ?? "NaaN",
- ingredients = newingredients ?? [];
- factory RecipeComponent.fromJson(Map<String, dynamic> json) {
- return RecipeComponent(
- newtitle: json['title'] as String,
- newingredients: (json['ingredients'] as List? ?? [])
- .map((item) => Ingredient.fromJson(item as Map<String, dynamic>))
- .toList(),
- description: json['description'] as String?,
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement