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

Bug fix: DoneTaskCommand undo/redo #133

Merged
merged 2 commits into from
Nov 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,45 @@ public DoneTaskCommand(Index index) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Task> taskList = model.getFilteredTaskList();
super.canExecute();
boolean isDone = changeTaskIsDone(model);
displayedString = isDone
? MESSAGE_SUCCESS
: MESSAGE_UNDONE;

return new CommandResult(String.format(displayedString, completedTask));
}

@Override
public boolean equals(Object o) {
return this == o || (o instanceof DoneTaskCommand && index.equals(((DoneTaskCommand) o).index));
}

@Override
public CommandResult undo(Model model) throws CommandException {
requireNonNull(model);
super.canUndo();
changeTaskIsDone(model);
return new CommandResult(String.format(displayedString, this.completedTask));
}

private boolean changeTaskIsDone(Model model) throws CommandException {
List<Task> taskList = model.getFilteredTaskList();
if (index.getZeroBased() >= taskList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}
super.canExecute();

Task task = taskList.get(index.getZeroBased());
boolean isDone = task.isDone();
Task completedTask = new Task(task.getTitle(),
task.getDescription().orElse(null),
task.getTimestamp().orElse(null),
task.getTags(),
!task.isDone(),
task.getContacts());

this.completedTask = completedTask;
model.setTask(task, completedTask);

displayedString = isDone
? MESSAGE_UNDONE
: MESSAGE_SUCCESS;

return new CommandResult(String.format(displayedString, completedTask));
}

@Override
public boolean equals(Object o) {
return this == o || (o instanceof DoneTaskCommand && index.equals(((DoneTaskCommand) o).index));
}

@Override
public CommandResult undo(Model model) throws CommandException {
super.canUndo();
this.execute(model);
return new CommandResult(String.format(MESSAGE_SUCCESS,
this.completedTask));
return completedTask.isDone();
}
}