finished parsing of a yaml recipe into objects

This commit is contained in:
broodjeaap89 2021-07-10 17:39:40 +02:00
parent 44b32a6416
commit 3eb2c0ff93
4 changed files with 52 additions and 4 deletions

View file

@ -2,6 +2,8 @@ import 'package:flutter/services.dart' show rootBundle;
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredients.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/Ingredients.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
class PizzaRecipe { class PizzaRecipe {
@ -9,10 +11,12 @@ class PizzaRecipe {
final String description; final String description;
final Ingredients ingredients; final Ingredients ingredients;
PizzaRecipe(this.name, this.description, this.ingredients); final List<RecipeStep> recipeSteps;
PizzaRecipe(this.name, this.description, this.ingredients, this.recipeSteps);
static Future<PizzaRecipe> fromYaml() async{ static Future<PizzaRecipe> fromYaml() async{
String yamlString = await loadAsset("assets/recipes/neapolitan.yaml"); String yamlString = await loadAsset("assets/recipes/neapolitan_cold.yaml");
var yaml = loadYaml(yamlString); var yaml = loadYaml(yamlString);
var recipe = yaml["recipe"]; var recipe = yaml["recipe"];
@ -28,12 +32,38 @@ class PizzaRecipe {
value: (ingredient) => Ingredient(ingredient["name"], ingredient["unit"], ingredient["value"]) value: (ingredient) => Ingredient(ingredient["name"], ingredient["unit"], ingredient["value"])
); );
print(newIngredients); YamlList steps = recipe["steps"];
var newRecipeSteps = List.generate(steps.length, (i) {
YamlMap step = steps[i];
String stepName = step["name"];
String stepDescription = step["description"];
YamlMap waitMap = step.containsKey("wait") ? step["wait"] : YamlList();
String waitUnit = waitMap["unit"];
int waitMin = waitMap["min"];
int waitMax = waitMap["max"];
print(step);
YamlList subSteps = step.containsKey("substeps") ? step["substeps"] : YamlList();
var newSubSteps = List.generate(subSteps.length, (j) {
var subStep = subSteps[j];
return RecipeSubStep(subStep["name"], subStep["description"]);
});
return RecipeStep(
stepName,
stepDescription,
waitUnit,
waitMin,
waitMax,
newSubSteps
);
});
return PizzaRecipe( return PizzaRecipe(
name, name,
description, description,
Ingredients(newIngredients, ingredientMethod) Ingredients(newIngredients, ingredientMethod),
newRecipeSteps
); );
} }
} }

View file

@ -0,0 +1,12 @@
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
class RecipeStep {
final String name;
final String waitUnit;
final int waitMin;
final int waitMax;
final String description;
final List<RecipeSubStep> subSteps;
RecipeStep(this.name, this.description, this.waitUnit, this.waitMin, this.waitMax, this.subSteps);
}

View file

@ -0,0 +1,6 @@
class RecipeSubStep {
final String name;
final String description;
RecipeSubStep(this.name, this.description);
}