so Floor just sucks? commit before reverting

This commit is contained in:
broodjeaap89 2021-07-25 15:27:11 +02:00
parent 1382a4a843
commit 914e17fa3c
11 changed files with 163 additions and 35 deletions

View file

@ -433,7 +433,7 @@ class _$IngredientDao extends IngredientDao {
}
@override
Future<List<Ingredient>> getPizzaRecipeSteps(int pizzaRecipeId) async {
Future<List<Ingredient>> getPizzaRecipeIngredients(int pizzaRecipeId) async {
return _queryAdapter.queryList(
'SELECT * FROM Ingredient WHERE pizzaRecipeId = ?1',
mapper: (Map<String, Object?> row) => Ingredient(

View file

@ -1,6 +1,5 @@
import 'package:floor/floor.dart';
import 'package:flutter/material.dart';
import 'package:pizzaplanner/entities/PizzaDatabase.dart';
import 'package:pizzaplanner/util.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart';
@ -17,9 +16,11 @@ class PizzaRecipe {
final String name;
final String description;
//final List<Ingredient> ingredients;
@ignore
List<Ingredient> ingredients = [];
//final List<RecipeStep> recipeSteps;
@ignore
List<RecipeStep> recipeSteps = [];
PizzaRecipe(this.name, this.description, {this.id});
@ -36,9 +37,6 @@ class PizzaRecipe {
}
Future<Table> getIngredientsTable(int pizzaCount, int doughBallSize) async {
final database = await getDatabase();
final ingredientDao = database.ingredientDao;
final ingredients = await ingredientDao.getPizzaRecipeSteps(this.id!);
return Table(
border: TableBorder.all(),
columnWidths: const <int, TableColumnWidth>{
@ -56,7 +54,7 @@ class PizzaRecipe {
)
] +
ingredients.map((ingredient) =>
this.ingredients.map((ingredient) =>
ingredient.getIngredientTableRow(pizzaCount, doughBallSize))
.toList()
);
@ -124,14 +122,16 @@ class PizzaRecipe {
return Tuple4(pizzaRecipe, ingredients, recipeSteps, recipeSubSteps);
}
Future<Duration> getMinDuration() async {
List<RecipeStep> recipeSteps = await this.getRecipeSteps();
Duration getMinDuration() {
return Duration(seconds: recipeSteps.map((recipeStep) => recipeStep.getWaitMinInSeconds()).reduce((a, b) => a+b));
}
Duration getMaxDuration() {
return Duration(seconds: recipeSteps.map((recipeStep) => recipeStep.getWaitMaxInSeconds()).reduce((a, b) => a+b));
}
Future<Duration> getCurrentDuration() async {
List<RecipeStep> recipeSteps = await this.getRecipeSteps();
return Duration(seconds: recipeSteps.map((recipeStep) => recipeStep.getCurrentWaitInSeconds()).reduce((a, b) => a+b));
return Duration(seconds: this.recipeSteps.map((recipeStep) => recipeStep.getCurrentWaitInSeconds()).reduce((a, b) => a+b));
}
String toString() {
@ -141,8 +141,7 @@ class PizzaRecipe {
Future<Table> getStepTimeTable(DateTime startTime) async {
List<TableRow> stepRows = [];
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(startTime.millisecondsSinceEpoch);
List<RecipeStep> recipeSteps = await this.getRecipeSteps();
for (var recipeStep in recipeSteps.reversed) {
for (var recipeStep in this.recipeSteps.reversed) {
Duration stepWaitDuration = Duration(seconds: recipeStep.getCurrentWaitInSeconds());
stepRows.add(
TableRow(

View file

@ -21,7 +21,8 @@ class RecipeStep {
late int waitValue;
final String description;
// final List<RecipeSubStep> subSteps;
@ignore
List<RecipeSubStep> subSteps = [];
RecipeStep(this.pizzaRecipeId, this.name, this.description, this.waitDescription, this.waitUnit, this.waitMin, this.waitMax, {this.id}) {
waitValue = waitMin;
@ -61,10 +62,4 @@ class RecipeStep {
int getCurrentWaitInSeconds() {
return convertToSeconds(this.waitValue);
}
static Future<List<RecipeStep>> getRecipeStepForRecipe(PizzaRecipe pizzaRecipe) async {
final database = await getDatabase();
final recipeStepDao = database.recipeStepDao;
return recipeStepDao.getPizzaRecipeSteps(pizzaRecipe.id!);
}
}

View file

@ -0,0 +1,38 @@
import 'package:pizzaplanner/entities/PizzaDatabase.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
import 'package:pizzaplanner/util.dart';
class AppDao {
static const DATABASE_PATH = "pizza.db";
static Future<List<PizzaRecipe>> getPizzaRecipes() async {
final database = await $FloorPizzaDatabase.databaseBuilder(DATABASE_PATH).build();
final pizzaRecipeDao = database.pizzaRecipeDao;
final recipeStepDao = database.recipeStepDao;
final recipeSubStepDao = database.recipeSubStepDao;
final ingredientDao = database.ingredientDao;
List<PizzaRecipe> pizzaRecipes = await pizzaRecipeDao.getAllPizzaRecipes();
if (pizzaRecipes.isEmpty){
await loadYamlRecipesIntoDb();
pizzaRecipes = await pizzaRecipeDao.getAllPizzaRecipes();
}
for (var pizzaRecipe in pizzaRecipes) {
pizzaRecipe.ingredients = await ingredientDao.getPizzaRecipeIngredients(pizzaRecipe.id!);
print(pizzaRecipe.ingredients);
pizzaRecipe.recipeSteps = await recipeStepDao.getPizzaRecipeSteps(pizzaRecipe.id!);
print(pizzaRecipe.recipeSteps);
for (var pizzaRecipeStep in pizzaRecipe.recipeSteps){
pizzaRecipeStep.subSteps = await recipeSubStepDao.getRecipeStepSubSteps(pizzaRecipeStep.id!);
print(pizzaRecipeStep.subSteps);
}
}
return pizzaRecipes;
}
}

View file

@ -11,7 +11,7 @@ abstract class IngredientDao {
Stream<Ingredient?> findIngredientById(int id);
@Query("SELECT * FROM Ingredient WHERE pizzaRecipeId = :pizzaRecipeId")
Future<List<Ingredient>> getPizzaRecipeSteps(int pizzaRecipeId);
Future<List<Ingredient>> getPizzaRecipeIngredients(int pizzaRecipeId);
@insert
Future<void> insertIngredient(Ingredient ingredient);

View file

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:pizzaplanner/pages/AddPizzaEvent/AddPizzaEventPage.dart';
import 'package:pizzaplanner/pages/AddPizzaEvent/PickPizzaRecipePage.dart';
import 'package:pizzaplanner/pages/PizzaEventsPage.dart';
void main() {
@ -24,6 +25,9 @@ class RouteGenerator {
case "/": {
return MaterialPageRoute(builder: (context) => PizzaEventsPage());
}
case "/pickRecipe": {
return MaterialPageRoute(builder: (context) => PickPizzaRecipePage());
}
case "/add": {
return MaterialPageRoute(builder: (context) => AddPizzaEventPage());
}

View file

@ -15,7 +15,7 @@ class AddPizzaEventPage extends StatefulWidget {
class AddPizzaEventPageState extends State<AddPizzaEventPage> {
String name = "";
late Future<PizzaRecipe> pizzaRecipe;
final Future<List<PizzaRecipe>> pizzaRecipes = getRecipes();
//final Future<List<PizzaRecipe>> pizzaRecipes = getRecipes();
int pizzaCount = 1;
int doughBallSize = 250;
DateTime eventTime = DateTime.now();
@ -61,7 +61,7 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
children: <Widget>[
Icon(FontAwesome5.pizza_slice),
Container(width: 25),
Expanded(
/*Expanded(
child: FutureBuilder<List<PizzaRecipe>>(
future: pizzaRecipes,
builder: (BuildContext context, AsyncSnapshot<List<PizzaRecipe>> snapshot){
@ -84,7 +84,7 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
}
}
),
)
)*/
]
),
Row(

View file

@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
import 'package:pizzaplanner/entities/dao/AppDao.dart';
import 'package:pizzaplanner/widgets/PizzaRecipeWidget.dart';
class PickPizzaRecipePage extends StatefulWidget {
@override
PickPizzaRecipePageState createState() => PickPizzaRecipePageState();
}
class PickPizzaRecipePageState extends State<PickPizzaRecipePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Add Pizza2 Event"),
),
resizeToAvoidBottomInset: false,
body: Container(
padding: EdgeInsets.all(8),
child: Column(
children: <Widget>[
Expanded(
flex: 80,
child: FutureBuilder(
future: AppDao.getPizzaRecipes(),
builder: (BuildContext context, AsyncSnapshot<List<PizzaRecipe>> snapshot) {
if (snapshot.hasData && !snapshot.hasError){
return ListView(
children: snapshot.data!.map((pizzaRecipe) {
return PizzaRecipeWidget(pizzaRecipe);
}).toList(),
);
} else if (snapshot.hasError){
print(snapshot.error);
return Text("Something went wrong");
} else {
return Text("Loading Pizza Recipes");
}
}
)
),
Divider(),
SizedBox(
width: double.infinity,
height: 70,
child: Container(
color: Colors.blue,
child: TextButton(
child: Text("Review", style: TextStyle(color: Colors.white)),
onPressed: () {
},
)
)
)
]
)
)
);
}
}

View file

@ -28,7 +28,7 @@ class PizzaEventsState extends State<PizzaEventsPage> {
onPressed: () async {
final dynamic newPizzaEvent = await Navigator.pushNamed(
context,
"/add",
"/pickRecipe",
);
if (newPizzaEvent != null){

View file

@ -5,14 +5,7 @@ import 'package:intl/intl.dart';
import 'package:pizzaplanner/entities/PizzaDatabase.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
Future<List<PizzaRecipe>> getRecipes() async {
final database = await getDatabase();
final pizzaRecipeDao = database.pizzaRecipeDao;
final pizzaRecipes = await pizzaRecipeDao.getAllPizzaRecipes();
if (pizzaRecipes.isNotEmpty) {
return pizzaRecipes;
}
Future<List<PizzaRecipe>> loadYamlRecipesIntoDb() async {
// load recipes from yaml files in the asset directory
final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
@ -23,10 +16,11 @@ Future<List<PizzaRecipe>> getRecipes() async {
var parsedPizzaRecipe = await PizzaRecipe.fromYaml(filePath);
await parsedPizzaRecipe.item1.insert();
newPizzaRecipes.add(parsedPizzaRecipe.item1);
print(parsedPizzaRecipe.item1.name);
parsedPizzaRecipe.item2.forEach((ingredient) async { await ingredient.insert(); });
parsedPizzaRecipe.item3.forEach((recipeStep) async { await recipeStep.insert(); });
parsedPizzaRecipe.item4.forEach((recipeSubStep) async { await recipeSubStep.insert(); });
print(parsedPizzaRecipe.item1.description);
}
}
return newPizzaRecipes;

View file

@ -0,0 +1,37 @@
import 'package:flutter/material.dart';
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
class PizzaRecipeWidget extends StatelessWidget {
final PizzaRecipe pizzaRecipe;
PizzaRecipeWidget(this.pizzaRecipe);
@override
Widget build(BuildContext context) {
return Container(
height: 120,
color: Colors.blueAccent,
child: Container(
padding: const EdgeInsets.all(8),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(pizzaRecipe.name),
]
),
Text(pizzaRecipe.description),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("${pizzaRecipe.getMinDuration().inHours.round()} to ${pizzaRecipe.getMaxDuration().inHours.round()} hours")
]
)
]
)
)
);
}
}