also switched hive box access to static const strings in the (entity) classes

This commit is contained in:
broodjeaap89 2021-09-24 20:25:04 +02:00
parent 2909f1c1d0
commit d697fdb7ef
11 changed files with 14 additions and 11 deletions

View file

@ -11,6 +11,8 @@ part 'pizza_recipe.g.dart';
@HiveType(typeId: 0) @HiveType(typeId: 0)
class PizzaRecipe extends HiveObject { class PizzaRecipe extends HiveObject {
static const String hiveName = "PizzaRecipes";
@HiveField(0) @HiveField(0)
String name; String name;

View file

@ -11,6 +11,8 @@ part 'pizza_event.g.dart';
@HiveType(typeId: 4) @HiveType(typeId: 4)
class PizzaEvent extends HiveObject{ class PizzaEvent extends HiveObject{
static const String hiveName = "PizzaEvent";
@HiveField(0) @HiveField(0)
String name; String name;

View file

@ -42,8 +42,8 @@ Future<void> main() async {
Hive.registerAdapter(RecipeSubStepAdapter()); Hive.registerAdapter(RecipeSubStepAdapter());
Hive.registerAdapter(IngredientAdapter()); Hive.registerAdapter(IngredientAdapter());
await Hive.openBox<PizzaEvent>("PizzaEvents"); await Hive.openBox<PizzaEvent>(PizzaEvent.hiveName);
final pizzaRecipesBox = await Hive.openBox<PizzaRecipe>("PizzaRecipes"); final pizzaRecipesBox = await Hive.openBox<PizzaRecipe>(PizzaRecipe.hiveName);
if (pizzaRecipesBox.isEmpty){ if (pizzaRecipesBox.isEmpty){
pizzaRecipesBox.addAll(await getRecipes()); pizzaRecipesBox.addAll(await getRecipes());

View file

@ -174,7 +174,7 @@ class AddPizzaEventPageState extends State<AddPizzaEventPage> {
.add(const Duration(minutes: 1)); .add(const Duration(minutes: 1));
} }
final pizzaEventsBox = Hive.box<PizzaEvent>("PizzaEvents"); final pizzaEventsBox = Hive.box<PizzaEvent>(PizzaEvent.hiveName);
final PizzaEvent pizzaEvent = PizzaEvent( final PizzaEvent pizzaEvent = PizzaEvent(
name, name,
widget.pizzaRecipe, widget.pizzaRecipe,

View file

@ -207,7 +207,7 @@ class AddRecipeURLPageState extends State<AddRecipeURLPage> {
} }
Future<void> addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async { Future<void> addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async {
final pizzaRecipeBox = Hive.box<PizzaRecipe>("PizzaRecipes"); final pizzaRecipeBox = Hive.box<PizzaRecipe>(PizzaRecipe.hiveName);
if (pizzaRecipeBox.containsKey(pizzaRecipe.key)) { if (pizzaRecipeBox.containsKey(pizzaRecipe.key)) {
return; // this recipe is already in the box return; // this recipe is already in the box
} }

View file

@ -199,7 +199,7 @@ class EditRecipePageState extends State<EditRecipePage> {
if (pizzaRecipe.isInBox){ if (pizzaRecipe.isInBox){
pizzaRecipe.save(); pizzaRecipe.save();
} else { } else {
final pizzaRecipesBox = await Hive.openBox<PizzaRecipe>("PizzaRecipes"); final pizzaRecipesBox = await Hive.openBox<PizzaRecipe>(PizzaRecipe.hiveName);
pizzaRecipesBox.add(pizzaRecipe); pizzaRecipesBox.add(pizzaRecipe);
} }
if (!mounted){ if (!mounted){

View file

@ -13,7 +13,7 @@ class PickPizzaRecipePage extends StatelessWidget {
return PizzaPlannerScaffold( return PizzaPlannerScaffold(
title: const Text("Pick Recipe"), title: const Text("Pick Recipe"),
body: ValueListenableBuilder( body: ValueListenableBuilder(
valueListenable: Hive.box<PizzaRecipe>("PizzaRecipes").listenable(), valueListenable: Hive.box<PizzaRecipe>(PizzaRecipe.hiveName).listenable(),
builder: (context, Box<PizzaRecipe> pizzaRecipesBox, widget) { builder: (context, Box<PizzaRecipe> pizzaRecipesBox, widget) {
return ListView.separated( return ListView.separated(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),

View file

@ -39,7 +39,7 @@ class PizzaEventNotificationState extends State<PizzaEventNotificationPage> {
final pizzaEventId = int.parse(split[0]); final pizzaEventId = int.parse(split[0]);
final recipeStepId = int.parse(split[1]); final recipeStepId = int.parse(split[1]);
final pizzaEventsBox = Hive.box<PizzaEvent>("PizzaEvents"); final pizzaEventsBox = Hive.box<PizzaEvent>(PizzaEvent.hiveName);
pizzaEvent = pizzaEventsBox.get(pizzaEventId)!; pizzaEvent = pizzaEventsBox.get(pizzaEventId)!;
recipeStep = pizzaEvent.recipe.recipeSteps[recipeStepId]; recipeStep = pizzaEvent.recipe.recipeSteps[recipeStepId];

View file

@ -25,7 +25,7 @@ class PizzaEventsState extends State<PizzaEventsPage> {
return PizzaPlannerScaffold( return PizzaPlannerScaffold(
title: const Text("Pizza Events"), title: const Text("Pizza Events"),
body: ValueListenableBuilder( body: ValueListenableBuilder(
valueListenable: Hive.box<PizzaEvent>("PizzaEvents").listenable(), valueListenable: Hive.box<PizzaEvent>(PizzaEvent.hiveName).listenable(),
builder: (context, Box<PizzaEvent> pizzaEventBox, widget) { builder: (context, Box<PizzaEvent> pizzaEventBox, widget) {
if (pizzaEventBox.isEmpty){ if (pizzaEventBox.isEmpty){
return Container(); return Container();

View file

@ -49,7 +49,7 @@ class RecipesPageState extends State<RecipesPage> {
Expanded( Expanded(
flex: 50, flex: 50,
child: ValueListenableBuilder( child: ValueListenableBuilder(
valueListenable: Hive.box<PizzaRecipe>("PizzaRecipes").listenable(), valueListenable: Hive.box<PizzaRecipe>(PizzaRecipe.hiveName).listenable(),
builder: (context, Box<PizzaRecipe> pizzaRecipesBox, widget) { builder: (context, Box<PizzaRecipe> pizzaRecipesBox, widget) {
return ListView.separated( return ListView.separated(
itemCount: pizzaRecipesBox.length, itemCount: pizzaRecipesBox.length,
@ -220,7 +220,7 @@ class RecipesPageState extends State<RecipesPage> {
try { try {
File(result.files.single.path).readAsString().then((String contents) async { File(result.files.single.path).readAsString().then((String contents) async {
final pizzaRecipe = await PizzaRecipe.fromYaml(contents); final pizzaRecipe = await PizzaRecipe.fromYaml(contents);
final pizzaRecipeBox = Hive.box<PizzaRecipe>("PizzaRecipes"); final pizzaRecipeBox = Hive.box<PizzaRecipe>(PizzaRecipe.hiveName);
pizzaRecipeBox.add(pizzaRecipe); pizzaRecipeBox.add(pizzaRecipe);
}); });
} catch (exception) { } catch (exception) {

View file

@ -12,6 +12,5 @@
## Refactor ## Refactor
- RecipeStep.waitUnit should probably be an enum? - RecipeStep.waitUnit should probably be an enum?
- refactor to const page names instead of loose strings everywhere ('/path/page') - refactor to const page names instead of loose strings everywhere ('/path/page')
- also do this with hive box names
## Bug ## Bug