From d697fdb7ef326314e94b40ef50da700e75ead50b Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Fri, 24 Sep 2021 20:25:04 +0200 Subject: [PATCH] also switched hive box access to static const strings in the (entity) classes --- lib/entities/PizzaRecipe/pizza_recipe.dart | 2 ++ lib/entities/pizza_event.dart | 2 ++ lib/main.dart | 4 ++-- lib/pages/add_pizza_event_page.dart | 2 +- lib/pages/add_recipe_url.dart | 2 +- lib/pages/edit_recipe_page.dart | 2 +- lib/pages/pick_pizza_recipe_page.dart | 2 +- lib/pages/pizza_event_notification_page.dart | 2 +- lib/pages/pizza_events_page.dart | 2 +- lib/pages/recipes_page.dart | 4 ++-- todo.md | 1 - 11 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/entities/PizzaRecipe/pizza_recipe.dart b/lib/entities/PizzaRecipe/pizza_recipe.dart index 4457d13..ff1bd82 100644 --- a/lib/entities/PizzaRecipe/pizza_recipe.dart +++ b/lib/entities/PizzaRecipe/pizza_recipe.dart @@ -11,6 +11,8 @@ part 'pizza_recipe.g.dart'; @HiveType(typeId: 0) class PizzaRecipe extends HiveObject { + static const String hiveName = "PizzaRecipes"; + @HiveField(0) String name; diff --git a/lib/entities/pizza_event.dart b/lib/entities/pizza_event.dart index 9764191..ecbee56 100644 --- a/lib/entities/pizza_event.dart +++ b/lib/entities/pizza_event.dart @@ -11,6 +11,8 @@ part 'pizza_event.g.dart'; @HiveType(typeId: 4) class PizzaEvent extends HiveObject{ + static const String hiveName = "PizzaEvent"; + @HiveField(0) String name; diff --git a/lib/main.dart b/lib/main.dart index 5e35908..cbf55e4 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -42,8 +42,8 @@ Future main() async { Hive.registerAdapter(RecipeSubStepAdapter()); Hive.registerAdapter(IngredientAdapter()); - await Hive.openBox("PizzaEvents"); - final pizzaRecipesBox = await Hive.openBox("PizzaRecipes"); + await Hive.openBox(PizzaEvent.hiveName); + final pizzaRecipesBox = await Hive.openBox(PizzaRecipe.hiveName); if (pizzaRecipesBox.isEmpty){ pizzaRecipesBox.addAll(await getRecipes()); diff --git a/lib/pages/add_pizza_event_page.dart b/lib/pages/add_pizza_event_page.dart index 2e9781e..5882638 100644 --- a/lib/pages/add_pizza_event_page.dart +++ b/lib/pages/add_pizza_event_page.dart @@ -174,7 +174,7 @@ class AddPizzaEventPageState extends State { .add(const Duration(minutes: 1)); } - final pizzaEventsBox = Hive.box("PizzaEvents"); + final pizzaEventsBox = Hive.box(PizzaEvent.hiveName); final PizzaEvent pizzaEvent = PizzaEvent( name, widget.pizzaRecipe, diff --git a/lib/pages/add_recipe_url.dart b/lib/pages/add_recipe_url.dart index 0dd3efe..227de27 100644 --- a/lib/pages/add_recipe_url.dart +++ b/lib/pages/add_recipe_url.dart @@ -207,7 +207,7 @@ class AddRecipeURLPageState extends State { } Future addPizzaRecipeToBox(PizzaRecipe pizzaRecipe) async { - final pizzaRecipeBox = Hive.box("PizzaRecipes"); + final pizzaRecipeBox = Hive.box(PizzaRecipe.hiveName); if (pizzaRecipeBox.containsKey(pizzaRecipe.key)) { return; // this recipe is already in the box } diff --git a/lib/pages/edit_recipe_page.dart b/lib/pages/edit_recipe_page.dart index db5e77c..b060ea5 100644 --- a/lib/pages/edit_recipe_page.dart +++ b/lib/pages/edit_recipe_page.dart @@ -199,7 +199,7 @@ class EditRecipePageState extends State { if (pizzaRecipe.isInBox){ pizzaRecipe.save(); } else { - final pizzaRecipesBox = await Hive.openBox("PizzaRecipes"); + final pizzaRecipesBox = await Hive.openBox(PizzaRecipe.hiveName); pizzaRecipesBox.add(pizzaRecipe); } if (!mounted){ diff --git a/lib/pages/pick_pizza_recipe_page.dart b/lib/pages/pick_pizza_recipe_page.dart index 39eda63..0442c8f 100644 --- a/lib/pages/pick_pizza_recipe_page.dart +++ b/lib/pages/pick_pizza_recipe_page.dart @@ -13,7 +13,7 @@ class PickPizzaRecipePage extends StatelessWidget { return PizzaPlannerScaffold( title: const Text("Pick Recipe"), body: ValueListenableBuilder( - valueListenable: Hive.box("PizzaRecipes").listenable(), + valueListenable: Hive.box(PizzaRecipe.hiveName).listenable(), builder: (context, Box pizzaRecipesBox, widget) { return ListView.separated( padding: const EdgeInsets.all(8), diff --git a/lib/pages/pizza_event_notification_page.dart b/lib/pages/pizza_event_notification_page.dart index 50f8793..78207a0 100644 --- a/lib/pages/pizza_event_notification_page.dart +++ b/lib/pages/pizza_event_notification_page.dart @@ -39,7 +39,7 @@ class PizzaEventNotificationState extends State { final pizzaEventId = int.parse(split[0]); final recipeStepId = int.parse(split[1]); - final pizzaEventsBox = Hive.box("PizzaEvents"); + final pizzaEventsBox = Hive.box(PizzaEvent.hiveName); pizzaEvent = pizzaEventsBox.get(pizzaEventId)!; recipeStep = pizzaEvent.recipe.recipeSteps[recipeStepId]; diff --git a/lib/pages/pizza_events_page.dart b/lib/pages/pizza_events_page.dart index e95bf80..913898e 100644 --- a/lib/pages/pizza_events_page.dart +++ b/lib/pages/pizza_events_page.dart @@ -25,7 +25,7 @@ class PizzaEventsState extends State { return PizzaPlannerScaffold( title: const Text("Pizza Events"), body: ValueListenableBuilder( - valueListenable: Hive.box("PizzaEvents").listenable(), + valueListenable: Hive.box(PizzaEvent.hiveName).listenable(), builder: (context, Box pizzaEventBox, widget) { if (pizzaEventBox.isEmpty){ return Container(); diff --git a/lib/pages/recipes_page.dart b/lib/pages/recipes_page.dart index 73eb5ee..ec6d556 100644 --- a/lib/pages/recipes_page.dart +++ b/lib/pages/recipes_page.dart @@ -49,7 +49,7 @@ class RecipesPageState extends State { Expanded( flex: 50, child: ValueListenableBuilder( - valueListenable: Hive.box("PizzaRecipes").listenable(), + valueListenable: Hive.box(PizzaRecipe.hiveName).listenable(), builder: (context, Box pizzaRecipesBox, widget) { return ListView.separated( itemCount: pizzaRecipesBox.length, @@ -220,7 +220,7 @@ class RecipesPageState extends State { try { File(result.files.single.path).readAsString().then((String contents) async { final pizzaRecipe = await PizzaRecipe.fromYaml(contents); - final pizzaRecipeBox = Hive.box("PizzaRecipes"); + final pizzaRecipeBox = Hive.box(PizzaRecipe.hiveName); pizzaRecipeBox.add(pizzaRecipe); }); } catch (exception) { diff --git a/todo.md b/todo.md index 10b1cdb..79b8573 100644 --- a/todo.md +++ b/todo.md @@ -12,6 +12,5 @@ ## Refactor - RecipeStep.waitUnit should probably be an enum? - refactor to const page names instead of loose strings everywhere ('/path/page') - - also do this with hive box names ## Bug \ No newline at end of file