Skip to content

Commit

Permalink
#179 - Only load user data when really needed
Browse files Browse the repository at this point in the history
  • Loading branch information
hohonuuli committed Jul 2, 2024
1 parent 6f34578 commit 18ba08a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion org.mbari.vars.ui/src/main/java/org/mbari/vars/ui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ else if (Initializer.getSettingsDirectory() == null) {
Platform.runLater(() -> {
primaryStage.setWidth(width + 1);
});
// Let the UI thread catch up and redraw
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Expand All @@ -130,7 +131,6 @@ else if (Initializer.getSettingsDirectory() == null) {
primaryStage.setWidth(width);
});
});

});

primaryStage.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,20 @@ public PopOver getOpenPopOver() {
return openPopOver;
}

private void setUser(String username) {
if (username != null) {
toolBox.getServices()
.getUserService()
.findAllUsers()
.thenAccept(users -> {
Optional<User> opt = users.stream()
.filter(u -> u.getUsername().equals(username))
.findFirst();
opt.ifPresent(user -> toolBox.getEventBus().send(new UserChangedEvent(user)));
});
}
}

public ComboBox<String> getUsersComboBox() {
if (usersComboBox == null) {

Expand All @@ -520,26 +534,30 @@ public ComboBox<String> getUsersComboBox() {
ObservableList<String> items = FXCollections.observableList(newItems);
usersComboBox.setItems(items);
usersComboBox.getSelectionModel().select(user.getUsername());
setUser(user.getUsername());
});
});

// When a username is selected send a change event
usersComboBox.getSelectionModel()
.selectedItemProperty()
.addListener((obs, oldv, newv) -> {
if (newv != null) {
toolBox.getServices()
.getUserService()
.findAllUsers()
.thenAccept(users -> {
Optional<User> opt = users.stream()
.filter(u -> u.getUsername().equals(newv))
.findFirst();
opt.ifPresent(user -> eventBus.send(new UserChangedEvent(user)));
});
usersComboBox.showingProperty().addListener((obs, oldv, newv) -> {
if (newv != null && !newv) {
setUser(usersComboBox.getSelectionModel().getSelectedItem());
}
});

usersComboBox.onHiddenProperty().addListener((obs, oldv, newv) -> {
setUser(usersComboBox.getSelectionModel().getSelectedItem());
});


// When a username is selected send a change event
// usersComboBox.getSelectionModel()
// .selectedItemProperty()
// .addListener((obs, oldv, newv) -> {
// if (newv != null && !usersComboBox.isShowing()) {
// setUser(newv);
// }
// });

loadUsers();

// Listen for new services event and update users after service is changed.
Expand Down Expand Up @@ -571,6 +589,7 @@ private void loadUsers() {
String defaultUser = System.getProperty("user.name");
if (usernames.contains(defaultUser)) {
usersComboBox.getSelectionModel().select(defaultUser);
setUser(defaultUser);
}
});
});
Expand Down

0 comments on commit 18ba08a

Please sign in to comment.