Skip to content

Commit

Permalink
Update mark unmark task commands
Browse files Browse the repository at this point in the history
Mark and unmark task commands do not change the gui immediately after being executed.

Updating the commands gets rid of this bug.

Let's update mark and unmark task commands.
  • Loading branch information
feliciagan committed Oct 13, 2022
1 parent 4f88b02 commit 5f9c6ef
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 168 deletions.
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditTaskDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import seedu.address.commons.util.CollectionUtil;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Deadline;
import seedu.address.model.task.Description;

/**
Expand All @@ -15,6 +16,7 @@
*/
public class EditTaskDescriptor {
private Description description;
private Deadline deadline;
private boolean isDone;
private Set<Tag> tags;

Expand All @@ -26,6 +28,7 @@ public EditTaskDescriptor() {}
*/
public EditTaskDescriptor(EditTaskDescriptor toCopy) {
setDescription(toCopy.description);
setDeadline(toCopy.deadline);
setIsDone(toCopy.isDone);
setTags(toCopy.tags);
}
Expand All @@ -45,6 +48,14 @@ public Optional<Description> getDescription() {
return Optional.ofNullable(description);
}

public void setDeadline(Deadline deadline) {
this.deadline = deadline;
}

public Optional<Deadline> getDeadline() {
return Optional.ofNullable(deadline);
}

public void setIsDone(boolean isDone) {
this.isDone = isDone;
}
Expand Down Expand Up @@ -86,6 +97,7 @@ public boolean equals(Object other) {
EditTaskDescriptor e = (EditTaskDescriptor) other;

return getDescription().equals(e.getDescription())
&& getDeadline().equals(e.getDeadline())
&& getIsDone().equals(e.getIsDone())
&& getTags().equals(e.getTags());
}
Expand Down
113 changes: 10 additions & 103 deletions src/main/java/seedu/address/logic/commands/task/EditTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TASK_DESCRIPTION;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.EditTaskDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.tag.Tag;
Expand Down Expand Up @@ -42,18 +39,19 @@ public class EditTaskCommand extends Command {
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task list.";

private final Index index;
private final EditTaskCommand.EditTaskDescriptor editTaskDescriptor;
private final EditTaskDescriptor editTaskDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param editTaskDescriptor details to edit the person with
* Creates an EditTaskCommand object.
* @param index of the task in the filtered task list to edit.
* @param editTaskDescriptor details to edit the task with.
*/
public EditTaskCommand(Index index, EditTaskCommand.EditTaskDescriptor editTaskDescriptor) {
public EditTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) {
requireNonNull(index);
requireNonNull(editTaskDescriptor);

this.index = index;
this.editTaskDescriptor = new EditTaskCommand.EditTaskDescriptor(editTaskDescriptor);
this.editTaskDescriptor = new EditTaskDescriptor(editTaskDescriptor);
}

@Override
Expand All @@ -62,7 +60,7 @@ public CommandResult execute(Model model) throws CommandException {
List<Task> lastShownList = model.getFilteredTaskList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Task taskToEdit = lastShownList.get(index.getZeroBased());
Expand All @@ -81,12 +79,12 @@ public CommandResult execute(Model model) throws CommandException {
* Creates and returns a {@code Task} with the details of {@code taskToEdit}
* edited with {@code editTaskDescriptor}.
*/
private static Task createEditedTask(Task taskToEdit, EditTaskCommand.EditTaskDescriptor editTaskDescriptor) {
private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTaskDescriptor) {
assert taskToEdit != null;

Description updatedDescription = editTaskDescriptor.getDescription().orElse(taskToEdit.getDescription());
Deadline updatedDeadline = editTaskDescriptor.getDeadline().orElse(taskToEdit.getDeadline());
Boolean updatedIsDone = editTaskDescriptor.getStatus().orElse(taskToEdit.getStatus());
Boolean updatedIsDone = editTaskDescriptor.getIsDone().orElse(taskToEdit.getStatus());
Set<Tag> updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags());

return new Task(updatedDescription, updatedDeadline, updatedIsDone, updatedTags);
Expand All @@ -109,95 +107,4 @@ public boolean equals(Object other) {
return index.equals(e.index)
&& editTaskDescriptor.equals(e.editTaskDescriptor);
}

/**
* Stores the details to edit the task with. Each non-empty field value will replace the
* corresponding field value of the person.
*/
public static class EditTaskDescriptor {
private Description description;
private Deadline deadline;
private Boolean isDone;
private Set<Tag> tags;

public EditTaskDescriptor() {}

/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditTaskDescriptor(EditTaskCommand.EditTaskDescriptor toCopy) {
setDescription(toCopy.description);
setDeadline(toCopy.deadline);
setStatus(toCopy.isDone);
setTags(toCopy.tags);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(description, tags);
}

public void setDescription(Description description) {
this.description = description;
}

public Optional<Description> getDescription() {
return Optional.ofNullable(description);
}

public void setDeadline(Deadline deadline) {
this.deadline = deadline;
}

public Optional<Deadline> getDeadline() {
return Optional.ofNullable(deadline);
}

public void setStatus(Boolean isDone) {
this.isDone = isDone;
}

public Optional<Boolean> getStatus() {
return Optional.ofNullable(isDone);
}

/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}

/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditTaskCommand.EditTaskDescriptor)) {
return false;
}

// state check
EditTaskCommand.EditTaskDescriptor e = (EditTaskCommand.EditTaskDescriptor) other;

return getDescription().equals(e.getDescription())
&& getTags().equals(e.getTags());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package seedu.address.logic.commands.task;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_TASKS;

import java.util.List;
import java.util.Set;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.EditTaskDescriptor;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.tag.Tag;
import seedu.address.model.task.Deadline;
import seedu.address.model.task.Description;
import seedu.address.model.task.Task;

/**
Expand All @@ -24,32 +30,76 @@ public class MarkTaskCommand extends Command {
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_MARK_TEST_SUCCESS = "Marked Task: %1$s";
public static final String MESSAGE_MARK_TASK_SUCCESS = "Marked Task: %1$s";
public static final String MESSAGE_DUPLICATE_TASK = "This task already exists in the task list.";

private final Index targetIndex;
private final Index index;
private final EditTaskDescriptor editTaskDescriptor;

public MarkTaskCommand(Index targetIndex) {
this.targetIndex = targetIndex;
/**
* Creates an MarkTaskCommand object.
* @param index of the task in the filtered task list to edit.
* @param editTaskDescriptor details to edit the task with.
*/
public MarkTaskCommand(Index index, EditTaskDescriptor editTaskDescriptor) {
requireNonNull(index);
requireNonNull(editTaskDescriptor);

this.index = index;
this.editTaskDescriptor = editTaskDescriptor;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Task> lastShownList = model.getFilteredTaskList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_TASK_DISPLAYED_INDEX);
}

Task taskToMark = lastShownList.get(targetIndex.getZeroBased());
taskToMark.markTask();
return new CommandResult(String.format(MESSAGE_MARK_TEST_SUCCESS, taskToMark));
Task taskToMark = lastShownList.get(index.getZeroBased());
Task markedTask = createEditedTask(taskToMark, editTaskDescriptor);

if (!taskToMark.isSameTask(markedTask) && model.hasTask(markedTask)) {
throw new CommandException(MESSAGE_DUPLICATE_TASK);
}

model.setTask(taskToMark, markedTask);
model.updateFilteredTaskList(PREDICATE_SHOW_ALL_TASKS);
return new CommandResult(String.format(MESSAGE_MARK_TASK_SUCCESS, markedTask));
}

/**
* Creates and returns a {@code Task} with the details of {@code taskToEdit}
* edited with {@code editTaskDescriptor}.
*/
private static Task createEditedTask(Task taskToEdit, EditTaskDescriptor editTaskDescriptor) {
assert taskToEdit != null;

Description updatedDescription = editTaskDescriptor.getDescription().orElse(taskToEdit.getDescription());
Deadline updatedDeadline = editTaskDescriptor.getDeadline().orElse(taskToEdit.getDeadline());
Boolean updatedIsDone = editTaskDescriptor.getIsDone().orElse(taskToEdit.getStatus());
Set<Tag> updatedTags = editTaskDescriptor.getTags().orElse(taskToEdit.getTags());

return new Task(updatedDescription, updatedDeadline, updatedIsDone, updatedTags);
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof MarkTaskCommand
&& targetIndex.equals(((MarkTaskCommand) other).targetIndex));
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof MarkTaskCommand)) {
return false;
}

// state check
MarkTaskCommand m = (MarkTaskCommand) other;
return index.equals(m.index)
&& editTaskDescriptor.equals(m.editTaskDescriptor);
}
}
Loading

0 comments on commit 5f9c6ef

Please sign in to comment.