Warned user on group creation if he already has a Group with that name

This commit is contained in:
delvh 2020-07-16 17:47:59 +02:00
parent ae41ab9637
commit ec700685ac

View File

@ -16,6 +16,8 @@ import envoy.client.ui.ClearableTextField;
import envoy.client.ui.SceneContext; import envoy.client.ui.SceneContext;
import envoy.client.ui.listcell.ContactControl; import envoy.client.ui.listcell.ContactControl;
import envoy.client.ui.listcell.ListCellFactory; import envoy.client.ui.listcell.ListCellFactory;
import envoy.data.Contact;
import envoy.data.Group;
import envoy.data.User; import envoy.data.User;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.event.GroupCreation; import envoy.event.GroupCreation;
@ -50,6 +52,8 @@ public class GroupCreationScene {
private SceneContext sceneContext; private SceneContext sceneContext;
private LocalDB localDB;
private static final EventBus eventBus = EventBus.getInstance(); private static final EventBus eventBus = EventBus.getInstance();
@FXML @FXML
@ -66,7 +70,8 @@ public class GroupCreationScene {
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public void initializeData(SceneContext sceneContext, LocalDB localDB) { public void initializeData(SceneContext sceneContext, LocalDB localDB) {
this.sceneContext = sceneContext; this.sceneContext = sceneContext;
this.localDB = localDB;
Platform.runLater(() -> userList.getItems() Platform.runLater(() -> userList.getItems()
.addAll(localDB.getChats() .addAll(localDB.getChats()
.stream() .stream()
@ -109,14 +114,47 @@ public class GroupCreationScene {
if (!Bounds.isValidContactName(name)) { if (!Bounds.isValidContactName(name)) {
new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait(); new Alert(AlertType.ERROR, "The entered group name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
groupNameField.getTextField().clear(); 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 { } 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(); 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.<br>
* 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 @FXML
private void backButtonClicked() { sceneContext.pop(); } private void backButtonClicked() { sceneContext.pop(); }
} }