diff --git a/kitchenowl/lib/cubits/recipe_add_update_cubit.dart b/kitchenowl/lib/cubits/recipe_add_update_cubit.dart index c5537ca3..2a0b4f28 100644 --- a/kitchenowl/lib/cubits/recipe_add_update_cubit.dart +++ b/kitchenowl/lib/cubits/recipe_add_update_cubit.dart @@ -1,5 +1,4 @@ import 'package:equatable/equatable.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:kitchenowl/helpers/named_bytearray.dart'; import 'package:kitchenowl/models/household.dart'; import 'package:kitchenowl/models/item.dart'; @@ -8,8 +7,9 @@ import 'package:kitchenowl/models/tag.dart'; import 'package:kitchenowl/services/api/api_service.dart'; import 'package:kitchenowl/services/transaction_handler.dart'; import 'package:kitchenowl/services/transactions/tag.dart'; +import 'package:replay_bloc/replay_bloc.dart'; -class AddUpdateRecipeCubit extends Cubit { +class AddUpdateRecipeCubit extends ReplayCubit { final Household household; final Recipe recipe; @@ -37,6 +37,7 @@ class AddUpdateRecipeCubit extends Cubit { final tags = await TransactionHandler.getInstance() .runTransaction(TransactionTagGetAll(household: household)); emit(state.copyWith(tags: tags)); + this.clearHistory(); } Future saveRecipe() async { @@ -79,6 +80,7 @@ class AddUpdateRecipeCubit extends Cubit { )); } emit(_state.copyWith(hasChanges: false)); + this.clearHistory(); } return null; diff --git a/kitchenowl/lib/l10n/app_en.arb b/kitchenowl/lib/l10n/app_en.arb index c4d484f6..4e990a01 100644 --- a/kitchenowl/lib/l10n/app_en.arb +++ b/kitchenowl/lib/l10n/app_en.arb @@ -261,6 +261,7 @@ "@recipesOverwriteDescription": {}, "@recipesRecent": {}, "@recipesSuggested": {}, + "@redo": {}, "@refresh": {}, "@remove": {}, "@rename": {}, @@ -315,6 +316,7 @@ "@totalTime": {}, "@uncategorized": {}, "@underConstruction": {}, + "@undo": {}, "@unlink": { "description": "As in remove link to social account (e.g. Google)" }, @@ -529,6 +531,7 @@ "recipesOverwriteDescription": "If activated, existing recipes with the same name will be overwritten", "recipesRecent": "Recent", "recipesSuggested": "Suggested", + "redo": "Redo", "refresh": "Refresh", "remove": "Remove", "rename": "Rename", @@ -571,6 +574,7 @@ "totalTime": "Total time", "uncategorized": "Uncategorized", "underConstruction": "Under construction", + "undo": "Undo", "unlink": "Unlink", "unreachableMessage": "Hmmmm… couldn't reach server", "unsavedChangesBody": "Are you sure you want to go back?", diff --git a/kitchenowl/lib/pages/recipe_add_update_page.dart b/kitchenowl/lib/pages/recipe_add_update_page.dart index 5101d07c..c1f73266 100644 --- a/kitchenowl/lib/pages/recipe_add_update_page.dart +++ b/kitchenowl/lib/pages/recipe_add_update_page.dart @@ -83,8 +83,18 @@ class _AddUpdateRecipePageState extends State { return BlocProvider( create: (context) => HouseholdCubit(widget.household), - child: BlocBuilder( + child: BlocConsumer( bloc: cubit, + listener: (context, state) { + if (nameController.text != state.name) + nameController.text = state.name; + if (descController.text != state.description) + descController.text = state.description; + if (yieldsController.text != state.yields.toString()) + yieldsController.text = state.yields.toString(); + if (sourceController.text != state.source) + sourceController.text = state.source; + }, buildWhen: (previous, current) => previous.hasChanges != current.hasChanges, builder: (context, state) { @@ -114,33 +124,46 @@ class _AddUpdateRecipePageState extends State { ? AppLocalizations.of(context)!.recipeEdit : AppLocalizations.of(context)!.recipeNew), actions: [ + BlocBuilder( + bloc: cubit, + builder: (context, state) => IconButton( + onPressed: cubit.canUndo ? cubit.undo : null, + icon: Icon(Icons.undo_rounded), + tooltip: AppLocalizations.of(context)!.undo, + ), + ), + BlocBuilder( + bloc: cubit, + builder: (context, state) => IconButton( + onPressed: cubit.canRedo ? cubit.redo : null, + icon: Icon(Icons.redo_rounded), + tooltip: AppLocalizations.of(context)!.redo, + ), + ), if (mobileLayout) BlocBuilder( bloc: cubit, - builder: (context, state) { - return LoadingIconButton( - icon: const Icon(Icons.save_rounded), - tooltip: AppLocalizations.of(context)!.save, - onPressed: state.isValid() && state.hasChanges - ? () async { - final recipe = await cubit.saveRecipe(); - if (!mounted) return; - Navigator.of(context) - .pop(UpdateEnum.updated); - if (recipe != null && - widget.openRecipeAfterCreation) { - context.go( - "/household/${cubit.household.id}/recipes/details/${recipe.id}", - extra: Tuple2( - cubit.household, - recipe, - ), - ); - } + builder: (context, state) => LoadingIconButton( + icon: const Icon(Icons.save_rounded), + tooltip: AppLocalizations.of(context)!.save, + onPressed: state.isValid() && state.hasChanges + ? () async { + final recipe = await cubit.saveRecipe(); + if (!mounted) return; + Navigator.of(context).pop(UpdateEnum.updated); + if (recipe != null && + widget.openRecipeAfterCreation) { + context.go( + "/household/${cubit.household.id}/recipes/details/${recipe.id}", + extra: Tuple2( + cubit.household, + recipe, + ), + ); } - : null, - ); - }, + } + : null, + ), ), ], ), diff --git a/kitchenowl/pubspec.lock b/kitchenowl/pubspec.lock index 8673a78c..b36eee7b 100755 --- a/kitchenowl/pubspec.lock +++ b/kitchenowl/pubspec.lock @@ -820,6 +820,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.6.0" + replay_bloc: + dependency: "direct main" + description: + name: replay_bloc + sha256: "36d0fb566e47aa0f1f96d567e93b442c3fa5c957777ef52a5f60d2f9e9d8e766" + url: "https://pub.dev" + source: hosted + version: "0.2.7" responsive_builder: dependency: "direct main" description: diff --git a/kitchenowl/pubspec.yaml b/kitchenowl/pubspec.yaml index 65dc3e6f..a78b6f50 100644 --- a/kitchenowl/pubspec.yaml +++ b/kitchenowl/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: flutter_secure_storage: ^9.0.0 shared_preferences: ^2.1.0 flutter_bloc: ^8.1.2 + replay_bloc: ^0.2.7 animations: ^2.0.7 shimmer: ^3.0.0 equatable: ^2.0.5