Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103-F09-1#340 from wuzengfu/refactor-code
Browse files Browse the repository at this point in the history
Refactor code and add test cases for PaginationPanel.java
  • Loading branch information
WinstonJin authored Nov 11, 2024
2 parents 546fba8 + e4b5cf4 commit cd6be1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 2 additions & 8 deletions src/main/java/seedu/address/model/contact/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Role(String roleName) {
public static boolean isValidRoleName(String test) {
requireNonNull(test);
return Arrays.stream(AVAILABLE_ROLES)
.map(role -> role.toLowerCase())
.map(String::toLowerCase)
.anyMatch(role -> role.equals(test.trim().toLowerCase()));
}

Expand All @@ -82,13 +82,7 @@ private String toOfficialCase(String input) {
}

private int assignRoleIndex(String roleName) {
assert roleName.equals(PRESIDENT) // must be case sensitive
|| roleName.equals(VICE_PRESIDENT)
|| roleName.equals(ADMIN)
|| roleName.equals(MARKETING)
|| roleName.equals(EVENTS_INTERNAL)
|| roleName.equals(EVENTS_EXTERNAL)
|| roleName.equals(EXTERNAL_RELATIONS);
assert Arrays.asList(AVAILABLE_ROLES).contains(roleName);

int errorStatus = -1;

Expand Down
18 changes: 14 additions & 4 deletions src/main/java/seedu/address/ui/PaginationPanel.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package seedu.address.ui;

import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.logging.Logger;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.Pagination;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.contact.Contact;

/**
Expand All @@ -15,6 +19,7 @@
public class PaginationPanel extends Pagination {
public static final int ROWS_PER_PAGE = 10;
private static int currentPageIndex = 0;
private static final Logger logger = LogsCenter.getLogger(PaginationPanel.class);
private final ObservableList<Contact> contactList;

/**
Expand All @@ -23,8 +28,9 @@ public class PaginationPanel extends Pagination {
*/
public PaginationPanel(ObservableList<Contact> contactList) {
super();
requireNonNull(contactList);
this.contactList = contactList;
this.contactList.addListener(this::onListItemsChanged); // enables UI update to ContactPagination
this.contactList.addListener(this::onListItemsChanged);
this.initPagination();
}

Expand All @@ -34,18 +40,21 @@ public PaginationPanel(ObservableList<Contact> contactList) {
* @return The index of the item in {@code contactList}.
*/
public static int convertItemIndexToDisplayIndex(int itemIndex) {
return (currentPageIndex) * ROWS_PER_PAGE + itemIndex + 1;
assert itemIndex >= 0;
return currentPageIndex * ROWS_PER_PAGE + itemIndex + 1;
}

private void initPagination() {
final int minPageCount = 1;
int pageCount = Math.max((int) Math.ceil((double) contactList.size() / ROWS_PER_PAGE), minPageCount);
int expectedPageCount = (int) Math.ceil((double) contactList.size() / ROWS_PER_PAGE);
int pageCount = Math.max(expectedPageCount, minPageCount);
this.setPageCount(pageCount);
this.setStyle("-fx-page-information-alignment: bottom;");
this.setPageFactory(this::createPage);
logger.info("A pagination which contains " + pageCount + " page(s) has been created.");
}

private Node createPage(int pageIndex) {
assert pageIndex >= 0;
PaginationPanel.currentPageIndex = pageIndex;
int fromIndex = pageIndex * PaginationPanel.ROWS_PER_PAGE;
int endIndex = Math.min(fromIndex + PaginationPanel.ROWS_PER_PAGE, contactList.size());
Expand All @@ -59,5 +68,6 @@ private Node createPage(int pageIndex) {

private void onListItemsChanged(ListChangeListener.Change<? extends Contact> unused) {
this.initPagination();
logger.info("Re-render pagination due to the change of list items.");
}
}

0 comments on commit cd6be1f

Please sign in to comment.