diff --git a/lib/entities/PizzaRecipe/pizza_recipe.dart b/lib/entities/PizzaRecipe/pizza_recipe.dart index b425370..1392c89 100644 --- a/lib/entities/PizzaRecipe/pizza_recipe.dart +++ b/lib/entities/PizzaRecipe/pizza_recipe.dart @@ -90,9 +90,10 @@ class PizzaRecipe extends HiveObject { } static Future fromYaml(String yamlString) async{ - final yaml = loadYaml(yamlString); - final YamlMap recipe = yaml["recipe"] as YamlMap; - + return fromParsedYaml(loadYaml(yamlString) as YamlMap); + } + + static Future fromParsedYaml(YamlMap recipe) async { final String name = recipe["name"] as String; final String? image = recipe.containsKey("image") ? recipe["image"] as String : null; final String description = recipe["description"] as String; @@ -101,10 +102,10 @@ class PizzaRecipe extends HiveObject { final List newIngredients = ingredients.map( (ingredient) => Ingredient( - ingredient["name"] as String, - ingredient["unit"] as String, - ingredient["value"] as double - )).toList(); + ingredient["name"] as String, + ingredient["unit"] as String, + ingredient["value"] as double + )).toList(); final YamlList steps = recipe["steps"] as YamlList; final newRecipeSteps = List.generate(steps.length, (i) { @@ -130,27 +131,27 @@ class PizzaRecipe extends HiveObject { final newSubSteps = List.generate(subSteps.length, (j) { final subStep = subSteps[j]; return RecipeSubStep( - subStep["name"] as String, + subStep["name"] as String, subStep["description"] as String ); }); return RecipeStep( - stepName, - stepDescription, - waitDescription, - waitUnit, - waitMin, - waitMax, - newSubSteps + stepName, + stepDescription, + waitDescription, + waitUnit, + waitMin, + waitMax, + newSubSteps ); }); return PizzaRecipe( - name, - description, - newIngredients, - newRecipeSteps, - image: image + name, + description, + newIngredients, + newRecipeSteps, + image: image ); } diff --git a/lib/pages/add_recipe_url.dart b/lib/pages/add_recipe_url.dart index a3f7422..278c033 100644 --- a/lib/pages/add_recipe_url.dart +++ b/lib/pages/add_recipe_url.dart @@ -125,7 +125,15 @@ class AddRecipeURLPageState extends State { return await singleRecipe(yamlBody); } if (yamlBody.startsWith("recipes:")){ - return await recipeDir(yamlBody); + try { + return await recipeDir(yamlBody); + } catch (exception){ + return const [ + Text( + "Failed to load", + ) + ]; + } } } catch (exception) { print(exception); @@ -139,66 +147,75 @@ class AddRecipeURLPageState extends State { final widgets = []; for (final item in urls) { try { - final name = item["name"] as String; - final url = item["url"] as String; - widgets.add( - InkWell( - onTap: () { - Navigator.pushNamed(context, AddRecipeURLPage.route, arguments: url); - }, - child: Container( - height: 70, - width: double.infinity, - color: Colors.blue, - child: Column( - children: [ - Center( - child: Text(name, style: const TextStyle(color: Colors.white)), - ), - const Divider(), - Center( - child: Text(url, style: const TextStyle(color: Colors.white)), - ), - ], - ) - ), - ) - ); + final itemMap = item as YamlMap; + if (itemMap.containsKey("url")){ // Dir item + widgets.add(buildDirWidget(itemMap)); + } else { // recipe item + widgets.add(await buildRecipeWidget(itemMap)); + } widgets.add(const Divider()); } catch (exception){ - print(exception); + widgets.add( + Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "Failed to load", + style: Theme.of(context).textTheme.subtitle2 + ) + ], + ) + ); } - } return widgets; } + Widget buildDirWidget(YamlMap itemMap){ + final name = itemMap["name"] as String; + final url = itemMap["url"] as String; + return InkWell( + onTap: () { + Navigator.pushNamed(context, AddRecipeURLPage.route, arguments: url); + }, + child: Container( + height: 70, + width: double.infinity, + color: Colors.blue, + child: Column( + children: [ + Center( + child: Text(name, style: const TextStyle(color: Colors.white)), + ), + const Divider(), + Center( + child: Text(url, style: const TextStyle(color: Colors.white)), + ), + ], + ) + ), + ); + } + + Future buildRecipeWidget(YamlMap itemMap) async { + final pizzaRecipe = await PizzaRecipe.fromParsedYaml(itemMap); + return InkWell( + onTap: () { + showDialog(context: context, builder: (BuildContext context) { + return buildRecipeDialog(pizzaRecipe); + }); + }, + child: PizzaRecipeWidget(pizzaRecipe), + ); + } + Future> singleRecipe(String yamlBody) async { final pizzaRecipe = await PizzaRecipe.fromYaml(yamlBody); return [ InkWell( onTap: () { showDialog(context: context, builder: (BuildContext context) { - return AlertDialog( - title: Text(pizzaRecipe.name), - content: const Text("What do you want to do?"), - actions: [ - TextButton( - onPressed: () { - Navigator.pop(context); - Navigator.pushNamed(context, RecipePage.route, arguments: pizzaRecipe); - }, - child: const Text("View"), - ), - TextButton( - onPressed: () { - Navigator.pop(context); - addPizzaRecipeToBox(pizzaRecipe); - }, - child: const Text("Add"), - ), - ] - ); + return buildRecipeDialog(pizzaRecipe); }); }, child: PizzaRecipeWidget(pizzaRecipe), @@ -206,6 +223,29 @@ class AddRecipeURLPageState extends State { ]; } + AlertDialog buildRecipeDialog(PizzaRecipe pizzaRecipe){ + return AlertDialog( + title: Text(pizzaRecipe.name), + content: const Text("What do you want to do?"), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + Navigator.pushNamed(context, RecipePage.route, arguments: pizzaRecipe); + }, + child: const Text("View"), + ), + TextButton( + onPressed: () { + Navigator.pop(context); + addPizzaRecipeToBox(pizzaRecipe); + }, + child: const Text("Add"), + ), + ] + ); + } + Future addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async { final pizzaRecipeBox = Hive.box(PizzaRecipe.hiveName); if (pizzaRecipeBox.containsKey(pizzaRecipe.key)) {