Skip to content

Commit

Permalink
feat: Undo/Redo for recipe editing (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored Jul 9, 2024
1 parent b913817 commit 09ab6f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
6 changes: 4 additions & 2 deletions kitchenowl/lib/cubits/recipe_add_update_cubit.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<AddUpdateRecipeState> {
class AddUpdateRecipeCubit extends ReplayCubit<AddUpdateRecipeState> {
final Household household;
final Recipe recipe;

Expand Down Expand Up @@ -37,6 +37,7 @@ class AddUpdateRecipeCubit extends Cubit<AddUpdateRecipeState> {
final tags = await TransactionHandler.getInstance()
.runTransaction(TransactionTagGetAll(household: household));
emit(state.copyWith(tags: tags));
this.clearHistory();
}

Future<Recipe?> saveRecipe() async {
Expand Down Expand Up @@ -79,6 +80,7 @@ class AddUpdateRecipeCubit extends Cubit<AddUpdateRecipeState> {
));
}
emit(_state.copyWith(hasChanges: false));
this.clearHistory();
}

return null;
Expand Down
4 changes: 4 additions & 0 deletions kitchenowl/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
"@recipesOverwriteDescription": {},
"@recipesRecent": {},
"@recipesSuggested": {},
"@redo": {},
"@refresh": {},
"@remove": {},
"@rename": {},
Expand Down Expand Up @@ -315,6 +316,7 @@
"@totalTime": {},
"@uncategorized": {},
"@underConstruction": {},
"@undo": {},
"@unlink": {
"description": "As in remove link to social account (e.g. Google)"
},
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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?",
Expand Down
71 changes: 47 additions & 24 deletions kitchenowl/lib/pages/recipe_add_update_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,18 @@ class _AddUpdateRecipePageState extends State<AddUpdateRecipePage> {

return BlocProvider(
create: (context) => HouseholdCubit(widget.household),
child: BlocBuilder<AddUpdateRecipeCubit, AddUpdateRecipeState>(
child: BlocConsumer<AddUpdateRecipeCubit, AddUpdateRecipeState>(
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) {
Expand Down Expand Up @@ -114,33 +124,46 @@ class _AddUpdateRecipePageState extends State<AddUpdateRecipePage> {
? AppLocalizations.of(context)!.recipeEdit
: AppLocalizations.of(context)!.recipeNew),
actions: [
BlocBuilder<AddUpdateRecipeCubit, AddUpdateRecipeState>(
bloc: cubit,
builder: (context, state) => IconButton(
onPressed: cubit.canUndo ? cubit.undo : null,
icon: Icon(Icons.undo_rounded),
tooltip: AppLocalizations.of(context)!.undo,
),
),
BlocBuilder<AddUpdateRecipeCubit, AddUpdateRecipeState>(
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<AddUpdateRecipeCubit, AddUpdateRecipeState>(
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<Household, Recipe>(
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<Household, Recipe>(
cubit.household,
recipe,
),
);
}
: null,
);
},
}
: null,
),
),
],
),
Expand Down
8 changes: 8 additions & 0 deletions kitchenowl/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 09ab6f7

Please sign in to comment.