pizza recipes are now loaded from the github repo on first app run, so always up to date
as a backup still have local recipes
This commit is contained in:
parent
42dcacee0b
commit
b6f58222f1
3 changed files with 38 additions and 3 deletions
|
@ -94,6 +94,9 @@ class PizzaRecipe extends HiveObject {
|
|||
}
|
||||
|
||||
static Future<PizzaRecipe> fromParsedYaml(YamlMap recipe) async {
|
||||
if (recipe.containsKey("recipe")){
|
||||
recipe = recipe["recipe"] as YamlMap;
|
||||
}
|
||||
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;
|
||||
|
|
|
@ -47,7 +47,10 @@ Future<void> main() async {
|
|||
final pizzaRecipesBox = await Hive.openBox<PizzaRecipe>(PizzaRecipe.hiveName);
|
||||
|
||||
if (pizzaRecipesBox.isEmpty){
|
||||
pizzaRecipesBox.addAll(await getRecipes());
|
||||
pizzaRecipesBox.addAll(await getRecipesFromGithub());
|
||||
}
|
||||
if (pizzaRecipesBox.isEmpty){
|
||||
pizzaRecipesBox.addAll(await getLocalRecipes());
|
||||
}
|
||||
|
||||
// notification init
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/pizza_recipe.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
Future<List<PizzaRecipe>> getRecipes() async {
|
||||
Future<List<PizzaRecipe>> getLocalRecipes() async {
|
||||
final manifestContent = await rootBundle.loadString('AssetManifest.json');
|
||||
final Map<String, dynamic> manifestMap = json.decode(manifestContent) as Map<String, dynamic>;
|
||||
final List<String> fileList = manifestMap.keys.toList();
|
||||
|
@ -18,6 +21,32 @@ Future<List<PizzaRecipe>> getRecipes() async {
|
|||
return pizzaRecipes;
|
||||
}
|
||||
|
||||
Future<List<PizzaRecipe>> getRecipesFromGithub() async {
|
||||
const String initialRecipeListUrl = "https://raw.githubusercontent.com/broodjeaap/PizzaRecipes/master/app_init/recipes.yaml";
|
||||
|
||||
final uri = Uri.parse(initialRecipeListUrl);
|
||||
if (!uri.isAbsolute){
|
||||
return const [];
|
||||
}
|
||||
final response = await http.get(uri);
|
||||
if (response.statusCode != 200) {
|
||||
return const [];
|
||||
}
|
||||
|
||||
final yamlBody = response.body;
|
||||
if (!yamlBody.startsWith("recipes:")){
|
||||
return const [];
|
||||
}
|
||||
final rootYaml = loadYaml(yamlBody) as YamlMap;
|
||||
final recipeList = rootYaml["recipes"] as YamlList;
|
||||
final returnRecipeList = <PizzaRecipe>[];
|
||||
for (final recipe in recipeList){
|
||||
returnRecipeList.add(await PizzaRecipe.fromParsedYaml(recipe as YamlMap));
|
||||
}
|
||||
return returnRecipeList;
|
||||
return const [];
|
||||
}
|
||||
|
||||
Future<String> loadAsset(String path) async {
|
||||
return rootBundle.loadString(path);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue