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{
|
static Future<PizzaRecipe> fromYaml(String yamlString) async{
|
||||||
final yaml = loadYaml(yamlString);
|
return fromParsedYaml(loadYaml(yamlString) as YamlMap);
|
||||||
final YamlMap recipe = yaml["recipe"] as YamlMap;
|
}
|
||||||
|
|
||||||
|
static Future<PizzaRecipe> fromParsedYaml(YamlMap recipe) async {
|
||||||
final String name = recipe["name"] as String;
|
final String name = recipe["name"] as String;
|
||||||
final String? image = recipe.containsKey("image") ? recipe["image"] as String : null;
|
final String? image = recipe.containsKey("image") ? recipe["image"] as String : null;
|
||||||
final String description = recipe["description"] as String;
|
final String description = recipe["description"] as String;
|
||||||
|
|
|
@ -125,7 +125,15 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
||||||
return await singleRecipe(yamlBody);
|
return await singleRecipe(yamlBody);
|
||||||
}
|
}
|
||||||
if (yamlBody.startsWith("recipes:")){
|
if (yamlBody.startsWith("recipes:")){
|
||||||
|
try {
|
||||||
return await recipeDir(yamlBody);
|
return await recipeDir(yamlBody);
|
||||||
|
} catch (exception){
|
||||||
|
return const <Widget>[
|
||||||
|
Text(
|
||||||
|
"Failed to load",
|
||||||
|
)
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (exception) {
|
} catch (exception) {
|
||||||
print(exception);
|
print(exception);
|
||||||
|
@ -139,10 +147,34 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
||||||
final widgets = <Widget>[];
|
final widgets = <Widget>[];
|
||||||
for (final item in urls) {
|
for (final item in urls) {
|
||||||
try {
|
try {
|
||||||
final name = item["name"] as String;
|
final itemMap = item as YamlMap;
|
||||||
final url = item["url"] as String;
|
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(
|
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: () {
|
onTap: () {
|
||||||
Navigator.pushNamed(context, AddRecipeURLPage.route, arguments: url);
|
Navigator.pushNamed(context, AddRecipeURLPage.route, arguments: url);
|
||||||
},
|
},
|
||||||
|
@ -162,15 +194,19 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
)
|
|
||||||
);
|
);
|
||||||
widgets.add(const Divider());
|
|
||||||
} catch (exception){
|
|
||||||
print(exception);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
Future<Widget> buildRecipeWidget(YamlMap itemMap) async {
|
||||||
return widgets;
|
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 {
|
Future<List<Widget>> singleRecipe(String yamlBody) async {
|
||||||
|
@ -179,6 +215,15 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
|
||||||
InkWell(
|
InkWell(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
showDialog(context: context, builder: (BuildContext context) {
|
showDialog(context: context, builder: (BuildContext context) {
|
||||||
|
return buildRecipeDialog(pizzaRecipe);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
child: PizzaRecipeWidget(pizzaRecipe),
|
||||||
|
)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertDialog buildRecipeDialog(PizzaRecipe pizzaRecipe){
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: Text(pizzaRecipe.name),
|
title: Text(pizzaRecipe.name),
|
||||||
content: const Text("What do you want to do?"),
|
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 {
|
Future<void> addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async {
|
||||||
|
|
Loading…
Add table
Reference in a new issue