Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Date semantics to Timestamp #89

Merged
merged 18 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
*/
public static String parseTitle(String title) {
requireNonNull(title);

return title.trim();
}

Expand All @@ -141,7 +140,6 @@ public static String parseTitle(String title) {
*/
public static String parseDescription(String description) {
requireNonNull(description);

return description.trim();
}

Expand All @@ -150,8 +148,12 @@ public static String parseDescription(String description) {
* @param timestamp The timestamp string to parse
* @return A parsed timestamp
*/
public static Timestamp parseTimestamp(String timestamp) {
public static Timestamp parseTimestamp(String timestamp) throws ParseException {
requireNonNull(timestamp);
String trimmedTS = timestamp.trim();
if (!Timestamp.isValidTimeStamp(trimmedTS)) {
throw new ParseException(Timestamp.MESSAGE_CONSTRAINTS);
}
return new Timestamp(timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public AddTaskCommand parse(String args) throws ParseException {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddTaskCommand.MESSAGE_USAGE));
}
String description = argMultimap.getValue(PREFIX_DESCRIPTION).orElse(null);
Timestamp timestamp = argMultimap.getValue(PREFIX_TIMESTAMP).map(ParserUtil::parseTimestamp).orElse(null);

Timestamp timestamp;
if (argMultimap.getAllValues(PREFIX_TIMESTAMP) == null) {
timestamp = null;
} else {
timestamp = ParserUtil.parseTimestamp(argMultimap.getValue(PREFIX_TIMESTAMP).get());
}
Set<Tag> tags = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));


return new AddTaskCommand(new Task(title, description, timestamp, tags));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public EditTaskCommand parse(String args) throws ParseException {
argMultimap.getValue(PREFIX_DESCRIPTION)
.map(ParserUtil::parseDescription)
.ifPresent(editTaskDescriptor::setDescription);
argMultimap.getValue(PREFIX_TIMESTAMP)
.map(ParserUtil::parseTimestamp)
.ifPresent(editTaskDescriptor::setTimestamp);

if (argMultimap.getValue(PREFIX_TIMESTAMP) != null) {
editTaskDescriptor.setTimestamp(ParserUtil.parseTimestamp(argMultimap.getValue(PREFIX_TIMESTAMP).get()));
}
parseTagsForEdit(argMultimap.getAllValues(PREFIX_TAG))
.ifPresent(editTaskDescriptor::setTags);

Expand Down
25 changes: 19 additions & 6 deletions src/main/java/seedu/address/model/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Task {
private final Timestamp timestamp;
private final Set<Tag> tags = new HashSet<>();
private final boolean isDone;
private boolean isOverdue = false;
wlren marked this conversation as resolved.
Show resolved Hide resolved

/**
* Creates a task with a given title, and optionally a description, timestamp and a set of tags.
Expand Down Expand Up @@ -49,11 +50,14 @@ public Task(String title, String description, Timestamp timestamp, Set<Tag> tags
this.timestamp = timestamp;
this.tags.addAll(tags);
this.isDone = isDone;
if (this.timestamp != null) {
this.isOverdue = updateIsOverdue();
}
}


public String getTitle() {
return title;
return this.title;
}

public Optional<String> getDescription() {
Expand All @@ -65,11 +69,19 @@ public Optional<Timestamp> getTimestamp() {
}

public Set<Tag> getTags() {
return tags;
return this.tags;
}

public boolean getIsDone() {
return isDone;
return this.isDone;
}

public boolean getIsOverdue() {
return this.isOverdue;
}

public boolean updateIsOverdue() {
return Timestamp.checkIsOverdue(this.timestamp);
wlren marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand All @@ -86,16 +98,17 @@ public boolean equals(Object o) {
&& Objects.equals(title, task.title)
&& Objects.equals(description, task.description)
&& Objects.equals(timestamp, task.timestamp)
&& Objects.equals(tags, task.tags);
&& Objects.equals(tags, task.tags)
&& Objects.equals(isOverdue, task.isOverdue);
}

@Override
public int hashCode() {
return Objects.hash(title, description, timestamp, tags, isDone);
return Objects.hash(title, description, timestamp, tags, isDone, isOverdue);
}

@Override
public String toString() {
return title;
return this.title;
}
}
39 changes: 38 additions & 1 deletion src/main/java/seedu/address/model/task/Timestamp.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package seedu.address.model.task;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.AppUtil.checkArgument;

import java.time.LocalDate;
import java.util.Arrays;


/**
* Represents a timestamp for a task.
*/
public class Timestamp {
public static final String MESSAGE_CONSTRAINTS = "Timestamp should be in YYYY-MM-DD format";
private static final String VALIDATION_REGEX =
wlren marked this conversation as resolved.
Show resolved Hide resolved
"^((2000|2400|2800|(19|2[0-9](0[48]|[2468][048]|[13579][26])))-02-29)$"
+ "|^(((19|2[0-9])[0-9]{2})-02-(0[1-9]|1[0-9]|2[0-8]))$"
+ "|^(((19|2[0-9])[0-9]{2})-(0[13578]|10|12)-(0[1-9]|[12][0-9]|3[01]))$"
+ "|^(((19|2[0-9])[0-9]{2})-(0[469]|11)-(0[1-9]|[12][0-9]|30))$";

private final String timestamp;

/**
* Creates a TimeStamp with the given string.
* @param timestamp the string representing the timestamp
*/
public Timestamp(String timestamp) {
requireNonNull(timestamp);
checkArgument(isValidTimeStamp(timestamp), MESSAGE_CONSTRAINTS);
this.timestamp = timestamp;
}

Expand All @@ -31,5 +47,26 @@ public String toString() {
return this.timestamp;
}

// TODO: Create a DateTime representation of timestamps
private static LocalDate getToday() {

return LocalDate.now();
}

public static boolean isValidTimeStamp(String test) {
wlren marked this conversation as resolved.
Show resolved Hide resolved
return test.matches(VALIDATION_REGEX);
}

/**
* Checks if the particular task is overdue.
*
* @param time the Timestamp to check against the current date
* @return boolean
*/
public static boolean checkIsOverdue(Timestamp time) {
wlren marked this conversation as resolved.
Show resolved Hide resolved
int[] timeArray = Arrays.stream(time.toString().split("-")).mapToInt(Integer::parseInt).toArray();
wlren marked this conversation as resolved.
Show resolved Hide resolved
LocalDate timestamp = LocalDate.of(timeArray[0], timeArray[1], timeArray[2]);

return getToday().isAfter(timestamp);
}

}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public Task toModelType() throws IllegalValueException {
if (timestamp.equals("null")) {
modelTimeStamp = null;
} else {
if (!Timestamp.isValidTimeStamp(timestamp)) {
throw new IllegalValueException(Timestamp.MESSAGE_CONSTRAINTS);
}
modelTimeStamp = new Timestamp(timestamp);
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/seedu/address/ui/TaskCard.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package seedu.address.ui;

import java.util.Comparator;
import java.util.Optional;
import java.util.logging.Logger;

import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.task.Task;
import seedu.address.ui.exceptions.GuiException;

public class TaskCard extends UiPart<Region> {
private static final String FXML = "TaskCard.fxml";
private static final Logger logger = LogsCenter.getLogger(UiManager.class);

@FXML
private Label name;
Expand Down Expand Up @@ -68,7 +71,11 @@ public TaskCard(Task task, int oneIndex, TaskListPanel.TaskEditor taskEditor) {
timestamp.setManaged(false);
} else {
timestamp.setText(
Optional.ofNullable(task.getTimestamp()).map(ts -> "\uD83D\uDD52 " + ts.toString()).orElse(""));
task.getTimestamp().map(ts -> "\uD83D\uDD52 " + ts.toString()).orElse(""));
if (task.getIsOverdue()) {
logger.info(Boolean.toString(task.getIsOverdue()));
timestamp.setTextFill(Color.color(1.0, 0.0, 0.0));
}
}

isCompleted.setText("");
Expand Down