Skip to content

Commit

Permalink
Merge pull request #340 from yixiann/finalise-ui-tags-2
Browse files Browse the repository at this point in the history
Add UI for tags
  • Loading branch information
RichDom2185 authored Oct 27, 2022
2 parents d81dd61 + fe981b7 commit 26e6a54
Show file tree
Hide file tree
Showing 27 changed files with 498 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,49 @@
import static java.util.Objects.requireNonNull;
import static seedu.foodrem.commons.enums.CommandType.FILTER_TAG_COMMAND;

import seedu.foodrem.commons.core.Messages;
import seedu.foodrem.logic.commands.Command;
import seedu.foodrem.logic.commands.CommandResult;
import seedu.foodrem.logic.commands.exceptions.CommandException;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.item.TagSetContainsTagPredicate;
import seedu.foodrem.model.tag.Tag;
import seedu.foodrem.viewmodels.FilterByTag;

/**
* Filters all items in FoodRem for items that contain the specified tag
*/
public class FilterTagCommand extends Command {
private final TagSetContainsTagPredicate pred;

private final Tag tag;

/**
* @param tag to filter the Item list for Items tagged with it
*/
public FilterTagCommand(Tag tag) {
requireNonNull(tag);
this.pred = new TagSetContainsTagPredicate(tag);
this.tag = tag;
}

@Override
public CommandResult<String> execute(Model model) throws CommandException {
public CommandResult<FilterByTag> execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasTag(pred.getTag())) {
if (!model.hasTag(tag)) {
throw new CommandException("This tag does not exist in the FoodRem");
}

model.updateFilteredItemList(pred);
return CommandResult.from(String.format(this.getSuccessMessage(),
model.getCurrentList().size()));
String primaryMessage = "Filtered by tag:";
String secondaryMessage = String.format("%s items filtered", model.getCurrentList().size());
return CommandResult.from(new FilterByTag(tag, primaryMessage, secondaryMessage));
}

public static String getUsage() {
return FILTER_TAG_COMMAND.getUsage();
}

protected String getSuccessMessage() {
return String.format("Filtered by tag: %s\n%s",
this.pred.getTag().getName(),
Messages.MESSAGE_ITEMS_FILTERED_OVERVIEW);
}

@Override
public boolean equals(Object other) {
return other == this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.foodrem.logic.commands.exceptions.CommandException;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.tag.Tag;
import seedu.foodrem.viewmodels.TagsWithMessage;

/**
* Deletes an existing tag in FoodRem.
Expand All @@ -17,22 +18,24 @@ public class DeleteTagCommand extends Command {

/**
* Creates an DeleteTagCommand to delete the specified {@code Tag}
*
* @param tagToDelete the tag that will be deleted from foodRem.
*/
public DeleteTagCommand(Tag tagToDelete) {
requireNonNull(tagToDelete);
toDelete = tagToDelete;
}

@Override
public CommandResult<String> execute(Model model) throws CommandException {
public CommandResult<TagsWithMessage> execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasTag(toDelete)) {
throw new CommandException("This tag does not exist in the FoodRem");
}

model.deleteTag(toDelete);
return CommandResult.from(String.format("Tag deleted: %1$s", toDelete));
return CommandResult.from(new TagsWithMessage("Tag deleted:", toDelete));
}

public static String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,23 @@
import static java.util.Objects.requireNonNull;
import static seedu.foodrem.commons.enums.CommandType.LIST_TAG_COMMAND;

import java.util.List;

import seedu.foodrem.logic.commands.Command;
import seedu.foodrem.logic.commands.CommandResult;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.tag.Tag;
import seedu.foodrem.viewmodels.TagsWithMessage;

/**
* Lists all the tags available
*/
public class ListTagCommand extends Command {
private static final String MESSAGE_SUCCESS = "Listed all tags:";

@Override
public CommandResult<String> execute(Model model) {
public CommandResult<TagsWithMessage> execute(Model model) {
requireNonNull(model);
model.updateFilteredTagList(Model.PREDICATE_SHOW_ALL_TAGS);
List<Tag> allTags = model.getFilteredTagList();

StringBuilder allTagsList = new StringBuilder("Listed all tags:\n");

for (Tag tag : allTags) {
allTagsList.append(tag.getName());
allTagsList.append(System.getProperty("line.separator"));
}

String tagList = allTagsList.toString();

return CommandResult.from(tagList);
return CommandResult.from(new TagsWithMessage(MESSAGE_SUCCESS, model.getFilteredTagList().toArray(Tag[]::new)));
}

public static String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.foodrem.logic.commands.exceptions.CommandException;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.tag.Tag;
import seedu.foodrem.viewmodels.TagsWithMessage;

/**
* Adds a tag to FoodRem.
Expand All @@ -16,23 +17,25 @@ public class NewTagCommand extends Command {
private final Tag toAdd;

/**
* Creates an AddTagCommand to add the specified {@code Tag}
* Creates a NewTagCommand to add the specified {@code Tag}
*
* @param tag the tag that will be added to foodRem.
*/
public NewTagCommand(Tag tag) {
requireNonNull(tag);
toAdd = tag;
}

@Override
public CommandResult<String> execute(Model model) throws CommandException {
public CommandResult<TagsWithMessage> execute(Model model) throws CommandException {
requireNonNull(model);

if (model.hasTag(toAdd)) {
throw new CommandException("This tag already exists in FoodRem");
}

model.addTag(toAdd);
return CommandResult.from(String.format("New tag added: %1$s", toAdd));
return CommandResult.from(new TagsWithMessage("New tag added:", toAdd));
}

public static String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.foodrem.logic.commands.exceptions.CommandException;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.tag.Tag;
import seedu.foodrem.viewmodels.TagToRename;

/**
* Renames an existing tag in FoodRem.
Expand All @@ -17,7 +18,10 @@ public class RenameTagCommand extends Command {
private final Tag renamedTag;

/**
* Creates an AddTagCommand to add the specified {@code Tag}
* Creates a RenameTagCommand to rename a specified {@code Tag}
*
* @param originalTag the tag to be renamed
* @param renamedTag the new tag
*/
public RenameTagCommand(Tag originalTag, Tag renamedTag) {
requireNonNull(originalTag);
Expand All @@ -27,7 +31,7 @@ public RenameTagCommand(Tag originalTag, Tag renamedTag) {
}

@Override
public CommandResult<String> execute(Model model) throws CommandException {
public CommandResult<TagToRename> execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.hasTag(originalTag)) {
Expand All @@ -39,7 +43,7 @@ public CommandResult<String> execute(Model model) throws CommandException {
}

model.setTag(originalTag, renamedTag);
return CommandResult.from(String.format("Original tag: %s\nRenamed tag: %s\n", originalTag, renamedTag));
return CommandResult.from(new TagToRename(originalTag, renamedTag, "Tag renamed:"));
}

public static String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.requireNonNull;
import static seedu.foodrem.commons.enums.CommandType.TAG_COMMAND;

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

import seedu.foodrem.commons.core.index.Index;
Expand All @@ -24,6 +23,9 @@ public class TagCommand extends Command {

/**
* Creates a TagCommand to tag the specified {@code Item} with a specified {@code Tag}
*
* @param tagName the name of the tag
* @param index the index of the item to tag
*/
public TagCommand(String tagName, Index index) {
requireNonNull(tagName);
Expand All @@ -34,33 +36,18 @@ public TagCommand(String tagName, Index index) {

@Override
public CommandResult<ItemWithMessage> execute(Model model) throws CommandException {
Item itemToTag = validateAndGetTargetItem(model, tag, index);
Item itemToTag = TagCommandUtil.validateAndGetItem(model, tag, index);

Set<Tag> itemTags = itemToTag.getTagSet();
if (itemTags.contains(tag)) {
throw new CommandException("This item has already been tagged with this tag");
}

itemTags.add(tag);
Item newTagSetItem = Item.createItemWithTags(itemToTag, itemTags);

model.setItem(itemToTag, newTagSetItem);

return CommandResult.from(
new ItemWithMessage(newTagSetItem, "Item tagged successfully. View updated item below:"));
}

static Item validateAndGetTargetItem(Model model, Tag tag, Index index) throws CommandException {
requireNonNull(model);

if (!model.hasTag(tag)) {
throw new CommandException("This tag does not exist");
}

List<Item> lastShownList = model.getCurrentList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException("The item index does not exist");
}

return lastShownList.get(index.getZeroBased());
return CommandResult.from(new ItemWithMessage(newTagSetItem, "Item tagged successfully. Updated item:"));
}

public static String getUsage() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package seedu.foodrem.logic.commands.tagcommands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.foodrem.commons.core.index.Index;
import seedu.foodrem.logic.commands.exceptions.CommandException;
import seedu.foodrem.model.Model;
import seedu.foodrem.model.item.Item;
import seedu.foodrem.model.tag.Tag;

/**
* Handles validation for tag commands
*/
public class TagCommandUtil {
private TagCommandUtil() {}

/**
* Checks if a model has a tag and if the index is valid before getting the item.
*
* @param model the current model
* @param tag the tag to be validated
* @param index the index of the item
*/
public static Item validateAndGetItem(Model model, Tag tag, Index index) throws CommandException {
requireNonNull(model);

if (!model.hasTag(tag)) {
throw new CommandException("This tag does not exist");
}

List<Item> lastShownList = model.getCurrentList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException("The item index does not exist");
}

return lastShownList.get(index.getZeroBased());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public class UntagCommand extends Command {
private final Tag tag;

/**
* Creates a TagCommand to tag the specified {@code Item} with a specified {@code Tag}
* Creates an UntagCommand to untag the specified {@code Item} with a specified {@code Tag}
*
* @param tagName the name of the tag
* @param index the index of the item to untag
*/
public UntagCommand(String tagName, Index index) {
requireNonNull(tagName);
Expand All @@ -33,17 +36,18 @@ public UntagCommand(String tagName, Index index) {

@Override
public CommandResult<ItemWithMessage> execute(Model model) throws CommandException {
Item itemToUntag = TagCommand.validateAndGetTargetItem(model, tag, index);
Item itemToUntag = TagCommandUtil.validateAndGetItem(model, tag, index);

Set<Tag> itemTags = itemToUntag.getTagSet();
if (!itemTags.contains(tag)) {
throw new CommandException("This item is not tagged with this tag");
throw new CommandException("This item has not been tagged with this tag");
}

itemTags.remove(tag);
Item newTagSetItem = Item.createItemWithTags(itemToUntag, itemTags);
model.setItem(itemToUntag, newTagSetItem);

return CommandResult.from(
new ItemWithMessage(newTagSetItem, "Item untagged successfully. View updated item below:"));
return CommandResult.from(new ItemWithMessage(newTagSetItem, "Item untagged successfully. Updated item:"));
}

public static String getUsage() {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/seedu/foodrem/viewmodels/FilterByTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package seedu.foodrem.viewmodels;

import java.util.Objects;

import seedu.foodrem.model.tag.Tag;

/**
* A view model for generating a view with tags as well as two messages.
* @author Tan Yi Xian
*/
public class FilterByTag {
private final Tag tag;
private final String primaryMessage;
private final String secondaryMessage;

/**
* Creates a view model containing the message and the tags to rename.
* @param tag the tag.
* @param primaryMessage the message to be displayed above the tags.
* @param secondaryMessage the message to displayed under the tags.
*/
public FilterByTag(Tag tag, String primaryMessage, String secondaryMessage) {
this.tag = tag;
this.primaryMessage = primaryMessage;
this.secondaryMessage = secondaryMessage;
}

public Tag getTag() {
return tag;
}

public String getPrimaryMessage() {
return primaryMessage;
}

public String getSecondaryMessage() {
return secondaryMessage;
}

@Override
public boolean equals(Object other) {
return this == other
|| (other instanceof FilterByTag
&& tag.equals(((FilterByTag) other).tag)
&& primaryMessage.equals(((FilterByTag) other).primaryMessage)
&& secondaryMessage.equals(((FilterByTag) other).secondaryMessage));
}

@Override
public int hashCode() {
return Objects.hash(tag, primaryMessage, secondaryMessage);
}
}
Loading

0 comments on commit 26e6a54

Please sign in to comment.