feat: add recipe creation, editing, and detail screens; enhance recipe model with instructions and ingredients
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
class IngredientSuggestion {
|
||||
final int productId;
|
||||
final String productName;
|
||||
final double score;
|
||||
|
||||
const IngredientSuggestion({
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.score,
|
||||
});
|
||||
|
||||
factory IngredientSuggestion.fromJson(Map<String, dynamic> json) =>
|
||||
IngredientSuggestion(
|
||||
productId: (json['productId'] as num).toInt(),
|
||||
productName: json['productName'] as String? ?? '',
|
||||
score: (json['score'] as num).toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
class ParsedIngredient {
|
||||
final String rawName;
|
||||
final double quantity;
|
||||
final String unit;
|
||||
final String? note;
|
||||
final List<IngredientSuggestion> suggestions;
|
||||
|
||||
const ParsedIngredient({
|
||||
required this.rawName,
|
||||
required this.quantity,
|
||||
required this.unit,
|
||||
this.note,
|
||||
required this.suggestions,
|
||||
});
|
||||
|
||||
factory ParsedIngredient.fromJson(Map<String, dynamic> json) {
|
||||
final rawSuggestions = json['suggestions'] as List<dynamic>? ?? [];
|
||||
return ParsedIngredient(
|
||||
rawName: json['rawName'] as String? ?? '',
|
||||
quantity: (json['quantity'] as num? ?? 0).toDouble(),
|
||||
unit: json['unit'] as String? ?? '',
|
||||
note: json['note'] as String?,
|
||||
suggestions: rawSuggestions
|
||||
.map((s) => IngredientSuggestion.fromJson(s as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParsedRecipe {
|
||||
final String name;
|
||||
final String? description;
|
||||
final String? instructions;
|
||||
final List<ParsedIngredient> ingredients;
|
||||
|
||||
const ParsedRecipe({
|
||||
required this.name,
|
||||
this.description,
|
||||
this.instructions,
|
||||
required this.ingredients,
|
||||
});
|
||||
|
||||
factory ParsedRecipe.fromJson(Map<String, dynamic> json) {
|
||||
final rawIngredients = json['ingredients'] as List<dynamic>? ?? [];
|
||||
return ParsedRecipe(
|
||||
name: json['name'] as String? ?? '',
|
||||
description: json['description'] as String?,
|
||||
instructions: json['instructions'] as String?,
|
||||
ingredients: rawIngredients
|
||||
.map((i) => ParsedIngredient.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
import 'recipe_ingredient.dart';
|
||||
|
||||
class Recipe {
|
||||
final int id;
|
||||
final String title;
|
||||
final String? description;
|
||||
final String? imageUrl;
|
||||
final int? servings;
|
||||
final String? instructions;
|
||||
final List<RecipeIngredient> ingredients;
|
||||
|
||||
const Recipe({
|
||||
required this.id,
|
||||
@@ -11,6 +15,8 @@ class Recipe {
|
||||
this.description,
|
||||
this.imageUrl,
|
||||
this.servings,
|
||||
this.instructions,
|
||||
this.ingredients = const [],
|
||||
});
|
||||
|
||||
factory Recipe.fromJson(Map<String, dynamic> json) {
|
||||
@@ -19,6 +25,7 @@ class Recipe {
|
||||
final dynamic rawDescription = json['description'];
|
||||
final dynamic rawImageUrl = json['imageUrl'];
|
||||
final dynamic rawServings = json['servings'];
|
||||
final rawIngredients = json['ingredients'] as List<dynamic>? ?? [];
|
||||
|
||||
return Recipe(
|
||||
id: rawId is num ? rawId.toInt() : int.parse(rawId.toString()),
|
||||
@@ -30,6 +37,10 @@ class Recipe {
|
||||
: (rawServings is num
|
||||
? rawServings.toInt()
|
||||
: int.tryParse(rawServings.toString())),
|
||||
instructions: json['instructions'] as String?,
|
||||
ingredients: rawIngredients
|
||||
.map((i) => RecipeIngredient.fromJson(i as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
class RecipeIngredient {
|
||||
final int id;
|
||||
final int productId;
|
||||
final String productName;
|
||||
final double quantity;
|
||||
final String unit;
|
||||
final String? note;
|
||||
|
||||
const RecipeIngredient({
|
||||
required this.id,
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
required this.quantity,
|
||||
required this.unit,
|
||||
this.note,
|
||||
});
|
||||
|
||||
factory RecipeIngredient.fromJson(Map<String, dynamic> json) {
|
||||
final product = json['product'] as Map<String, dynamic>?;
|
||||
final rawQty = json['quantity'];
|
||||
return RecipeIngredient(
|
||||
id: (json['id'] as num).toInt(),
|
||||
productId: (json['productId'] as num).toInt(),
|
||||
productName: product?['name'] as String? ?? '',
|
||||
quantity: rawQty is num
|
||||
? rawQty.toDouble()
|
||||
: double.tryParse(rawQty?.toString() ?? '') ?? 0,
|
||||
unit: json['unit'] as String? ?? '',
|
||||
note: json['note'] as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user