Fixed bug enabling contact duplication
...when two clients simultaneously add each other to the respective contact list
This commit is contained in:
parent
e51d2946d0
commit
a283217308
@ -1,5 +1,6 @@
|
||||
package envoy.client.ui.controller;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@ -53,8 +54,18 @@ public class ContactSearchScene {
|
||||
|
||||
private LocalDB localDB;
|
||||
|
||||
private static EventBus eventBus = EventBus.getInstance();
|
||||
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
|
||||
private Alert alert = new Alert(AlertType.CONFIRMATION);
|
||||
|
||||
private User currentlySelectedUser;
|
||||
|
||||
private final Consumer<ContactOperation> handler = e -> {
|
||||
final var contact = e.get();
|
||||
if (e.getOperationType() == ElementOperation.ADD) Platform
|
||||
.runLater(() -> { userList.getItems().remove(contact); if (currentlySelectedUser.equals(contact) && alert.isShowing()) alert.close(); });
|
||||
};
|
||||
|
||||
private static final EventBus eventBus = EventBus.getInstance();
|
||||
private static final Logger logger = EnvoyLog.getLogger(ChatScene.class);
|
||||
|
||||
/**
|
||||
* @param sceneContext enables the user to return to the chat scene
|
||||
@ -72,6 +83,7 @@ public class ContactSearchScene {
|
||||
searchBar.setClearButtonListener(e -> { searchBar.getTextField().clear(); userList.getItems().clear(); });
|
||||
eventBus.register(UserSearchResult.class,
|
||||
response -> Platform.runLater(() -> { userList.getItems().clear(); userList.getItems().addAll(response.get()); }));
|
||||
eventBus.register(ContactOperation.class, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,20 +120,21 @@ public class ContactSearchScene {
|
||||
private void userListClicked() {
|
||||
final var user = userList.getSelectionModel().getSelectedItem();
|
||||
if (user != null) {
|
||||
final var alert = new Alert(AlertType.CONFIRMATION);
|
||||
alert.setTitle("Add Contact to Contact List");
|
||||
alert.setHeaderText("Add the user " + user.getName() + " to your contact list?");
|
||||
currentlySelectedUser = user;
|
||||
alert = new Alert(AlertType.CONFIRMATION);
|
||||
alert.setTitle("Add User to Contact List");
|
||||
alert.setHeaderText("Add the user " + currentlySelectedUser.getName() + " to your contact list?");
|
||||
// Normally, this would be total BS (we are already on the FX Thread), however
|
||||
// it could be proven that the creation of this dialog wrapped in
|
||||
// Platform.runLater is less error-prone than without it
|
||||
Platform.runLater(() -> alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> {
|
||||
final var event = new ContactOperation(user, ElementOperation.ADD);
|
||||
final var event = new ContactOperation(currentlySelectedUser, ElementOperation.ADD);
|
||||
// Sends the event to the server
|
||||
eventBus.dispatch(new SendEvent(event));
|
||||
// Removes the chosen user and updates the UI
|
||||
userList.getItems().remove(user);
|
||||
userList.getItems().remove(currentlySelectedUser);
|
||||
eventBus.dispatch(event);
|
||||
logger.log(Level.INFO, "Added user " + user);
|
||||
logger.log(Level.INFO, "Added user " + currentlySelectedUser);
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user