Skip to content

Commit

Permalink
Merge pull request #90 from koh-jx/fix-Purge-Tasks
Browse files Browse the repository at this point in the history
Implement Purge undo, cleanup
  • Loading branch information
wlren authored Oct 27, 2021
2 parents 6e34d06 + 907fff1 commit efbbdba
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 19 deletions.
8 changes: 8 additions & 0 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ Deletes a specified task from the tasks list by a given index.

`task delete 2`

### Undo previous command : `undo`

Undos the previous command, returning the user back to the previous state, up to a maximum of 15 previous commands.

Format: `undo`

- Setting a filter is not considered a command that can be undone.

### Purge all visible tasks : `task purge`

Clears all visible (filtered) tasks from the address book.
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ public CommandResult execute(Model model) throws CommandException {

@Override
public CommandResult undo(Model model) throws CommandException {
if (this.canExecute) {
throw new CommandException(Messages.MESSAGE_UNABLE_TO_UNDO);
}
super.canUndo();
model.setAddressBook(this.oldAddressBook);
this.canExecute = true;
return new CommandResult(MESSAGE_SUCCESS);
}
}
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public CommandResult execute(Model model) throws CommandException {
super.canExecute();
this.previousPredicate = model.getFilteredPersonPredicate();
model.updateFilteredPersonList(predicate);
this.canExecute = false;
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand All @@ -58,7 +57,6 @@ public boolean equals(Object other) {
public CommandResult undo(Model model) throws CommandException {
super.canUndo();
model.updateFilteredPersonList(this.previousPredicate);
this.canExecute = true;
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public CommandResult undo(Model model) throws CommandException {
super.canUndo();
Predicate<? super Task> predicate = model.getFilteredTaskPredicate();
model.insertTask(deletedTask, targetIndex.getZeroBased());
this.canExecute = true;
return new CommandResult(String.format(MESSAGE_SUCCESS, deletedTask));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public boolean equals(Object o) {
public CommandResult undo(Model model) throws CommandException {
super.canUndo();
this.execute(model);
this.canExecute = true;
return new CommandResult(String.format(MESSAGE_SUCCESS,
this.completedTask));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public CommandResult undo(Model model) throws CommandException {
oldTaskDescriptor);
model.setTask(taskToEdit,
previousEditTask);
this.canExecute = true;
return new CommandResult(String.format(MESSAGE_EDIT_TASK_SUCCESS,
taskToEdit));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.TaskCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.task.Task;
import seedu.address.model.task.filters.TaskFilter;

/**
* Purges all tasks currently in the task list
Expand All @@ -22,6 +25,9 @@ public class PurgeTaskCommand extends TaskCommand {
+ ": Purges all tasks in the displayed task list.\n"
+ "Example: " + FULL_COMMAND_WORD;

private TreeMap<Integer, Task> deletedTasks;
private ArrayList<TaskFilter> deletedFilters;

/**
* Executes the command and returns the result message.
*
Expand All @@ -38,9 +44,19 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_NO_TASK_TO_PURGE);
}

// Create a copy of the list and deletes all tasks from the original
Task[] tasksToDelete = taskList.toArray(new Task[0]);
model.deleteAllInFilteredTaskList(tasksToDelete);
// Create a copy of the list for undo with their respective indexes
deletedTasks = new TreeMap<>();
for (Task deletedTask : taskList) {
int index = model.indexOf(deletedTask);
deletedTasks.put(index, deletedTask);
}

deletedFilters = new ArrayList<>();
deletedFilters.addAll(model.getSelectedTaskFilters());

super.canExecute();

model.deleteAllInFilteredTaskList();
return new CommandResult(MESSAGE_SUCCESS);
}

Expand All @@ -54,4 +70,18 @@ public boolean equals(Object other) {
// instanceof handles nulls
return other instanceof PurgeTaskCommand;
}

@Override
public CommandResult undo(Model model) throws CommandException {
super.canUndo();

model.setTaskFilters(deletedFilters);
for (int index : deletedTasks.keySet()) {
Task task = deletedTasks.get(index);
model.insertTask(task, index);
}

return new CommandResult(MESSAGE_SUCCESS);

}
}
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ public interface Model {
void deleteTask(Task deletedTask);

/**
* Deletes a list of given tasks. The tasks must exist in the task list.
* @param tasksToDelete List of tasks to delete.
* Deletes the list of Filtered Tasks and their filters.
*/
void deleteAllInFilteredTaskList(Task... tasksToDelete);
void deleteAllInFilteredTaskList();

/**
* Adds the given task to the task list.
Expand All @@ -122,6 +121,13 @@ public interface Model {
*/
void insertTask(Task task, int index);

/**
* Checks the index of the given task in the task list.
* @param task Task to check the index of.
* @return The 0-based index of the task.
*/
int indexOf(Task task);

/** Returns an unmodifiable view of the task list */
ObservableList<Task> getFilteredTaskList();

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -192,6 +193,11 @@ public void addTask(Task task) {
recomputeAvailableTaskFilters();
}

@Override
public int indexOf(Task task) {
return taskList.indexOf(task);
}

@Override
public void insertTask(Task task, int index) {
taskList.addTaskAtIndex(task, index);
Expand Down Expand Up @@ -285,13 +291,13 @@ public void deleteTask(Task deletedTask) {
}

/**
* Deletes a list of given tasks.
* Deletes the filtered list and their filters from the task list.
* This method does not {@code updateTaskFilters} so as to show distinct changes to the task list, if any.
* Instead, it removes all currently selected task filters from {@code availableTaskFilters}
* @param tasksToDelete List of tasks in the filtered list to delete.
*/
@Override
public void deleteAllInFilteredTaskList(Task... tasksToDelete) {
public void deleteAllInFilteredTaskList() {
List<Task> tasksToDelete = new ArrayList<>(filteredTasks);
for (Task task : tasksToDelete) {
taskList.removeTask(task);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/model/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public void setTask(Task oldTask, Task newTask) {
tasks.set(tasks.indexOf(oldTask), newTask);
}

public int indexOf(Task task) {
return tasks.indexOf(task);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ public CommandHistory getCommandHistory() {
}

@Override
public void deleteAllInFilteredTaskList(Task... tasksToDelete) {
public void deleteAllInFilteredTaskList() {
throw new AssertionError("This method should not be called.");
}

@Override
public int indexOf(Task task) {
throw new AssertionError("This method should not be called.");
}
}
Expand Down

0 comments on commit efbbdba

Please sign in to comment.