From 3026bc0bfa68142f10d8f459deff7254e8ab1cdf Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Tue, 3 Aug 2021 19:17:24 +0200 Subject: [PATCH] completed refactor of how completed steps are tracked, no longer by just substeps --- lib/entities/PizzaRecipe/PizzaRecipe.dart | 7 ++ lib/entities/PizzaRecipe/RecipeStep.dart | 76 ++++++++++++++++++++- lib/entities/PizzaRecipe/RecipeSubStep.dart | 26 +++++++ lib/pages/PizzaEventPage.dart | 45 +----------- 4 files changed, 111 insertions(+), 43 deletions(-) diff --git a/lib/entities/PizzaRecipe/PizzaRecipe.dart b/lib/entities/PizzaRecipe/PizzaRecipe.dart index 18302ba..2e9b529 100644 --- a/lib/entities/PizzaRecipe/PizzaRecipe.dart +++ b/lib/entities/PizzaRecipe/PizzaRecipe.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; +import 'package:pizzaplanner/pages/PizzaEventPage.dart'; import 'package:pizzaplanner/util.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart'; @@ -60,6 +61,12 @@ class PizzaRecipe extends HiveObject { return stepCount; } + Widget getPizzaEventRecipeWidget(PizzaEventPageState pizzaEventPage) { + return ListView( + children: this.recipeSteps.map((recipeStep) => recipeStep.buildPizzaEventRecipeStepWidget(pizzaEventPage)).toList() + ); + } + static Future fromYaml(yamlPath) async{ String yamlString = await loadAsset(yamlPath); var yaml = loadYaml(yamlString); diff --git a/lib/entities/PizzaRecipe/RecipeStep.dart b/lib/entities/PizzaRecipe/RecipeStep.dart index 17f5f6c..2429604 100644 --- a/lib/entities/PizzaRecipe/RecipeStep.dart +++ b/lib/entities/PizzaRecipe/RecipeStep.dart @@ -1,5 +1,8 @@ +import 'package:flutter/material.dart'; +import 'package:fluttericon/font_awesome5_icons.dart'; import 'package:hive/hive.dart'; import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart'; +import 'package:pizzaplanner/pages/PizzaEventPage.dart'; part 'RecipeStep.g.dart'; @@ -29,12 +32,83 @@ class RecipeStep extends HiveObject { @HiveField(7) List subSteps; - bool get completed => subSteps.every((subStep) => subStep.completed); + @HiveField(8) + DateTime? completedOn; + + bool get completed => _completed(); RecipeStep(this.name, this.description, this.waitDescription, this.waitUnit, this.waitMin, this.waitMax, this.subSteps) { waitValue = waitMin; } + bool _completed(){ + return subSteps.length > 0 ? + subSteps.every((subStep) => subStep.completed) : + completedOn != null; + } + + Widget buildPizzaEventRecipeStepWidget(PizzaEventPageState pizzaEventPage){ + return this.subSteps.length > 0 ? + buildPizzaEventRecipeStepWidgetWithSubSteps(pizzaEventPage) : + buildPizzaEventRecipeStepWidgetWithoutSubSteps(pizzaEventPage); + } + + Widget buildPizzaEventRecipeStepWidgetWithSubSteps(PizzaEventPageState pizzaEventPage) { + int recipeSubStepsCompleted = this.subSteps.where((subStep) => subStep.completed).length; + int recipeSubSteps = this.subSteps.length; + return ExpansionTile( + title: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Icon(FontAwesome5.sitemap), + Text(this.name), + Text("$recipeSubStepsCompleted/$recipeSubSteps") + ], + ), + children: [ + Text(this.description), + + ] + subSteps.map((subStep) => subStep.buildPizzaEventSubStepWidget(pizzaEventPage)).toList() + ); + } + + Widget buildPizzaEventRecipeStepWidgetWithoutSubSteps(PizzaEventPageState pizzaEventPage) { + return ExpansionTile( + title: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Icon(FontAwesome5.sitemap), + Text(this.name), + Text("${this.completedOn == null ? 0 : 1}/1") + ], + ), + children: [ + Text(this.description), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text(this.name), + Checkbox( + value: this.completedOn != null, + onChanged: (bool? newValue) async { + if (newValue == null){ + return; + } + if (newValue){ + this.completedOn = DateTime.now(); + } else { + this.completedOn = null; + } + await pizzaEventPage.widget.pizzaEvent.save(); + pizzaEventPage.triggerSetState(); + }, + ) + ], + ) + ] + ); + } + int convertToSeconds(int value){ switch (waitUnit){ case "minutes": { diff --git a/lib/entities/PizzaRecipe/RecipeSubStep.dart b/lib/entities/PizzaRecipe/RecipeSubStep.dart index ab137d6..2375091 100644 --- a/lib/entities/PizzaRecipe/RecipeSubStep.dart +++ b/lib/entities/PizzaRecipe/RecipeSubStep.dart @@ -1,5 +1,7 @@ +import 'package:flutter/material.dart'; import 'package:hive/hive.dart'; +import 'package:pizzaplanner/pages/PizzaEventPage.dart'; part 'RecipeSubStep.g.dart'; @@ -17,4 +19,28 @@ class RecipeSubStep extends HiveObject { bool get completed => completedOn != null; RecipeSubStep(this.name, this.description); + + Widget buildPizzaEventSubStepWidget(PizzaEventPageState pizzaEventPage) { + return Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + Text(this.name), + Checkbox( + value: this.completed, + onChanged: (bool? newValue) async { + if (newValue == null){ + return; + } + if (newValue){ + this.completedOn = DateTime.now(); + } else { + this.completedOn = null; + } + await pizzaEventPage.widget.pizzaEvent.save(); + pizzaEventPage.triggerSetState(); + }, + ) + ], + ); + } } \ No newline at end of file diff --git a/lib/pages/PizzaEventPage.dart b/lib/pages/PizzaEventPage.dart index 4d70737..2d619a3 100644 --- a/lib/pages/PizzaEventPage.dart +++ b/lib/pages/PizzaEventPage.dart @@ -21,49 +21,10 @@ class PizzaEventPageState extends State { resizeToAvoidBottomInset: false, body: Container( padding: EdgeInsets.all(10), - child: ListView( - children: this.widget.pizzaEvent.recipe.recipeSteps.map((recipeStep) { - int recipeSubStepsCompleted = recipeStep.subSteps.where((subStep) => subStep.completed).length; - int recipeSubSteps = recipeStep.subSteps.length != 0 ? recipeStep.subSteps.length : 1; - return ExpansionTile( - title: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Icon(FontAwesome5.sitemap), - Text(recipeStep.name), - Text("$recipeSubStepsCompleted/$recipeSubSteps") - ], - ), - children: [ - Text(recipeStep.description), - - ] + recipeStep.subSteps.map((subStep) { - return Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Text(subStep.name), - Checkbox( - value: subStep.completed, - onChanged: (bool? newValue) async { - if (newValue == null){ - return; - } - if (newValue){ - subStep.completedOn = DateTime.now(); - } else { - subStep.completedOn = null; - } - await this.widget.pizzaEvent.save(); - setState(() {}); - }, - ) - ], - ); - }).toList(), - ); - }).toList(), - ) + child: this.widget.pizzaEvent.recipe.getPizzaEventRecipeWidget(this) ) ); } + + triggerSetState() => setState(() {}); } \ No newline at end of file