Advertisement
harmonyV

Untitled

Jan 23rd, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 4.90 KB | None | 0 0
  1. class Recipe {
  2.   Id id;
  3.   int? supabaseId;
  4.  
  5.   String title;
  6.   String description;
  7.   int servings;
  8.   List<Ingredient>? ingredients;
  9.   List<RecipeComponent>? components;
  10.   List<Instruction> instructions;
  11.   List<Nutrition> nutritionBreakdown;
  12.   int totalTime;
  13.   int calories;
  14.   String? chefsTip;
  15.   final bool? isPaid;
  16.  
  17.   final DietaryRestriction dietaryRestriction;
  18.   final MealType mealType;
  19.   final Weekday? weekday;
  20.  
  21.   Recipe({
  22.     required this.title,
  23.     required this.description,
  24.     this.ingredients,
  25.     required this.instructions,
  26.     required this.nutritionBreakdown,
  27.     required this.totalTime,
  28.     required this.calories,
  29.     required this.dietaryRestriction,
  30.     required this.mealType,
  31.     bool? newIsPaid,
  32.     int? newServings,
  33.     this.weekday,
  34.     this.chefsTip,
  35.     this.id = Isar.autoIncrement,
  36.     this.supabaseId,
  37.     this.components,
  38.   })  : isPaid = newIsPaid ?? true,
  39.         servings = newServings ?? 1;
  40.  
  41.   factory Recipe.fromJson(Map<String, dynamic> json) {
  42.     try {
  43.       return Recipe(
  44.         supabaseId: json["id"] as int,
  45.         title: json['title'] as String,
  46.         description: json['description'] as String,
  47.         totalTime: json['total_time'] as int,
  48.         calories: json['calories'] as int,
  49.         newIsPaid: json['is_paid'] as bool?, // Defaults to true in constructor
  50.         newServings: json['servings'] as int?,
  51.         dietaryRestriction:
  52.             DietaryRestriction.fromJson(json['dietary_restriction'] as String),
  53.         mealType: MealType.fromJson(json['meal_type'] as String),
  54.         weekday: json['weekday'] != null
  55.             ? Weekday.fromJson(json['weekday'] as String)
  56.             : null,
  57.         ingredients: (json['ingredients'] as List?)
  58.             ?.map((item) => Ingredient.fromJson(item as Map<String, dynamic>))
  59.             .toList(),
  60.         components: (json['components'] as List?)
  61.             ?.map((item) =>
  62.                 RecipeComponent.fromJson(item as Map<String, dynamic>))
  63.             .toList(),
  64.         instructions: (json['instructions'] as List? ?? [])
  65.             .map((item) => Instruction.fromJson(item as Map<String, dynamic>))
  66.             .toList(),
  67.         nutritionBreakdown: (json['nutrition_breakdown'] as List? ?? [])
  68.             .map((item) => Nutrition.fromJson(item as Map<String, dynamic>))
  69.             .toList(),
  70.         chefsTip: json['chefs_tip'] as String?,
  71.       );
  72.     } catch (e, stackTrace) {
  73.       log('Error parsing Recipe JSON: $e\n$stackTrace');
  74.       throw FormatException('Invalid Recipe JSON structure: $e');
  75.     }
  76.   }
  77.  
  78.   int get totalIngredientCount {
  79.     final totalIngredients = ingredients?.length ?? 0;
  80.     final totalComponentsIngredients = components?.fold(
  81.             0, (sum, component) => sum + component.ingredients.length) ??
  82.         0;
  83.  
  84.     return totalIngredients + totalComponentsIngredients;
  85.   }
  86.  
  87.   bool isValid() {
  88.     return (ingredients?.isNotEmpty == true ||
  89.             components?.isNotEmpty == true) &&
  90.         instructions.isNotEmpty &&
  91.         nutritionBreakdown.isNotEmpty;
  92.   }
  93.  
  94.   @override
  95.   String toString() {
  96.     return 'Recipe(id: $id, title: $title, components: ${components?.length ?? 0} groups, ingredients: ${ingredients?.length ?? 0} items)';
  97.   }
  98. }
  99.  
  100. class Ingredient {
  101.   String name;
  102.   String? metricAmount;
  103.   String? empiricAmount;
  104.  
  105.   Ingredient({
  106.     this.name = "NaaN",
  107.     this.metricAmount,
  108.     this.empiricAmount,
  109.   });
  110.  
  111.   factory Ingredient.fromJson(Map<String, dynamic> json) {
  112.     return Ingredient(
  113.       name: json['name'] as String? ?? "NaaN",
  114.       metricAmount: json['metricAmount'] as String?,
  115.       empiricAmount: json['empiricAmount'] as String?,
  116.     );
  117.   }
  118. }
  119.  
  120. class Instruction {
  121.   String title;
  122.   String body;
  123.  
  124.   Instruction({
  125.     this.title = "NaaN",
  126.     this.body = "NaaN",
  127.   });
  128.  
  129.   factory Instruction.fromJson(Map<String, dynamic> json) {
  130.     return Instruction(
  131.       title: json['title'] as String,
  132.       body: json['body'] as String,
  133.     );
  134.   }
  135. }
  136.  
  137. class Nutrition {
  138.   String name;
  139.   String value;
  140.  
  141.   Nutrition({
  142.     this.name = "NaaN",
  143.     this.value = "NaaN",
  144.   });
  145.  
  146.   factory Nutrition.fromJson(Map<String, dynamic> json) {
  147.     return Nutrition(
  148.       name: json['name'] as String,
  149.       value: json['value'] as String,
  150.     );
  151.   }
  152. }
  153.  
  154. class RecipeComponent {
  155.   String title;
  156.   List<Ingredient> ingredients;
  157.   String? description;
  158.  
  159.   RecipeComponent({
  160.     String? newtitle,
  161.     List<Ingredient>? newingredients,
  162.     this.description,
  163.   })  : title = newtitle ?? "NaaN",
  164.         ingredients = newingredients ?? [];
  165.  
  166.   factory RecipeComponent.fromJson(Map<String, dynamic> json) {
  167.     return RecipeComponent(
  168.       newtitle: json['title'] as String,
  169.       newingredients: (json['ingredients'] as List? ?? [])
  170.           .map((item) => Ingredient.fromJson(item as Map<String, dynamic>))
  171.           .toList(),
  172.       description: json['description'] as String?,
  173.     );
  174.   }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement