completed refactor of how completed steps are tracked, no longer by just substeps

This commit is contained in:
broodjeaap89 2021-08-03 19:17:24 +02:00
parent 319251c87e
commit 3026bc0bfa
4 changed files with 111 additions and 43 deletions

View file

@ -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<PizzaRecipe> fromYaml(yamlPath) async{
String yamlString = await loadAsset(yamlPath);
var yaml = loadYaml(yamlString);

View file

@ -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<RecipeSubStep> 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: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.name),
Text("$recipeSubStepsCompleted/$recipeSubSteps")
],
),
children: <Widget>[
Text(this.description),
] + subSteps.map((subStep) => subStep.buildPizzaEventSubStepWidget(pizzaEventPage)).toList()
);
}
Widget buildPizzaEventRecipeStepWidgetWithoutSubSteps(PizzaEventPageState pizzaEventPage) {
return ExpansionTile(
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Icon(FontAwesome5.sitemap),
Text(this.name),
Text("${this.completedOn == null ? 0 : 1}/1")
],
),
children: <Widget>[
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": {

View file

@ -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();
},
)
],
);
}
}

View file

@ -21,49 +21,10 @@ class PizzaEventPageState extends State<PizzaEventPage> {
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: <Widget>[
Icon(FontAwesome5.sitemap),
Text(recipeStep.name),
Text("$recipeSubStepsCompleted/$recipeSubSteps")
],
),
children: <Widget>[
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(() {});
}