Skip to content

Commit

Permalink
Implement unpin command
Browse files Browse the repository at this point in the history
  • Loading branch information
myteo committed Nov 4, 2016
1 parent b267787 commit af2cf5a
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 123 deletions.
19 changes: 15 additions & 4 deletions src/main/java/seedu/dailyplanner/logic/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ public Command parseCommand(String userInput) {

case UndoCommand.COMMAND_WORD:
return new UndoCommand();

case PinCommand.COMMAND_WORD:
return preparePin(arguments);
return preparePin(arguments);

case UnpinCommand.COMMAND_WORD:
return prepareUnpin(arguments);

case ShowCommand.COMMAND_WORD:
if (arguments.equals(""))
Expand All @@ -109,9 +112,17 @@ public Command parseCommand(String userInput) {
}
}

private Command prepareUnpin(String arguments) {
Optional<Integer> index = parseIndex(arguments);
if (!index.isPresent()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CompleteCommand.MESSAGE_USAGE));
}

return new UnpinCommand(index.get());
}

private Command preparePin(String arguments) {
// TODO Auto-generated method stub
Optional<Integer> index = parseIndex(arguments);
Optional<Integer> index = parseIndex(arguments);
if (!index.isPresent()) {
return new IncorrectCommand(String.format(MESSAGE_INVALID_COMMAND_FORMAT, CompleteCommand.MESSAGE_USAGE));
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/seedu/dailyplanner/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public void markTaskAsComplete(int targetIndex) throws UniqueTaskList.PersonNotF
public void pinTask(int targetIndex) throws PersonNotFoundException {
persons.pin(targetIndex);
}

public void unpinTask(int targetIndex) {
persons.unpin(targetIndex);

}
//// tag-level operations

public void addTag(Tag t) throws UniqueTagList.DuplicateTagException {
Expand Down Expand Up @@ -172,4 +177,5 @@ public int hashCode() {
// use this method for custom fields hashing instead of implementing your own
return Objects.hash(persons, tags);
}
}

}
4 changes: 4 additions & 0 deletions src/main/java/seedu/dailyplanner/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public interface Model {

/** Pins the given task. */
void pinTask(int targetIndex) throws PersonNotFoundException;

/** Unpins the given task. */
void unpinTask(int i) throws PersonNotFoundException;

/** Returns the filtered person list as an {@code UnmodifiableObservableList<ReadOnlyPerson>} */
UnmodifiableObservableList<ReadOnlyTask> getFilteredPersonList();
Expand All @@ -55,4 +58,5 @@ public interface Model {
/** Updates the filter of the filtered person list to show only completed tasks*/
void updateFilteredPersonListByCompletion(Set<String> keywords);


}
7 changes: 6 additions & 1 deletion src/main/java/seedu/dailyplanner/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ public synchronized void markTaskAsComplete(int targetIndex) throws PersonNotFou
public void pinTask(int targetIndex) throws PersonNotFoundException {
addressBook.pinTask(targetIndex);
indicateAddressBookChanged();

}

@Override
public void unpinTask(int targetIndex) throws PersonNotFoundException {
addressBook.unpinTask(targetIndex);
indicateAddressBookChanged();
}

// =========== Filtered Person List Accessors
Expand Down
242 changes: 125 additions & 117 deletions src/main/java/seedu/dailyplanner/model/task/UniqueTaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,124 +18,132 @@
*/
public class UniqueTaskList implements Iterable<Task> {

/**
* Signals that an operation would have violated the 'no duplicates'
* property of the list.
*/
public static class DuplicatePersonException extends DuplicateDataException {
protected DuplicatePersonException() {
super("Operation would result in duplicate persons");
/**
* Signals that an operation would have violated the 'no duplicates'
* property of the list.
*/
public static class DuplicatePersonException extends DuplicateDataException {
protected DuplicatePersonException() {
super("Operation would result in duplicate persons");
}
}
}

/**
* Signals that an operation targeting a specified person in the list would
* fail because there is no such matching person in the list.
*/
public static class PersonNotFoundException extends Exception {
}

private final ObservableList<Task> internalList = FXCollections.observableArrayList();
private final ObservableList<Task> pinnedList = FXCollections.observableArrayList();

/**
* Constructs empty PersonList.
*/
public UniqueTaskList() {
}

/**
* Returns true if the list contains an equivalent person as the given
* argument.
*/
public boolean contains(ReadOnlyTask toCheck) {
assert toCheck != null;
return internalList.contains(toCheck);
}

/**
* Adds a person to the list.
*
* @throws DuplicatePersonException
* if the person to add is a duplicate of an existing person in
* the list.
*/
public void add(Task toAdd) throws DuplicatePersonException {
assert toAdd != null;
if (contains(toAdd)) {
throw new DuplicatePersonException();

/**
* Signals that an operation targeting a specified person in the list would
* fail because there is no such matching person in the list.
*/
public static class PersonNotFoundException extends Exception {
}

private final ObservableList<Task> internalList = FXCollections.observableArrayList();
private final ObservableList<Task> pinnedList = FXCollections.observableArrayList();

/**
* Constructs empty PersonList.
*/
public UniqueTaskList() {
}

/**
* Returns true if the list contains an equivalent person as the given
* argument.
*/
public boolean contains(ReadOnlyTask toCheck) {
assert toCheck != null;
return internalList.contains(toCheck);
}

/**
* Adds a person to the list.
*
* @throws DuplicatePersonException
* if the person to add is a duplicate of an existing person in
* the list.
*/
public void add(Task toAdd) throws DuplicatePersonException {
assert toAdd != null;
if (contains(toAdd)) {
throw new DuplicatePersonException();
}
internalList.add(toAdd);
FXCollections.sort(internalList);
}

/**
* Removes the equivalent person from the list.
*
* @throws PersonNotFoundException
* if no such person could be found in the list.
*/
public boolean remove(ReadOnlyTask toRemove) throws PersonNotFoundException {
assert toRemove != null;
final boolean personFoundAndDeleted = internalList.remove(toRemove);

if (!personFoundAndDeleted) {
throw new PersonNotFoundException();
}
FXCollections.sort(internalList);
return personFoundAndDeleted;
}
internalList.add(toAdd);
FXCollections.sort(internalList);
}

/**
* Removes the equivalent person from the list.
*
* @throws PersonNotFoundException
* if no such person could be found in the list.
*/
public boolean remove(ReadOnlyTask toRemove) throws PersonNotFoundException {
assert toRemove != null;
final boolean personFoundAndDeleted = internalList.remove(toRemove);

if (!personFoundAndDeleted) {
throw new PersonNotFoundException();

/**
* Marks the task indicated by taskIndex as complete
*
* @throws PersonNotFoundException
* if the task index is invalid.
*/
public void complete(int taskIndex) throws PersonNotFoundException {

final Task completedTask = internalList.get(taskIndex);
completedTask.markAsComplete();
internalList.set(taskIndex, completedTask);
FXCollections.sort(internalList);
}
FXCollections.sort(internalList);
return personFoundAndDeleted;
}


/**
* Marks the task indicated by taskIndex as complete
*
* @throws PersonNotFoundException
* if the task index is invalid.
*/
public void complete(int taskIndex) throws PersonNotFoundException {

final Task completedTask = internalList.get(taskIndex);
completedTask.markAsComplete();
internalList.set(taskIndex, completedTask);
FXCollections.sort(internalList);
}

/**
* Pins the task indicated by taskIndex
*
* @return
*/
public void pin(int taskIndex) throws PersonNotFoundException {

final Task completedTask = internalList.get(taskIndex);
completedTask.pin();
internalList.set(taskIndex, completedTask);
pinnedList.add(completedTask);
}

public ObservableList<Task> getInternalList() {
return internalList;
}

public ObservableList<Task> getInternalPinnedList() {
return pinnedList;
}

@Override
public Iterator<Task> iterator() {
return internalList.iterator();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof UniqueTaskList // instanceof handles nulls
&& this.internalList.equals(((UniqueTaskList) other).internalList));
}

@Override
public int hashCode() {
return internalList.hashCode();
}

/**
* Pins the task indicated by taskIndex
*
* @return
*/
public void pin(int taskIndex) throws PersonNotFoundException {

final Task taskToPin = internalList.get(taskIndex);
taskToPin.pin();
internalList.set(taskIndex, taskToPin);
pinnedList.add(taskToPin);
}

public void unpin(int targetIndex) {
final Task taskToUnpin = internalList.get(targetIndex);
taskToUnpin.unpin();
internalList.set(targetIndex, taskToUnpin);
pinnedList.remove(taskToUnpin);

}

public ObservableList<Task> getInternalList() {
return internalList;
}

public ObservableList<Task> getInternalPinnedList() {
return pinnedList;
}

@Override
public Iterator<Task> iterator() {
return internalList.iterator();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof UniqueTaskList // instanceof handles nulls
&& this.internalList.equals(((UniqueTaskList) other).internalList));
}

@Override
public int hashCode() {
return internalList.hashCode();
}

}

0 comments on commit af2cf5a

Please sign in to comment.