switched to Hive for persistent object storage
This commit is contained in:
parent
41bdfe6e4c
commit
41d14a3e4c
9 changed files with 149 additions and 45 deletions
|
@ -1,12 +1,25 @@
|
|||
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
||||
|
||||
part 'PizzaEvent.g.dart';
|
||||
|
||||
@HiveType(typeId: 4)
|
||||
class PizzaEvent {
|
||||
final String name;
|
||||
final PizzaRecipe recipe;
|
||||
final int pizzaCount;
|
||||
final int doughBallSize;
|
||||
final DateTime dateTime;
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
PizzaRecipe recipe;
|
||||
|
||||
@HiveField(2)
|
||||
int pizzaCount;
|
||||
|
||||
@HiveField(3)
|
||||
int doughBallSize;
|
||||
|
||||
@HiveField(4)
|
||||
DateTime dateTime;
|
||||
|
||||
PizzaEvent(
|
||||
this.name,
|
||||
|
|
|
@ -1,11 +1,20 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
import 'package:pizzaplanner/util.dart';
|
||||
|
||||
class Ingredient {
|
||||
final String name;
|
||||
final String unit;
|
||||
final double value;
|
||||
part 'Ingredient.g.dart';
|
||||
|
||||
@HiveType(typeId: 1)
|
||||
class Ingredient extends HiveObject {
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String unit;
|
||||
|
||||
@HiveField(2)
|
||||
double value;
|
||||
|
||||
Ingredient(this.name, this.unit, this.value);
|
||||
|
||||
|
|
|
@ -1,19 +1,27 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:pizzaplanner/util.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart';
|
||||
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredients.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
||||
import 'package:pizzaplanner/util.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
class PizzaRecipe {
|
||||
final String name;
|
||||
final String description;
|
||||
final List<Ingredient> ingredients;
|
||||
part 'PizzaRecipe.g.dart';
|
||||
|
||||
final List<RecipeStep> recipeSteps;
|
||||
@HiveType(typeId: 0)
|
||||
class PizzaRecipe extends HiveObject {
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String description;
|
||||
|
||||
@HiveField(2)
|
||||
List<Ingredient> ingredients;
|
||||
|
||||
@HiveField(3)
|
||||
List<RecipeStep> recipeSteps;
|
||||
|
||||
PizzaRecipe(this.name, this.description, this.ingredients, this.recipeSteps);
|
||||
|
||||
|
|
|
@ -1,14 +1,33 @@
|
|||
import 'package:hive/hive.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
||||
|
||||
class RecipeStep {
|
||||
final String name;
|
||||
final String waitDescription;
|
||||
final String waitUnit;
|
||||
final int waitMin;
|
||||
final int waitMax;
|
||||
late int waitValue;
|
||||
final String description;
|
||||
final List<RecipeSubStep> subSteps;
|
||||
part 'RecipeStep.g.dart';
|
||||
|
||||
@HiveType(typeId: 2)
|
||||
class RecipeStep extends HiveObject {
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String waitDescription;
|
||||
|
||||
@HiveField(2)
|
||||
String waitUnit;
|
||||
|
||||
@HiveField(3)
|
||||
int waitMin;
|
||||
|
||||
@HiveField(4)
|
||||
int waitMax;
|
||||
|
||||
@HiveField(5)
|
||||
int? waitValue;
|
||||
|
||||
@HiveField(6)
|
||||
String description;
|
||||
|
||||
@HiveField(7)
|
||||
List<RecipeSubStep> subSteps;
|
||||
|
||||
RecipeStep(this.name, this.description, this.waitDescription, this.waitUnit, this.waitMin, this.waitMax, this.subSteps) {
|
||||
waitValue = waitMin;
|
||||
|
@ -40,6 +59,6 @@ class RecipeStep {
|
|||
}
|
||||
|
||||
int getCurrentWaitInSeconds() {
|
||||
return convertToSeconds(this.waitValue);
|
||||
return convertToSeconds(this.waitValue!);
|
||||
}
|
||||
}
|
|
@ -1,6 +1,15 @@
|
|||
class RecipeSubStep {
|
||||
final String name;
|
||||
final String description;
|
||||
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'RecipeSubStep.g.dart';
|
||||
|
||||
@HiveType(typeId: 3)
|
||||
class RecipeSubStep extends HiveObject {
|
||||
@HiveField(0)
|
||||
String name;
|
||||
|
||||
@HiveField(1)
|
||||
String description;
|
||||
|
||||
RecipeSubStep(this.name, this.description);
|
||||
}
|
|
@ -1,9 +1,30 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/Ingredient.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeStep.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
||||
import 'package:pizzaplanner/pages/AddPizzaEventPage.dart';
|
||||
import 'package:pizzaplanner/pages/PizzaEventsPage.dart';
|
||||
|
||||
void main() {
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
void main() async {
|
||||
await Hive.initFlutter();
|
||||
|
||||
Hive.registerAdapter(PizzaRecipeAdapter());
|
||||
Hive.registerAdapter(RecipeStepAdapter());
|
||||
Hive.registerAdapter(RecipeSubStepAdapter());
|
||||
Hive.registerAdapter(IngredientAdapter());
|
||||
Hive.registerAdapter(PizzaEventAdapter());
|
||||
|
||||
await Hive.openBox<PizzaEvent>("PizzaEvents");
|
||||
await Hive.openBox<PizzaRecipe>("PizzaRecipes");
|
||||
|
||||
runApp(PizzaPlanner());
|
||||
|
||||
//await Hive.close();
|
||||
}
|
||||
|
||||
class PizzaPlanner extends StatelessWidget {
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
|
||||
import 'package:fluttericon/font_awesome5_icons.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
||||
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
||||
import 'package:pizzaplanner/util.dart';
|
||||
|
@ -149,7 +150,7 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
|
|||
children: <Widget>[
|
||||
Expanded(
|
||||
child: Slider(
|
||||
value: recipeStep.waitValue.toDouble(),
|
||||
value: recipeStep.waitValue!.toDouble(),
|
||||
min: recipeStep.waitMin.toDouble(),
|
||||
max: recipeStep.waitMax.toDouble(),
|
||||
divisions: recipeStep.waitMax - recipeStep.waitMin,
|
||||
|
@ -188,19 +189,23 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
|
|||
DateTime? eventTime = await showDialog(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return ConfirmPizzaEventDialog(name: name, pizzaRecipe: pizzaRecipe, pizzaCount: pizzaCount, doughBallSize: doughBallSize);
|
||||
return ConfirmPizzaEventDialog(name: name, pizzaRecipe: pizzaRecipe, pizzaCount: pizzaCount, doughBallSize: doughBallSize);
|
||||
}
|
||||
);
|
||||
if (eventTime == null){
|
||||
return;
|
||||
}
|
||||
Navigator.pop(context, PizzaEvent(
|
||||
this.name,
|
||||
this.pizzaRecipe,
|
||||
this.pizzaCount,
|
||||
this.doughBallSize,
|
||||
eventTime
|
||||
));
|
||||
var pizzaEventsBox = await Hive.box<PizzaEvent>("PizzaEvents");
|
||||
pizzaEventsBox.add(
|
||||
PizzaEvent(
|
||||
this.name,
|
||||
this.pizzaRecipe,
|
||||
this.pizzaCount,
|
||||
this.doughBallSize,
|
||||
eventTime
|
||||
)
|
||||
);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
)
|
||||
)
|
||||
|
|
|
@ -3,6 +3,9 @@ import 'package:flutter/material.dart';
|
|||
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
||||
import 'package:pizzaplanner/widgets/PizzaEventWidget.dart';
|
||||
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
|
||||
class PizzaEventsPage extends StatefulWidget {
|
||||
@override
|
||||
PizzaEventsState createState() => PizzaEventsState();
|
||||
|
@ -18,11 +21,22 @@ class PizzaEventsState extends State<PizzaEventsPage> {
|
|||
appBar: AppBar(
|
||||
title: Text("Pizza Events"),
|
||||
),
|
||||
body: ListView.separated(
|
||||
padding: const EdgeInsets.all(8),
|
||||
itemCount: pizzaEvents.length,
|
||||
itemBuilder: (BuildContext context, int i) => PizzaEventWidget(pizzaEvents[i]),
|
||||
separatorBuilder: (BuildContext context, int i) => const Divider(),
|
||||
body: Container(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: ValueListenableBuilder(
|
||||
valueListenable: Hive.box<PizzaEvent>("PizzaEvents").listenable(),
|
||||
builder: (context, Box<PizzaEvent> box, widget) {
|
||||
if (box.isEmpty){
|
||||
return Container();
|
||||
}
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.all(8),
|
||||
itemCount: box.length,
|
||||
itemBuilder: (BuildContext context, int i) => PizzaEventWidget(box.getAt(i)!),
|
||||
separatorBuilder: (BuildContext context, int i) => const Divider(),
|
||||
);
|
||||
}
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
|
|
|
@ -35,10 +35,16 @@ dependencies:
|
|||
ref: "df4ed6fc2e24725604e90f79aedb98a7af7fb04d"
|
||||
yaml: ^3.1.0
|
||||
|
||||
hive: ^2.0.4
|
||||
hive_flutter: ^1.1.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
hive_generator: ^1.1.0
|
||||
build_runner: ^2.0.6
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
# following page: https://dart.dev/tools/pub/pubspec
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue