Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor undo #130

Merged
merged 14 commits into from Oct 21, 2019
Merged

Refactor undo #130

merged 14 commits into from Oct 21, 2019

Conversation

lrchema
Copy link

@lrchema lrchema commented Oct 19, 2019

Fixes #133

@lrchema lrchema requested a review from blimyj October 21, 2019 08:59
@lrchema lrchema changed the title WIP: Refactor undo Refactor undo Oct 21, 2019
blimyj
blimyj previously approved these changes Oct 21, 2019
Copy link

@blimyj blimyj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! You might want to consider some of the minor readability changes I suggested. Otherwise, LGTM! 😄

src/main/java/seedu/address/model/item/VisualizeList.java Outdated Show resolved Hide resolved
src/main/java/seedu/address/model/ItemIndexWrapper.java Outdated Show resolved Hide resolved
Icesiolz
Icesiolz previously approved these changes Oct 21, 2019
Copy link
Collaborator

@Icesiolz Icesiolz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to merge!

Copy link

@sianghwee sianghwee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just some feedback on the undo functionality.



@Override
public CommandResult execute(ItemModel model) {
requireNonNull(model);
beforeClear = model.getItemStorage().deepCopy();
System.out.println(beforeClear.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line for debugging purposes? Perhaps we should remove it?


@Override
public void reverse(ItemModel model) throws CommandException {
System.out.println(beforeClear.size());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this line too!

@@ -16,4 +17,7 @@ public CommandResult execute(ItemModel model) {
return new CommandResult(MESSAGE_EXIT_ACKNOWLEDGEMENT, false, true);
}

@Override
public void reverse(ItemModel model) throws CommandException { }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps include a comment here to explain why this is an empty code block.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think that this might not be a good practice cause we should not have a method block just for the sake of having one. Perhaps you can consider creating an abstract class called UndoableCommand from which all the command that have reverse extends from while for the rest you can just extend from command. This will also make it easier to catch for error if a command cannot be undo.

model.findItem(searchString);
return new CommandResult(
String.format(Messages.MESSAGE_ITEM_LISTED_OVERVIEW, model.getVisualList().size()));
}

@Override
public void reverse(ItemModel model) throws CommandException {
model.setVisualizeList(beforeFilter);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}

@Override
public CommandResult execute(ItemModel model) throws CommandException {
if (elisaStateHistory.size() <= 1) {
if (elisaCommandHistory.size() <= 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case it is fine to use == instead as there is no way that the size will be less than 0?

return new CommandResult("Undo successful!", true);
Command lastDone = elisaCommandHistory.popCommand();
lastDone.reverse(model);
return new CommandResult("Undo successful!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you are able to undo the command, maybe you can consider adding the command that you just undo so that the user is able to tell what was the command that was undone. I believe that it can be done by Command.COMMAND_WORD to get the command.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command class doesn't have the COMMAND_WORD, I will create a getter for it

* */
@Override
public void pushCommand(Command command) {
if (!(command instanceof UndoCommand || command instanceof ExitCommand)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is one case where having a separate abstract class called UndoableCommand might be good.

if (undoStack.size() > 0) {
return undoStack.pop();
} else {
return null;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps consider throwing the exception that there is no command in the undo stack from here so that there is no need to do 2 checks within the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I don't need to check here since the only usage of pop is inside UndoCommand where there is the check for empty stack

Copy link

@sianghwee sianghwee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some small comments that can be changed in the next PR. Good to go!

@@ -17,6 +17,6 @@
*/
public abstract CommandResult execute(ItemModel model) throws CommandException;

public abstract void reverse(ItemModel model) throws CommandException;
public abstract String getCommandWord();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should have been implemented in UndoableCommand but just change it in the next iteration.

lastDone.reverse(model);
return new CommandResult("Undo successful!");
return new CommandResult("Undo [" + lastDone.getCommandWord() + "] command successful!");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can attempt using String.format() next time to make it less messy.

Copy link

@blimyj blimyj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@blimyj blimyj merged commit fe19929 into AY1920S1-CS2103T-T10-3:master Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor undo to reverse method not state method
5 participants