diff --git a/.idea/misc.xml b/.idea/misc.xml index f4f6d79..01e9a27 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + \ No newline at end of file diff --git a/android/.idea/.gitignore b/android/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/android/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/android/.idea/compiler.xml b/android/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/android/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/android/.idea/gradle.xml b/android/.idea/gradle.xml new file mode 100644 index 0000000..b48fde7 --- /dev/null +++ b/android/.idea/gradle.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml new file mode 100644 index 0000000..6199cc2 --- /dev/null +++ b/android/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/android/.idea/modules.xml b/android/.idea/modules.xml new file mode 100644 index 0000000..bf609e0 --- /dev/null +++ b/android/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/android/.idea/runConfigurations.xml b/android/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/android/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/lib/entities/PizzaRecipe/PizzaRecipe.dart b/lib/entities/PizzaRecipe/PizzaRecipe.dart index 3dc8b98..a4f82b0 100644 --- a/lib/entities/PizzaRecipe/PizzaRecipe.dart +++ b/lib/entities/PizzaRecipe/PizzaRecipe.dart @@ -6,6 +6,7 @@ import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart'; +import 'package:tuple/tuple.dart'; import 'package:yaml/yaml.dart'; @entity @@ -61,7 +62,7 @@ class PizzaRecipe { ); } - static Future fromYaml(yamlPath) async{ + static Future, List, List>> fromYaml(yamlPath) async{ String yamlString = await loadAsset(yamlPath); var yaml = loadYaml(yamlString); var recipe = yaml["recipe"]; @@ -72,14 +73,13 @@ class PizzaRecipe { PizzaRecipe pizzaRecipe = PizzaRecipe(name, description); pizzaRecipe.insert(); - YamlList ingredients = recipe["ingredients"]; + YamlList ingredientsYamlList = recipe["ingredients"]; + List ingredients = ingredientsYamlList.map((ingredientYaml) => Ingredient(pizzaRecipe.id!, ingredientYaml["name"], ingredientYaml["unit"], ingredientYaml["value"])).toList(); - for (var ingredientYaml in ingredients){ - Ingredient ingredient = Ingredient(pizzaRecipe.id!, ingredientYaml["name"], ingredientYaml["unit"], ingredientYaml["value"]); - ingredient.insert(); - } YamlList steps = recipe["steps"]; + List recipeSteps = []; + List recipeSubSteps = []; for (var step in steps){ String stepName = step["name"]; String stepDescription = step["description"]; @@ -97,25 +97,31 @@ class PizzaRecipe { waitMin = waitMap["min"]; waitMax = waitMap["max"]; } - - RecipeStep recipeStep = RecipeStep( - pizzaRecipe.id!, - stepName, - stepDescription, - waitDescription, - waitUnit, - waitMin, - waitMax + var recipeStep = RecipeStep( + pizzaRecipe.id!, + stepName, + stepDescription, + waitDescription, + waitUnit, + waitMin, + waitMax + ); + recipeSteps.add( + recipeStep ); - recipeStep.insert(); YamlList subSteps = step.containsKey("substeps") ? step["substeps"] : YamlList(); for (var subStep in subSteps ) { - RecipeSubStep recipeSubStep = RecipeSubStep(recipeStep.id!, subStep["name"], subStep["description"]); - recipeSubStep.insert(); + recipeSubSteps.add( + RecipeSubStep( + recipeStep.id!, + subStep["name"], + subStep["description"] + ) + ); } } - return pizzaRecipe; + return Tuple4(pizzaRecipe, ingredients, recipeSteps, recipeSubSteps); } Future getMinDuration() async { diff --git a/lib/entities/PizzaRecipe/RecipeStep.dart b/lib/entities/PizzaRecipe/RecipeStep.dart index 5249918..f0ed265 100644 --- a/lib/entities/PizzaRecipe/RecipeStep.dart +++ b/lib/entities/PizzaRecipe/RecipeStep.dart @@ -61,4 +61,10 @@ class RecipeStep { int getCurrentWaitInSeconds() { return convertToSeconds(this.waitValue); } + + static Future> getRecipeStepForRecipe(PizzaRecipe pizzaRecipe) async { + final database = await getDatabase(); + final recipeStepDao = database.recipeStepDao; + return recipeStepDao.getPizzaRecipeSteps(pizzaRecipe.id!); + } } \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index e897d98..e1d772d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:pizzaplanner/pages/AddPizzaEventPage.dart'; +import 'package:pizzaplanner/pages/AddPizzaEvent/AddPizzaEventPage.dart'; import 'package:pizzaplanner/pages/PizzaEventsPage.dart'; void main() { diff --git a/lib/pages/AddPizzaEventPage.dart b/lib/pages/AddPizzaEvent/AddPizzaEventPage.dart similarity index 68% rename from lib/pages/AddPizzaEventPage.dart rename to lib/pages/AddPizzaEvent/AddPizzaEventPage.dart index 42576fa..eb1d734 100644 --- a/lib/pages/AddPizzaEventPage.dart +++ b/lib/pages/AddPizzaEvent/AddPizzaEventPage.dart @@ -4,6 +4,7 @@ import 'package:flutter_datetime_picker/flutter_datetime_picker.dart'; import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:pizzaplanner/entities/PizzaEvent.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; +import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart'; import 'package:pizzaplanner/util.dart'; class AddPizzaEventPage extends StatefulWidget { @@ -13,29 +14,14 @@ class AddPizzaEventPage extends StatefulWidget { class AddPizzaEventPageState extends State { String name = ""; - bool initialized = false; - late PizzaRecipe pizzaRecipe; - late List pizzaTypes; + late Future pizzaRecipe; + final Future> pizzaRecipes = getRecipes(); int pizzaCount = 1; int doughBallSize = 250; DateTime eventTime = DateTime.now(); bool nameValidation = false; - @override - void initState() { - super.initState(); - getRecipes().then((pTypes) { - this.pizzaTypes = pTypes; - this.pizzaRecipe = this.pizzaTypes.first; - setState(() {this.initialized = true;}); - }, onError: (e, stacktrace) { - print(e); - print(stacktrace); - Navigator.pop(context); - }); - } - @override Widget build(BuildContext context) { return Scaffold( @@ -70,24 +56,34 @@ class AddPizzaEventPageState extends State { ) ] ), + Row( children: [ Icon(FontAwesome5.pizza_slice), Container(width: 25), Expanded( - child: this.initialized ? // Only render the dropdown if the recipes have been loaded from storage - DropdownButton( - value: this.pizzaRecipe.name, - onChanged: (String? newType) { - setState(() => this.pizzaRecipe = this.pizzaTypes.firstWhere((pizzaRecipe) => pizzaRecipe.name == newType)); - }, - items: this.pizzaTypes.map((pizzaRecipe) { - return DropdownMenuItem( - value: pizzaRecipe.name, - child: Text(pizzaRecipe.name) - ); - }).toList() - ) : CircularProgressIndicator() + child: FutureBuilder>( + future: pizzaRecipes, + builder: (BuildContext context, AsyncSnapshot> snapshot){ + if(snapshot.hasData && !snapshot.hasError){ + //this.pizzaRecipe = snapshot.data!.first; + return DropdownButton( + value: "this.pizzaRecipe.name", + onChanged: (String? newType) { + //setState(() => this.pizzaRecipe = snapshot.data!.firstWhere((pizzaRecipe) => pizzaRecipe.name == newType)); + }, + items: snapshot.data!.map((pizzaRecipe) { + return DropdownMenuItem( + value: pizzaRecipe.name, + child: Text(pizzaRecipe.name) + ); + }).toList() + ); + } else { + return CircularProgressIndicator(); + } + } + ), ) ] ), @@ -141,31 +137,40 @@ class AddPizzaEventPageState extends State { flex: 45, child: ListView( children: [ - this.initialized ? Column( - children: this.pizzaRecipe.recipeSteps.where((recipeStep) => recipeStep.waitDescription.length > 0).map((recipeStep) { - return [ - Text(recipeStep.waitDescription), - Row( - children: [ - Expanded( - child: Slider( - value: recipeStep.waitValue.toDouble(), - min: recipeStep.waitMin.toDouble(), - max: recipeStep.waitMax.toDouble(), - divisions: recipeStep.waitMax - recipeStep.waitMin, - label: recipeStep.waitValue.toString(), - onChanged: (newValue) => this.setState(() => recipeStep.waitValue = newValue.toInt()), - ) - ), - Container( - width: 25, - child: Text(recipeStep.waitValue.toString()) + /*FutureBuilder( + future: RecipeStep.getRecipeStepForRecipe(this.pizzaRecipe), + builder: (BuildContext context, AsyncSnapshot> snapshot){ + if (snapshot.hasData && !snapshot.hasError) { + return Column( + children: snapshot.data!.where((recipeStep) => recipeStep.waitDescription.length > 0).map((recipeStep) { + return [ + Text(recipeStep.waitDescription), + Row( + children: [ + Expanded( + child: Slider( + value: recipeStep.waitValue.toDouble(), + min: recipeStep.waitMin.toDouble(), + max: recipeStep.waitMax.toDouble(), + divisions: recipeStep.waitMax - recipeStep.waitMin, + label: recipeStep.waitValue.toString(), + onChanged: (newValue) => this.setState(() => recipeStep.waitValue = newValue.toInt()), + ) + ), + Container( + width: 25, + child: Text(recipeStep.waitValue.toString()) + ) + ] ) - ] - ) - ]; - }).expand((option) => option).toList() - ) : Container(), + ]; + }).expand((option) => option).toList() + ); + } else { + return CircularProgressIndicator(); + } + } + )*/ ] ) ), @@ -188,15 +193,15 @@ class AddPizzaEventPageState extends State { DateTime? eventTime = await showDialog( context: context, builder: (context) { - return ConfirmPizzaEventDialog(name: name, pizzaRecipe: pizzaRecipe, pizzaCount: pizzaCount, doughBallSize: doughBallSize); + return Text("tmp") //ConfirmPizzaEventDialog(name: name, pizzaRecipe: pizzaRecipe, pizzaCount: pizzaCount, doughBallSize: doughBallSize); } ); if (eventTime == null){ return; } Navigator.pop(context, PizzaEvent( + 1, // this.pizzaRecipe.id!, this.name, - this.pizzaRecipe, this.pizzaCount, this.doughBallSize, eventTime @@ -234,10 +239,11 @@ class ConfirmPizzaEventState extends State { late final DateTime minTime; @override - void initState() { + void initState() async { super.initState(); - eventTime = DateTime.now().add(widget.pizzaRecipe.getCurrentDuration()).add(Duration(minutes: 1)); - minTime = DateTime.now().add(widget.pizzaRecipe.getCurrentDuration()); + var currentDuration = await widget.pizzaRecipe.getCurrentDuration(); + eventTime = DateTime.now().add(currentDuration).add(Duration(minutes: 1)); + minTime = DateTime.now().add(currentDuration); } @override @@ -255,7 +261,7 @@ class ConfirmPizzaEventState extends State { Text(widget.name), Divider(), Text("Ingredients"), - widget.pizzaRecipe.getIngredientsTable(widget.pizzaCount, widget.doughBallSize), + //widget.pizzaRecipe.getIngredientsTable(widget.pizzaCount, widget.doughBallSize), Divider(), SizedBox( width: double.infinity, @@ -293,7 +299,7 @@ class ConfirmPizzaEventState extends State { flex: 60, child: ListView( children: [ - widget.pizzaRecipe.getStepTimeTable(eventTime) + //widget.pizzaRecipe.getStepTimeTable(eventTime) ] ) ), diff --git a/lib/pages/AddPizzaEvent/PickPizzaRecipePage.dart b/lib/pages/AddPizzaEvent/PickPizzaRecipePage.dart new file mode 100644 index 0000000..e69de29 diff --git a/lib/pages/PizzaEventsPage.dart b/lib/pages/PizzaEventsPage.dart index 011a543..a9b242d 100644 --- a/lib/pages/PizzaEventsPage.dart +++ b/lib/pages/PizzaEventsPage.dart @@ -21,7 +21,7 @@ class PizzaEventsState extends State { body: ListView.separated( padding: const EdgeInsets.all(8), itemCount: pizzaEvents.length, - itemBuilder: (BuildContext context, int i) => PizzaEventWidget(pizzaEvents[i]), + itemBuilder: (BuildContext context, int i) => const Divider(),// PizzaEventWidget(pizzaEvents[i]), separatorBuilder: (BuildContext context, int i) => const Divider(), ), floatingActionButton: FloatingActionButton( diff --git a/lib/util.dart b/lib/util.dart index 98f0fe5..6470d17 100644 --- a/lib/util.dart +++ b/lib/util.dart @@ -6,17 +6,30 @@ import 'package:pizzaplanner/entities/PizzaDatabase.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; Future> getRecipes() async { + final database = await getDatabase(); + final pizzaRecipeDao = database.pizzaRecipeDao; + final pizzaRecipes = await pizzaRecipeDao.getAllPizzaRecipes(); + if (pizzaRecipes.isNotEmpty) { + return pizzaRecipes; + } + + // load recipes from yaml files in the asset directory final manifestContent = await rootBundle.loadString('AssetManifest.json'); final Map manifestMap = json.decode(manifestContent); final List fileList = manifestMap.keys.toList(); - final List pizzaRecipes = []; + final List newPizzaRecipes = []; for (var filePath in fileList) { if (filePath.startsWith("assets/recipes") && filePath.endsWith(".yaml")) { - PizzaRecipe pizzaRecipe = await PizzaRecipe.fromYaml(filePath); - pizzaRecipes.add(pizzaRecipe); + var parsedPizzaRecipe = await PizzaRecipe.fromYaml(filePath); + await parsedPizzaRecipe.item1.insert(); + newPizzaRecipes.add(parsedPizzaRecipe.item1); + + parsedPizzaRecipe.item2.forEach((ingredient) async { await ingredient.insert(); }); + parsedPizzaRecipe.item3.forEach((recipeStep) async { await recipeStep.insert(); }); + parsedPizzaRecipe.item4.forEach((recipeSubStep) async { await recipeSubStep.insert(); }); } } - return pizzaRecipes; + return newPizzaRecipes; } Future loadAsset(String path) async { diff --git a/lib/widgets/PizzaEventWidget.dart b/lib/widgets/PizzaEventWidget.dart index de87a12..8dd2aa1 100644 --- a/lib/widgets/PizzaEventWidget.dart +++ b/lib/widgets/PizzaEventWidget.dart @@ -1,11 +1,13 @@ import 'package:flutter/material.dart'; import 'package:pizzaplanner/entities/PizzaEvent.dart'; +import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart'; import 'package:pizzaplanner/util.dart'; class PizzaEventWidget extends StatelessWidget { final PizzaEvent pizzaEvent; + final PizzaRecipe pizzaRecipe; - PizzaEventWidget(this.pizzaEvent); + PizzaEventWidget(this.pizzaEvent, this.pizzaRecipe); @override Widget build(BuildContext context){ @@ -69,7 +71,7 @@ class PizzaEventWidget extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(getDateFormat().format(pizzaEvent.dateTime)), - Text(pizzaEvent.recipe.name) + Text(pizzaRecipe.name) ], ), diff --git a/pubspec.yaml b/pubspec.yaml index 8ea761b..b64a5f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -36,6 +36,7 @@ dependencies: yaml: ^3.1.0 floor: ^1.1.0 + tuple: ^2.0.0 dev_dependencies: flutter_test: