Skip to content

Commit

Permalink
Add function to remember old dividers positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluiexo committed Apr 4, 2024
1 parent db111c5 commit f58aa1c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
27 changes: 20 additions & 7 deletions src/main/java/staffconnect/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public MainWindow(Stage primaryStage, Logic logic) {

// Configure the UI
setWindowDefaultSize(logic.getGuiSettings());

// The default divider is 0.43
personOnDisplay = logic.getFirstPersonIfExist()
.map(PersonCard::new).orElse(new PersonCard());
.map(person -> new PersonCard(person, 0.43)).orElse(new PersonCard());

Check warning on line 64 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L64

Added line #L64 was not covered by tests

helpWindow = new HelpWindow();
}
Expand All @@ -87,7 +87,8 @@ public Stage getPrimaryStage() {
*/
void fillInnerParts() {

personListPanel = new PersonListPanel(logic.getFilteredPersonList(), this::changePersonCard);
personListPanel =
new PersonListPanel(logic.getFilteredPersonList(), this::changePersonCard, this::getDividerPosition);

Check warning on line 91 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L90-L91

Added lines #L90 - L91 were not covered by tests
personListPanel.setListSelectedIndex(0);
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

Expand All @@ -104,7 +105,6 @@ void fillInnerParts() {
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());



}

private void changePersonCard(PersonCard personToUpdate) {
Expand All @@ -115,6 +115,15 @@ private void changePersonCard(PersonCard personToUpdate) {

}

/**
* Gets the current divider position, to be used by the UI only
*
* @return a double value of the divider position.
*/
public double getDividerPosition() {
return personOnDisplay.getCurrentDividerPosition();

Check warning on line 124 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L124

Added line #L124 was not covered by tests
}

/**
* Executes the command and returns the result.
*
Expand All @@ -137,9 +146,12 @@ private CommandResult executeCommand(String commandText) throws CommandException
}

if (commandResult.hasPersonAndIndex()) {

double dividerPosition = personOnDisplay.getCurrentDividerPosition();
int index = commandResult.getIndex();
commandResult.getPersonToDisplay()
.ifPresentOrElse(person -> reloadPersonCardWithPerson(new PersonCard(person), index),
.ifPresentOrElse(
person -> reloadPersonCardWithPerson(new PersonCard(person, dividerPosition), index),

Check warning on line 154 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L150-L154

Added lines #L150 - L154 were not covered by tests
this::reloadPersonCardWithRoot);
} else {
reloadPersonCardWithRoot();

Check warning on line 157 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L156-L157

Added lines #L156 - L157 were not covered by tests
Expand Down Expand Up @@ -177,8 +189,9 @@ public void handleHelp() {
}

private void reloadPersonCardWithRoot() {

personOnDisplay = logic.getFirstPersonIfExist().map(PersonCard::new).orElse(new PersonCard());
double previousDividerPosition = personOnDisplay.getCurrentDividerPosition();
personOnDisplay = logic.getFirstPersonIfExist()
.map(person -> new PersonCard(person, previousDividerPosition)).orElse(new PersonCard());

Check warning on line 194 in src/main/java/staffconnect/ui/MainWindow.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/MainWindow.java#L192-L194

Added lines #L192 - L194 were not covered by tests

personCardPanelPlaceholder.getChildren().clear();
personCardPanelPlaceholder.getChildren().add(personOnDisplay.getRoot());
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/staffconnect/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public PersonCard() {
/**
* Creates a {@code PersonCard} with the given {@code Person} and index to display.
*/
public PersonCard(Person person) {
public PersonCard(Person person, double previousDivider) {
super(FXML);
this.person = person;

Expand All @@ -112,6 +112,9 @@ public PersonCard(Person person) {
module.setText("Module: " + person.getModule().value);
email.setText("Email: " + person.getEmail().value);

// Set the previous divider position from the old index
splitDisplay.setDividerPosition(0, previousDivider);

Check warning on line 116 in src/main/java/staffconnect/ui/PersonCard.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/PersonCard.java#L116

Added line #L116 was not covered by tests

computePixelHeight(); //Set up the pixel height variables

Check warning on line 118 in src/main/java/staffconnect/ui/PersonCard.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/PersonCard.java#L118

Added line #L118 was not covered by tests

person.getTags().stream()
Expand Down Expand Up @@ -218,6 +221,15 @@ private double getLongestWidth(List<Meeting> meetingList) {
}
}

/**
* Gets the current divider position for usage in the UI.
* @return a double value of the current divider position.
*/
public double getCurrentDividerPosition() {
double[] positions = splitDisplay.getDividerPositions();
return positions[0];

Check warning on line 230 in src/main/java/staffconnect/ui/PersonCard.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/PersonCard.java#L229-L230

Added lines #L229 - L230 were not covered by tests
}

/**
* Custom {@code ListCell} that displays the graphics of a {@code Meetings} using a {@code MeetingsCard}.
*/
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/staffconnect/ui/PersonListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ public class PersonListPanel extends UiPart<Region> {
/**
* Creates a {@code PersonListPanel} with the given {@code ObservableList}.
*/
public PersonListPanel(ObservableList<Person> personList, PersonDisplay personDisplay) {
public PersonListPanel(ObservableList<Person> personList, PersonDisplay personDisplay,
DividerPosition dividerPosition) {
super(FXML);
personListView.setItems(personList);
personListView.setCellFactory(listView -> new NameListViewCell());
personListView.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
personDisplay.changePersonCard(new PersonCard(personListView.getSelectionModel().getSelectedItem()));
personDisplay.changePersonCard(new PersonCard(personListView.getSelectionModel().getSelectedItem(),
dividerPosition.getDividerPosition()));

Check warning on line 35 in src/main/java/staffconnect/ui/PersonListPanel.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/PersonListPanel.java#L34-L35

Added lines #L34 - L35 were not covered by tests
}
});
personListView.setOnMouseClicked(event -> {
personDisplay.changePersonCard(new PersonCard(personListView.getSelectionModel().getSelectedItem()));
personDisplay.changePersonCard(new PersonCard(personListView.getSelectionModel().getSelectedItem(),
dividerPosition.getDividerPosition()));

Check warning on line 40 in src/main/java/staffconnect/ui/PersonListPanel.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/staffconnect/ui/PersonListPanel.java#L39-L40

Added lines #L39 - L40 were not covered by tests
});
}

Expand All @@ -51,6 +54,18 @@ public void setListSelectedIndex(int index) {

}

/**
* Represents a function that gets the previous divider position.
*/
@FunctionalInterface
public interface DividerPosition {

/**
* Gets the current divider position
*/
double getDividerPosition();
}

/**
* Represents a function that can change PersonCard.
*/
Expand Down

0 comments on commit f58aa1c

Please sign in to comment.