Skip to content

Commit

Permalink
Merge pull request #133 from koh-jx/master
Browse files Browse the repository at this point in the history
Bug fix: DoneTaskCommand undo/redo
  • Loading branch information
jeffsieu authored Nov 1, 2021
2 parents 1cac776 + e33e246 commit d160539
Showing 1 changed file with 25 additions and 21 deletions.
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();
}
}

0 comments on commit d160539

Please sign in to comment.