Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2425S1#202 from weijianwong/branch-re…
Browse files Browse the repository at this point in the history
…factor-ui

Refactor UI utility methods into a centralized UiUtil class
  • Loading branch information
luileng authored Nov 5, 2024
2 parents 5d8545d + 3857caa commit 8452746
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 117 deletions.
103 changes: 103 additions & 0 deletions src/main/java/tuteez/commons/util/UiUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tuteez.commons.util;

import java.util.Comparator;

import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import tuteez.model.person.Person;
import tuteez.model.person.TelegramUsername;

/**
* A container for Ui specific utility functions
*/
public class UiUtil {

/**
* Sets the name label text for the {@code Person}.
*
* @param nameLabel The label to update with the person's name.
* @param person The {@code Person} whose name is to be displayed.
*/
public static void setNameText(Label nameLabel, Person person) {
assert(person != null && nameLabel != null);
nameLabel.setText(person.getName().fullName);
}

/**
* Sets the phone label text for the {@code Person}.
*
* @param phoneLabel The label to update with the person's phone number.
* @param person The {@code Person} whose phone number is to be displayed.
*/
public static void setPhoneText(Label phoneLabel, Person person) {
assert(person != null && phoneLabel != null);
phoneLabel.setText(person.getPhone().value);
}

/**
* Sets the address label text if an address exists for the {@code Person}.
* Hides the address label if no address is present.
*
* @param addressLabel The label to update with the address.
* @param person The person whose address to display on the label.
*/
public static void setAddressText(Label addressLabel, Person person) {
assert(person != null && addressLabel != null);
if (person.getAddress().value != null) {
addressLabel.setText(person.getAddress().value);
addressLabel.setVisible(true);
} else {
addressLabel.setVisible(false);
}
}

/**
* Sets the Telegram username label text if a Telegram username exists for the {@code Person}.
* Hides the Telegram label if no username is present.
*
* @param telegramLabel The label to update with the Telegram username.
* @param person The person whose Telegram username to display on the label.
*/
public static void setTelegramUsernameText(Label telegramLabel, Person person) {
assert(person != null && telegramLabel != null);
TelegramUsername username = person.getTelegramUsername();
if (username != null && username.telegramUsername != null && !username.telegramUsername.isEmpty()) {
telegramLabel.setText("@" + username.telegramUsername);
telegramLabel.setVisible(true);
} else {
telegramLabel.setVisible(false);
}
}

/**
* Sets the email label text if an email exists for the {@code Person}.
* Hides the email label if no email is present.
*
* @param emailLabel The label to update with the email.
* @param person The {@code Person} whose email is to be displayed.
*/
public static void setEmailText(Label emailLabel, Person person) {
assert(person != null && emailLabel != null);
if (person.getEmail().value != null) {
emailLabel.setText(person.getEmail().value);
emailLabel.setVisible(true);
} else {
emailLabel.setVisible(false);
}
}

/**
* Sets the tags associated with the {@code Person} in the tags flow pane.
* Sorts the tags alphabetically for consistent ordering.
*
* @param tagsPane The flow pane to update with the tags.
* @param person The {@code Person} whose tags are to be displayed.
*/
public static void setTags(FlowPane tagsPane, Person person) {
assert(person != null && tagsPane != null);
tagsPane.getChildren().clear(); // Clear previous tags
person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tagsPane.getChildren().add(new Label(tag.tagName)));
}
}
53 changes: 7 additions & 46 deletions src/main/java/tuteez/ui/DisplayCard.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tuteez.ui;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;
Expand All @@ -10,8 +9,8 @@
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import tuteez.commons.util.UiUtil;
import tuteez.model.person.Person;
import tuteez.model.person.TelegramUsername;
import tuteez.model.person.lesson.Lesson;

/**
Expand All @@ -22,14 +21,9 @@ public class DisplayCard extends UiPart<Region> {
private static final String FXML = "DisplayCard.fxml";

public final Person person;

@FXML
private VBox cardPane;
@FXML
private Label name;
@FXML
private Label id;
@FXML
private Label phone;
@FXML
private Label telegram;
Expand All @@ -51,37 +45,17 @@ public DisplayCard(Optional<Person> lastViewedPerson) {
super(FXML);
this.person = lastViewedPerson.orElse(null);
if (lastViewedPerson.isPresent()) {
name.setText(person.getName().fullName);
phone.setText(person.getPhone().value);
setTelegramUsernameText(person);
address.setText(person.getAddress().value);
email.setText(person.getEmail().value);
setTags(person);
UiUtil.setNameText(name, person);
UiUtil.setPhoneText(phone, person);
UiUtil.setTelegramUsernameText(telegram, person);
UiUtil.setAddressText(address, person);
UiUtil.setEmailText(email, person);
UiUtil.setTags(displayTags, person);
setRemarks(person);
setDisplayLessons(person);
}
}

/**
* Sets the Telegram username text for the provided person.
*
* This method checks if the provided person has a valid, non-empty Telegram username.
* If a username is available, it updates the display text with the username prefixed by "@" and
* makes the Telegram field visible. If no valid username is found, the Telegram field is hidden.
*
* @param person the Person object whose Telegram username is to be displayed
*/
private void setTelegramUsernameText(Person person) {
assert(person != null);
TelegramUsername username = person.getTelegramUsername();
if (username != null && username.telegramUsername != null && !username.telegramUsername.isEmpty()) {
telegram.setText("@" + username.telegramUsername);
telegram.setVisible(true);
} else {
telegram.setVisible(false);
}
}

/**
* Sets the lessons for the provided person by sorting their lessons and displaying them in a numbered format.
*
Expand All @@ -106,19 +80,6 @@ private void setDisplayLessons(Person person) {
.forEach(label -> displayLessons.getChildren().add(label));
}

/**
* Sets the tags associated with the {@code Person} in the tags flow pane.
* Sorts the tags alphabetically for consistent ordering.
*
* @param person The {@code Person} whose tags are to be displayed.
*/
private void setTags(Person person) {
assert(person != null);
person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> displayTags.getChildren().add(new Label(tag.tagName)));
}

/**
* Sets the remarks associated with the {@code Person} in the remarks display pane.
* Each remark is displayed in a numbered format (e.g., "1. Remark text").
Expand Down
78 changes: 7 additions & 71 deletions src/main/java/tuteez/ui/PersonCard.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package tuteez.ui;

import java.util.Comparator;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Region;
import tuteez.commons.util.UiUtil;
import tuteez.model.person.Person;
import tuteez.model.person.TelegramUsername;
import tuteez.model.person.lesson.Lesson;

/**
Expand Down Expand Up @@ -50,64 +48,15 @@ public PersonCard(Person person, int displayedIndex) {
super(FXML);
this.person = person;
id.setText(displayedIndex + ". ");
name.setText(person.getName().fullName);
phone.setText(person.getPhone().value);
setTelegramUsernameText(person);
setAddressText(person);
setEmailText(person);
setTags(person);
UiUtil.setNameText(name, person);
UiUtil.setPhoneText(phone, person);
UiUtil.setTelegramUsernameText(telegram, person);
UiUtil.setAddressText(address, person);
UiUtil.setEmailText(email, person);
UiUtil.setTags(tags, person);
setNextLesson(person);
}

/**
* Sets the address label text if an address exists for the {@code Person}.
* Hides the address label if no address is present.
*
* @param person The {@code Person} whose address is to be displayed.
*/
private void setAddressText(Person person) {
assert(person != null);
if (person.getAddress().value != null) {
address.setText(person.getAddress().value);
address.setVisible(true);
} else {
address.setVisible(false);
}
}

/**
* Sets the email label text if an email exists for the {@code Person}.
* Hides the email label if no email is present.
*
* @param person The {@code Person} whose email is to be displayed.
*/
private void setEmailText(Person person) {
assert(person != null);
if (person.getEmail().value != null) {
email.setText(person.getEmail().value);
email.setVisible(true);
} else {
email.setVisible(false);
}
}

/**
* Sets the Telegram username label text if a Telegram username exists for the {@code Person}.
* Hides the Telegram label if no username is present.
*
* @param person The {@code Person} whose Telegram username is to be displayed.
*/
private void setTelegramUsernameText(Person person) {
assert(person != null);
TelegramUsername username = person.getTelegramUsername();
if (username != null && username.telegramUsername != null && !username.telegramUsername.isEmpty()) {
telegram.setText("@" + username.telegramUsername);
telegram.setVisible(true);
} else {
telegram.setVisible(false);
}
}

/**
* Displays the next lesson scheduled for the {@code Person}, based on the current time.
* If no lessons are scheduled, hides the lessons flow pane.
Expand All @@ -124,17 +73,4 @@ private void setNextLesson(Person person) {
nextLesson.setVisible(false);
}
}

/**
* Sets the tags associated with the {@code Person} in the tags flow pane.
* Sorts the tags alphabetically for consistent ordering.
*
* @param person The {@code Person} whose tags are to be displayed.
*/
private void setTags(Person person) {
assert(person != null);
person.getTags().stream()
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
}
}

0 comments on commit 8452746

Please sign in to comment.