refactored to allow for multiple recipes in a single yaml, mixed with urls to other recipes
This commit is contained in:
parent
6c4b0f4e05
commit
a0c0e02035
2 changed files with 109 additions and 68 deletions
|
@ -90,9 +90,10 @@ class PizzaRecipe extends HiveObject {
|
|||
}
|
||||
|
||||
static Future<PizzaRecipe> fromYaml(String yamlString) async{
|
||||
final yaml = loadYaml(yamlString);
|
||||
final YamlMap recipe = yaml["recipe"] as YamlMap;
|
||||
return fromParsedYaml(loadYaml(yamlString) as YamlMap);
|
||||
}
|
||||
|
||||
static Future<PizzaRecipe> 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;
|
||||
|
|
|
@ -125,7 +125,15 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
|||
return await singleRecipe(yamlBody);
|
||||
}
|
||||
if (yamlBody.startsWith("recipes:")){
|
||||
try {
|
||||
return await recipeDir(yamlBody);
|
||||
} catch (exception){
|
||||
return const <Widget>[
|
||||
Text(
|
||||
"Failed to load",
|
||||
)
|
||||
];
|
||||
}
|
||||
}
|
||||
} catch (exception) {
|
||||
print(exception);
|
||||
|
@ -139,10 +147,34 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
|||
final widgets = <Widget>[];
|
||||
for (final item in urls) {
|
||||
try {
|
||||
final name = item["name"] as String;
|
||||
final url = item["url"] as String;
|
||||
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){
|
||||
widgets.add(
|
||||
InkWell(
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
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);
|
||||
},
|
||||
|
@ -162,15 +194,19 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
|||
],
|
||||
)
|
||||
),
|
||||
)
|
||||
);
|
||||
widgets.add(const Divider());
|
||||
} catch (exception){
|
||||
print(exception);
|
||||
}
|
||||
|
||||
}
|
||||
return widgets;
|
||||
Future<Widget> 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<List<Widget>> singleRecipe(String yamlBody) async {
|
||||
|
@ -179,6 +215,15 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
|||
InkWell(
|
||||
onTap: () {
|
||||
showDialog(context: context, builder: (BuildContext context) {
|
||||
return buildRecipeDialog(pizzaRecipe);
|
||||
});
|
||||
},
|
||||
child: PizzaRecipeWidget(pizzaRecipe),
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
AlertDialog buildRecipeDialog(PizzaRecipe pizzaRecipe){
|
||||
return AlertDialog(
|
||||
title: Text(pizzaRecipe.name),
|
||||
content: const Text("What do you want to do?"),
|
||||
|
@ -199,11 +244,6 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
|||
),
|
||||
]
|
||||
);
|
||||
});
|
||||
},
|
||||
child: PizzaRecipeWidget(pizzaRecipe),
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
Future<void> addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async {
|
||||
|
|
Loading…
Add table
Reference in a new issue