Skip to content

Commit

Permalink
fix undo command
Browse files Browse the repository at this point in the history
  • Loading branch information
ghurabah93 committed Nov 6, 2016
1 parent dedc971 commit f6ee110
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 38 deletions.
16 changes: 15 additions & 1 deletion src/main/java/seedu/task/logic/RollBackCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@ public class RollBackCommand {
private String commandWord;
private Task newTask;
private Task oldTask;

private int previousIndex;
private int currentIndex;

public RollBackCommand(String commandWord, Task newTask, Task oldTask) {
this.commandWord = commandWord;
this.newTask = newTask;
this.oldTask = oldTask;
}

public RollBackCommand(String commandWord, Task newTask, Task oldTask,int currentIndex) {
this.commandWord = commandWord;
this.newTask = newTask;
this.oldTask = oldTask;
this.currentIndex = currentIndex;

}

public String getCommandWord() {
return commandWord;
}
Expand All @@ -32,4 +42,8 @@ public Task getNewTask() {
public Task getOldTask() {
return oldTask;
}

public int getCurrentIndex() {
return currentIndex;
}
}
8 changes: 5 additions & 3 deletions src/main/java/seedu/task/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public CommandResult execute(boolean isUndo) {
assert model != null;
try {
model.addTask(toAdd);
if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, toAdd, null));
}

// @@author A0147944U
// Sorts updated list of tasks
model.autoSortBasedOnCurrentSortPreference();
// @@author A0147335E-reused
int currentIndex = model.getTaskManager().getTaskList().indexOf(toAdd);
if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, toAdd, null, currentIndex));
}
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
} catch (UniqueTaskList.DuplicateTaskException e) {
return new CommandResult(MESSAGE_DUPLICATE_TASK);
Expand Down
32 changes: 20 additions & 12 deletions src/main/java/seedu/task/logic/commands/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@ public class DoneCommand extends Command {

public static final String MESSAGE_ALREADY_DONE = "Task has already been done!";

public final int targetIndex;

public int targetIndex;
public int currentIndex;
public DoneCommand(int targetIndex) {
this.targetIndex = targetIndex;
currentIndex = targetIndex;
}

public DoneCommand(int targetIndex, int currentIndex)
{
this.targetIndex = targetIndex;
this.currentIndex = currentIndex;
}
@Override
public CommandResult execute(boolean isUndo) {
assert model != null;
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

if (lastShownList.size() < targetIndex) {
if (lastShownList.size() < currentIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

ReadOnlyTask currentTask = lastShownList.get(targetIndex - 1);
ReadOnlyTask currentTask = lastShownList.get(currentIndex - 1);
boolean oldStatus = currentTask.getStatus().getDoneStatus();

try {
Expand All @@ -51,14 +57,14 @@ public CommandResult execute(boolean isUndo) {
assert false : "The target task cannot be missing";
}

Task newTask = new Task(currentTask);
newTask.getStatus().setDoneStatus(true);
Task taskToDone = new Task(currentTask);
taskToDone.getStatus().setDoneStatus(true);

try {
model.addTask(targetIndex - 1, newTask);
model.addTask(targetIndex - 1, taskToDone);
} catch (UniqueTaskList.DuplicateTaskException e) {}

if (oldStatus == newTask.getStatus().getDoneStatus()) {
if (oldStatus == taskToDone.getStatus().getDoneStatus()) {
return new CommandResult(MESSAGE_ALREADY_DONE);
} else {
// @@author A0147944U
Expand All @@ -67,14 +73,16 @@ public CommandResult execute(boolean isUndo) {
}
// @@author A0147335E

if (!isUndo) {
history.getUndoList().add(new RollBackCommand(COMMAND_WORD, newTask, null));
}

// @@author A0147944U
// Sorts updated list of tasks
model.autoSortBasedOnCurrentSortPreference();
// @@author A0147335E
return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, newTask.getName()));
int currentIndex = model.getTaskManager().getTaskList().indexOf(taskToDone);
if (!isUndo) {
history.getUndoList().add(new RollBackCommand(COMMAND_WORD, taskToDone, null, currentIndex));
}
return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, taskToDone.getName()));
}

}
25 changes: 20 additions & 5 deletions src/main/java/seedu/task/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class EditCommand extends Command {
public static final String EDIT_END_TIME = "end";
public static final String EDIT_DEADLINE = "due";
public static final String EDIT_TAG = "tag";
public final int targetIndex;
public int targetIndex;
public final int currentIndex;

//private final Task toEdit;
private final String toEdit;
private final String toEditItem;
Expand All @@ -61,6 +63,16 @@ public EditCommand(int targetIndex, String item, String editResult, Set<String>
this.toEdit = editResult;
this.toEditItem = item;
this.toEditTags = tags;
this.currentIndex = 0;

}

public EditCommand(int targetIndex, int currentIndex, String item, String editResult, Set<String> tags) throws IllegalValueException {
this.targetIndex = targetIndex;
this.toEdit = editResult;
this.toEditItem = item;
this.toEditTags = tags;
this.currentIndex = currentIndex;

}

Expand All @@ -71,7 +83,8 @@ public EditCommand(int targetIndex, String item, String editResult, Set<String>
public CommandResult execute(boolean isUndo) {
assert model != null;
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

if (currentIndex != 0)
targetIndex = currentIndex;
if (lastShownList.size() < targetIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
Expand Down Expand Up @@ -150,13 +163,15 @@ else if(end != null) {
assert false : "The target task cannot be missing";
}

if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, toAdd, (Task) currentTask));
}

// @@author A0147944U
// Sorts updated list of tasks
model.autoSortBasedOnCurrentSortPreference();
// @@author A0152958R
int currentIndex = model.getTaskManager().getTaskList().indexOf(toAdd);
if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, toAdd, (Task) currentTask, currentIndex));
}
return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS, toEdit));
}

Expand Down
20 changes: 15 additions & 5 deletions src/main/java/seedu/task/logic/commands/FavoriteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,25 @@ public class FavoriteCommand extends Command {

public static final String MESSAGE_ALREADY_FAVORITED = "Task has already been favorited!";

public final int targetIndex;
public int targetIndex;
public final int currentIndex;

public FavoriteCommand(int targetIndex) {
this.targetIndex = targetIndex;
this.currentIndex = 0;
}

public FavoriteCommand(int targetIndex, int currentIndex) {
this.targetIndex = targetIndex;
this.currentIndex = currentIndex;
}

@Override
public CommandResult execute(boolean isUndo) {
assert model != null;
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();

if (currentIndex != 0)
targetIndex = currentIndex;
if (lastShownList.size() < targetIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
Expand All @@ -65,13 +73,15 @@ public CommandResult execute(boolean isUndo) {
return new CommandResult(MESSAGE_ALREADY_FAVORITED);
}

if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, taskToFavorite, null));
}

// @@author A0147944U
// Sorts updated list of tasks
model.autoSortBasedOnCurrentSortPreference();
// @@author A0147335E
int currentIndex = model.getTaskManager().getTaskList().indexOf(taskToFavorite);
if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, taskToFavorite, null, currentIndex));
}
return new CommandResult(String.format(MESSAGE_FAVORITE_TASK_SUCCESS, taskToFavorite.getName()));
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/task/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public CommandResult execute(boolean isUndo) {
if (invalidKeyword) {
return new CommandResult(String.format(MESSAGE_FAILURE, keyword));
}
model.getTaskManager().getTaskList();
model.sortFilteredTaskList(keyword);
model.saveCurrentSortPreference(keyword);
if ("Default".equals(keyword)) {
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/seedu/task/logic/commands/UndoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,26 @@ private void prepareUndoRefreshCommand() {
}

private void prepareUndoDone(String[] commandParts) {
int undoIndex = lastIndexOfUndoList();

int currentIndex = getUndoList().get(undoIndex).getCurrentIndex() + 1;
int index = Integer.parseInt(commandParts[COMMAND_INDEX]);
Command command = new UndoneCommand(index);

Command command = new UndoneCommand(index, currentIndex);
setData(command);
executeCommand(command);
removePreviousCommand();
}

private void prepareUndoUndone(String[] commandParts) {
int undoIndex = lastIndexOfUndoList();

int currentIndex = getUndoList().get(undoIndex).getCurrentIndex() + 1;

int index = Integer.parseInt(commandParts[COMMAND_INDEX]);
Command command = new DoneCommand(index);


Command command = new DoneCommand(index, currentIndex);
setData(command);
executeCommand(command);
removePreviousCommand();
Expand Down Expand Up @@ -271,7 +281,8 @@ private void prepareUndoEdit(String[] commandParts) {
break;
}
try {
Command command = new EditCommand(index, toEditItem, toEdit, tagStringSet);
int currentIndex = getUndoList().get(undoIndex).getCurrentIndex() + 1;
Command command = new EditCommand(index, currentIndex, toEditItem, toEdit, tagStringSet);
setData(command);
executeCommand(command);
} catch (IllegalValueException e) {
Expand All @@ -281,8 +292,8 @@ private void prepareUndoEdit(String[] commandParts) {
}

private void prepareUndoAdd() {
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
Command command = new DeleteCommand(lastShownList.size());
int undoIndex = lastIndexOfUndoList();
Command command = new DeleteCommand(getUndoList().get(undoIndex).getCurrentIndex() + 1);
setData(command);
executeCommand(command);
removePreviousCommand();
Expand Down Expand Up @@ -335,7 +346,7 @@ private AddCommand addCommand(int index, HashSet<String> tagStringSet) throws Il
AddCommand command = new AddCommand(EMPTY_STRING + getUndoList().get(index).getNewTask().getName(),
EMPTY_STRING + getUndoList().get(index).getNewTask().getStartTime(),
EMPTY_STRING + getUndoList().get(index).getNewTask().getEndTime(),
EMPTY_STRING + getUndoList().get(index).getNewTask().getDeadline(), tagStringSet);
EMPTY_STRING + getUndoList().get(index).getNewTask().getDeadline(), tagStringSet, getUndoList().get(index).getNewTask().getStatus());
return command;
}

Expand Down
24 changes: 18 additions & 6 deletions src/main/java/seedu/task/logic/commands/UndoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,32 @@ public class UndoneCommand extends Command {

public static final String MESSAGE_ALREADY_UNDONE = "Task has already been undone!";

public final int targetIndex;
public int targetIndex;
public int currentIndex;


public UndoneCommand(int targetIndex)
{
this.targetIndex = targetIndex;
currentIndex = targetIndex;
}

public UndoneCommand(int targetIndex, int currentIndex)
{
this.targetIndex = targetIndex;
this.currentIndex = currentIndex;
}

@Override
public CommandResult execute(boolean isUndo) {
UnmodifiableObservableList<ReadOnlyTask> lastShownList = model.getFilteredTaskList();
assert model != null;
if (lastShownList.size() < targetIndex) {
if (lastShownList.size() < currentIndex) {
indicateAttemptToExecuteIncorrectCommand();
return new CommandResult(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

ReadOnlyTask currentTask = lastShownList.get(targetIndex - 1);
ReadOnlyTask currentTask = lastShownList.get(currentIndex - 1);
boolean previousDoneStatus = currentTask.getStatus().getDoneStatus();

try {
Expand All @@ -60,13 +69,16 @@ public CommandResult execute(boolean isUndo) {
if (isEqual(previousDoneStatus, taskToUndone.getStatus().getDoneStatus())) {
return new CommandResult(MESSAGE_ALREADY_UNDONE);
}
if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, taskToUndone, null));
}

// @@author A0147944U
// Sorts updated list of tasks
model.autoSortBasedOnCurrentSortPreference();
// @@author A0147335E
int currentIndex = model.getTaskManager().getTaskList().indexOf(taskToUndone);

if (!isUndo) {
getUndoList().add(new RollBackCommand(COMMAND_WORD, taskToUndone, null, currentIndex));
}
return new CommandResult(String.format(MESSAGE_DONE_TASK_SUCCESS, taskToUndone.getName()));
}

Expand Down

0 comments on commit f6ee110

Please sign in to comment.