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';
|
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
||||||
|
|
||||||
|
part 'PizzaEvent.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: 4)
|
||||||
class PizzaEvent {
|
class PizzaEvent {
|
||||||
final String name;
|
@HiveField(0)
|
||||||
final PizzaRecipe recipe;
|
String name;
|
||||||
final int pizzaCount;
|
|
||||||
final int doughBallSize;
|
@HiveField(1)
|
||||||
final DateTime dateTime;
|
PizzaRecipe recipe;
|
||||||
|
|
||||||
|
@HiveField(2)
|
||||||
|
int pizzaCount;
|
||||||
|
|
||||||
|
@HiveField(3)
|
||||||
|
int doughBallSize;
|
||||||
|
|
||||||
|
@HiveField(4)
|
||||||
|
DateTime dateTime;
|
||||||
|
|
||||||
PizzaEvent(
|
PizzaEvent(
|
||||||
this.name,
|
this.name,
|
||||||
|
|
|
@ -1,11 +1,20 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
|
||||||
import 'package:pizzaplanner/util.dart';
|
import 'package:pizzaplanner/util.dart';
|
||||||
|
|
||||||
class Ingredient {
|
part 'Ingredient.g.dart';
|
||||||
final String name;
|
|
||||||
final String unit;
|
@HiveType(typeId: 1)
|
||||||
final double value;
|
class Ingredient extends HiveObject {
|
||||||
|
@HiveField(0)
|
||||||
|
String name;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
String unit;
|
||||||
|
|
||||||
|
@HiveField(2)
|
||||||
|
double value;
|
||||||
|
|
||||||
Ingredient(this.name, this.unit, this.value);
|
Ingredient(this.name, this.unit, this.value);
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,27 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
import 'package:pizzaplanner/util.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/RecipeStep.dart';
|
||||||
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
||||||
import 'package:pizzaplanner/util.dart';
|
|
||||||
import 'package:yaml/yaml.dart';
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
class PizzaRecipe {
|
part 'PizzaRecipe.g.dart';
|
||||||
final String name;
|
|
||||||
final String description;
|
|
||||||
final List<Ingredient> ingredients;
|
|
||||||
|
|
||||||
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);
|
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';
|
import 'package:pizzaplanner/entities/PizzaRecipe/RecipeSubStep.dart';
|
||||||
|
|
||||||
class RecipeStep {
|
part 'RecipeStep.g.dart';
|
||||||
final String name;
|
|
||||||
final String waitDescription;
|
@HiveType(typeId: 2)
|
||||||
final String waitUnit;
|
class RecipeStep extends HiveObject {
|
||||||
final int waitMin;
|
@HiveField(0)
|
||||||
final int waitMax;
|
String name;
|
||||||
late int waitValue;
|
|
||||||
final String description;
|
@HiveField(1)
|
||||||
final List<RecipeSubStep> subSteps;
|
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) {
|
RecipeStep(this.name, this.description, this.waitDescription, this.waitUnit, this.waitMin, this.waitMax, this.subSteps) {
|
||||||
waitValue = waitMin;
|
waitValue = waitMin;
|
||||||
|
@ -40,6 +59,6 @@ class RecipeStep {
|
||||||
}
|
}
|
||||||
|
|
||||||
int getCurrentWaitInSeconds() {
|
int getCurrentWaitInSeconds() {
|
||||||
return convertToSeconds(this.waitValue);
|
return convertToSeconds(this.waitValue!);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,6 +1,15 @@
|
||||||
class RecipeSubStep {
|
|
||||||
final String name;
|
import 'package:hive/hive.dart';
|
||||||
final String description;
|
|
||||||
|
part 'RecipeSubStep.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: 3)
|
||||||
|
class RecipeSubStep extends HiveObject {
|
||||||
|
@HiveField(0)
|
||||||
|
String name;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
String description;
|
||||||
|
|
||||||
RecipeSubStep(this.name, this.description);
|
RecipeSubStep(this.name, this.description);
|
||||||
}
|
}
|
|
@ -1,9 +1,30 @@
|
||||||
import 'package:flutter/material.dart';
|
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/AddPizzaEventPage.dart';
|
||||||
import 'package:pizzaplanner/pages/PizzaEventsPage.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());
|
runApp(PizzaPlanner());
|
||||||
|
|
||||||
|
//await Hive.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
class PizzaPlanner extends StatelessWidget {
|
class PizzaPlanner extends StatelessWidget {
|
||||||
|
|
|
@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
|
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
|
||||||
import 'package:fluttericon/font_awesome5_icons.dart';
|
import 'package:fluttericon/font_awesome5_icons.dart';
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
||||||
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
import 'package:pizzaplanner/entities/PizzaRecipe/PizzaRecipe.dart';
|
||||||
import 'package:pizzaplanner/util.dart';
|
import 'package:pizzaplanner/util.dart';
|
||||||
|
@ -149,7 +150,7 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Slider(
|
child: Slider(
|
||||||
value: recipeStep.waitValue.toDouble(),
|
value: recipeStep.waitValue!.toDouble(),
|
||||||
min: recipeStep.waitMin.toDouble(),
|
min: recipeStep.waitMin.toDouble(),
|
||||||
max: recipeStep.waitMax.toDouble(),
|
max: recipeStep.waitMax.toDouble(),
|
||||||
divisions: recipeStep.waitMax - recipeStep.waitMin,
|
divisions: recipeStep.waitMax - recipeStep.waitMin,
|
||||||
|
@ -194,13 +195,17 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
|
||||||
if (eventTime == null){
|
if (eventTime == null){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Navigator.pop(context, PizzaEvent(
|
var pizzaEventsBox = await Hive.box<PizzaEvent>("PizzaEvents");
|
||||||
|
pizzaEventsBox.add(
|
||||||
|
PizzaEvent(
|
||||||
this.name,
|
this.name,
|
||||||
this.pizzaRecipe,
|
this.pizzaRecipe,
|
||||||
this.pizzaCount,
|
this.pizzaCount,
|
||||||
this.doughBallSize,
|
this.doughBallSize,
|
||||||
eventTime
|
eventTime
|
||||||
));
|
)
|
||||||
|
);
|
||||||
|
Navigator.pop(context);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,6 +3,9 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
import 'package:pizzaplanner/entities/PizzaEvent.dart';
|
||||||
import 'package:pizzaplanner/widgets/PizzaEventWidget.dart';
|
import 'package:pizzaplanner/widgets/PizzaEventWidget.dart';
|
||||||
|
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
|
|
||||||
class PizzaEventsPage extends StatefulWidget {
|
class PizzaEventsPage extends StatefulWidget {
|
||||||
@override
|
@override
|
||||||
PizzaEventsState createState() => PizzaEventsState();
|
PizzaEventsState createState() => PizzaEventsState();
|
||||||
|
@ -18,11 +21,22 @@ class PizzaEventsState extends State<PizzaEventsPage> {
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text("Pizza Events"),
|
title: Text("Pizza Events"),
|
||||||
),
|
),
|
||||||
body: ListView.separated(
|
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),
|
padding: const EdgeInsets.all(8),
|
||||||
itemCount: pizzaEvents.length,
|
itemCount: box.length,
|
||||||
itemBuilder: (BuildContext context, int i) => PizzaEventWidget(pizzaEvents[i]),
|
itemBuilder: (BuildContext context, int i) => PizzaEventWidget(box.getAt(i)!),
|
||||||
separatorBuilder: (BuildContext context, int i) => const Divider(),
|
separatorBuilder: (BuildContext context, int i) => const Divider(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
),
|
||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
|
|
|
@ -35,10 +35,16 @@ dependencies:
|
||||||
ref: "df4ed6fc2e24725604e90f79aedb98a7af7fb04d"
|
ref: "df4ed6fc2e24725604e90f79aedb98a7af7fb04d"
|
||||||
yaml: ^3.1.0
|
yaml: ^3.1.0
|
||||||
|
|
||||||
|
hive: ^2.0.4
|
||||||
|
hive_flutter: ^1.1.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
|
||||||
|
hive_generator: ^1.1.0
|
||||||
|
build_runner: ^2.0.6
|
||||||
|
|
||||||
# For information on the generic Dart part of this file, see the
|
# For information on the generic Dart part of this file, see the
|
||||||
# following page: https://dart.dev/tools/pub/pubspec
|
# following page: https://dart.dev/tools/pub/pubspec
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue