diff --git a/lib/entities/PizzaRecipe/pizza_recipe.dart b/lib/entities/PizzaRecipe/pizza_recipe.dart index a3ebfa5..ce15556 100644 --- a/lib/entities/PizzaRecipe/pizza_recipe.dart +++ b/lib/entities/PizzaRecipe/pizza_recipe.dart @@ -23,6 +23,12 @@ class PizzaRecipe extends HiveObject { @HiveField(3) List recipeSteps; + + // Using this because deleting it from the box does weird things. + // It seems to 'null' the item that you delete, and then reduce the size/length + // cutting off the last item + @HiveField(4) + bool deleted = false; PizzaRecipe(this.name, this.description, this.ingredients, this.recipeSteps); diff --git a/lib/pages/recipes_page.dart b/lib/pages/recipes_page.dart index bbd2a18..356a749 100644 --- a/lib/pages/recipes_page.dart +++ b/lib/pages/recipes_page.dart @@ -38,7 +38,7 @@ class RecipesPage extends StatelessWidget { itemCount: pizzaRecipesBox.length, itemBuilder: (context, i) { final pizzaRecipe = pizzaRecipesBox.get(i); - if (pizzaRecipe == null){ + if (pizzaRecipe == null || pizzaRecipe.deleted){ return const SizedBox(); } return InkWell( @@ -46,12 +46,69 @@ class RecipesPage extends StatelessWidget { Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe); }, onLongPress: () { - Navigator.pushNamed(context, "/recipes/edit", arguments: pizzaRecipe); + showDialog(context: context, builder: (BuildContext context) { + return AlertDialog( + title: Text(pizzaRecipe.name), + content: const Text("What do you want to do?"), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + Navigator.pushNamed(context, "/recipe/view", arguments: pizzaRecipe); + }, + child: const Text("View"), + ), + TextButton( + onPressed: () { + Navigator.pop(context); + Navigator.pushNamed(context, "/recipes/edit", arguments: pizzaRecipe); + }, + child: const Text("Edit"), + ), + TextButton( + onPressed: () { + Navigator.pop(context); + showDialog(context: context, builder: (BuildContext context) { + return AlertDialog( + title: const Text("Delete?"), + content: const Text("Are you sure?"), + actions: [ + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: const Text("Back"), + ), + TextButton( + onPressed: () async { + if (pizzaRecipe.isInBox){ + pizzaRecipe.deleted = true; + pizzaRecipe.save(); + } + Navigator.pop(context); + }, + child: const Text("Delete", style: TextStyle(color: Colors.redAccent)), + ), + ] + ); + }); + }, + child: const Text("Delete", style: TextStyle(color: Colors.redAccent)), + ), + ] + ); + }); }, child: PizzaRecipeWidget(pizzaRecipe), ); }, - separatorBuilder: (BuildContext context, int i) => const Divider(), + separatorBuilder: (BuildContext context, int i) { + final pizzaRecipe = pizzaRecipesBox.get(i); + if (pizzaRecipe == null || pizzaRecipe.deleted){ + return const SizedBox(); + } + return const Divider(); + }, ); } ),