From 625d64ad5f24dc0762b1184a878574bf890359cb Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 18 Jul 2020 18:20:52 +0200 Subject: [PATCH] Fixed bug enabling contact duplication ...when two clients simultaneously add each other to the respective contact list --- .../ui/controller/ContactSearchScene.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/client/src/main/java/envoy/client/ui/controller/ContactSearchScene.java b/client/src/main/java/envoy/client/ui/controller/ContactSearchScene.java index e21c646..4583aad 100644 --- a/client/src/main/java/envoy/client/ui/controller/ContactSearchScene.java +++ b/client/src/main/java/envoy/client/ui/controller/ContactSearchScene.java @@ -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 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); })); } }