From c0f4a8e212a47f2cff1dc7161dfbea62913fa483 Mon Sep 17 00:00:00 2001 From: delvh Date: Thu, 16 Jul 2020 17:47:59 +0200 Subject: [PATCH] Warned user on group creation if he already has a Group with that name --- .../ui/controller/GroupCreationScene.java | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/envoy/client/ui/controller/GroupCreationScene.java b/client/src/main/java/envoy/client/ui/controller/GroupCreationScene.java index e6ca29a..88f66d7 100644 --- a/client/src/main/java/envoy/client/ui/controller/GroupCreationScene.java +++ b/client/src/main/java/envoy/client/ui/controller/GroupCreationScene.java @@ -16,6 +16,8 @@ import envoy.client.ui.ClearableTextField; import envoy.client.ui.SceneContext; import envoy.client.ui.listcell.ContactControl; import envoy.client.ui.listcell.ListCellFactory; +import envoy.data.Contact; +import envoy.data.Group; import envoy.data.User; import envoy.event.EventBus; import envoy.event.GroupCreation; @@ -50,6 +52,8 @@ public class GroupCreationScene { private SceneContext sceneContext; + private LocalDB localDB; + private static final EventBus eventBus = EventBus.getInstance(); @FXML @@ -66,7 +70,8 @@ public class GroupCreationScene { * @since Envoy Client v0.1-beta */ public void initializeData(SceneContext sceneContext, LocalDB localDB) { - this.sceneContext = sceneContext; + this.sceneContext = sceneContext; + this.localDB = localDB; Platform.runLater(() -> userList.getItems() .addAll(localDB.getChats() .stream() @@ -109,14 +114,47 @@ public class GroupCreationScene { if (!Bounds.isValidContactName(name)) { new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); groupNameField.getTextField().clear(); + } else if (groupNameAlreadyPresent(name)) { + final var alert = new Alert(AlertType.WARNING, "You already have a group with that name.", ButtonType.OK, ButtonType.CANCEL); + alert.setTitle("Create Group?"); + alert.setHeaderText("Proceed?"); + alert.showAndWait().filter(btn -> btn == ButtonType.OK).ifPresent(btn -> createGroup(name)); } else { - eventBus.dispatch(new SendEvent( - new GroupCreation(name, userList.getSelectionModel().getSelectedItems().stream().map(User::getID).collect(Collectors.toSet())))); new Alert(AlertType.INFORMATION, String.format("Group '%s' successfully created.", name)).showAndWait(); - sceneContext.pop(); + createGroup(name); } } + /** + * Creates a new group with the given name and all selected members.
+ * Additionally pops the scene automatically. + * + * @param name the chosen group name + * @since Envoy Client v0.1-beta + */ + private void createGroup(String name) { + eventBus.dispatch(new SendEvent( + new GroupCreation(name, userList.getSelectionModel().getSelectedItems().stream().map(User::getID).collect(Collectors.toSet())))); + sceneContext.pop(); + } + + /** + * Returns true if the proposed group name is already present in the users + * {@code LocalDB}. + * + * @param newName the chosen group name + * @return true if this name is already present + * @since Envoy Client v0.1-beta + */ + public boolean groupNameAlreadyPresent(String newName) { + return localDB.getChats() + .stream() + .map(Chat::getRecipient) + .filter(Group.class::isInstance) + .map(Contact::getName) + .anyMatch(groupName -> groupName.equals(newName)); + } + @FXML private void backButtonClicked() { sceneContext.pop(); } }