From 3eb2c0ff93dac192baa9e16a24d296c7b338ca8a Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Sat, 10 Jul 2021 17:39:40 +0200 Subject: [PATCH] finished parsing of a yaml recipe into objects --- .../{neapolitan.yaml => neapolitan_cold.yaml} | 0 lib/entities/PizzaRecipe/PizzaRecipe.dart | 38 +++++++++++++++++-- lib/entities/PizzaRecipe/RecipeStep.dart | 12 ++++++ lib/entities/PizzaRecipe/RecipeSubStep.dart | 6 +++ 4 files changed, 52 insertions(+), 4 deletions(-) rename assets/recipes/{neapolitan.yaml => neapolitan_cold.yaml} (100%) create mode 100644 lib/entities/PizzaRecipe/RecipeStep.dart create mode 100644 lib/entities/PizzaRecipe/RecipeSubStep.dart diff --git a/assets/recipes/neapolitan.yaml b/assets/recipes/neapolitan_cold.yaml similarity index 100% rename from assets/recipes/neapolitan.yaml rename to assets/recipes/neapolitan_cold.yaml diff --git a/lib/entities/PizzaRecipe/PizzaRecipe.dart b/lib/entities/PizzaRecipe/PizzaRecipe.dart index 7636af9..0b5d651 100644 --- a/lib/entities/PizzaRecipe/PizzaRecipe.dart +++ b/lib/entities/PizzaRecipe/PizzaRecipe.dart @@ -2,6 +2,8 @@ import 'package:flutter/services.dart' show rootBundle; import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.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'; class PizzaRecipe { @@ -9,10 +11,12 @@ class PizzaRecipe { final String description; final Ingredients ingredients; - PizzaRecipe(this.name, this.description, this.ingredients); + final List recipeSteps; + + PizzaRecipe(this.name, this.description, this.ingredients, this.recipeSteps); static Future fromYaml() async{ - String yamlString = await loadAsset("assets/recipes/neapolitan.yaml"); + String yamlString = await loadAsset("assets/recipes/neapolitan_cold.yaml"); var yaml = loadYaml(yamlString); var recipe = yaml["recipe"]; @@ -28,12 +32,38 @@ class PizzaRecipe { 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( name, description, - Ingredients(newIngredients, ingredientMethod) + Ingredients(newIngredients, ingredientMethod), + newRecipeSteps ); } } diff --git a/lib/entities/PizzaRecipe/RecipeStep.dart b/lib/entities/PizzaRecipe/RecipeStep.dart new file mode 100644 index 0000000..218cd8c --- /dev/null +++ b/lib/entities/PizzaRecipe/RecipeStep.dart @@ -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 subSteps; + + RecipeStep(this.name, this.description, this.waitUnit, this.waitMin, this.waitMax, this.subSteps); +} \ No newline at end of file diff --git a/lib/entities/PizzaRecipe/RecipeSubStep.dart b/lib/entities/PizzaRecipe/RecipeSubStep.dart new file mode 100644 index 0000000..af3b2c1 --- /dev/null +++ b/lib/entities/PizzaRecipe/RecipeSubStep.dart @@ -0,0 +1,6 @@ +class RecipeSubStep { + final String name; + final String description; + + RecipeSubStep(this.name, this.description); +} \ No newline at end of file