prevent sending empty messages

This commit is contained in:
Arnaud Vergnet 2021-01-03 13:54:45 +01:00
parent e04b780397
commit 060137115f
2 changed files with 18 additions and 13 deletions

View file

@ -1,5 +1,6 @@
package fr.insa.clavardator.ui.chat;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import fr.insa.clavardator.ui.ButtonPressEvent;
import fr.insa.clavardator.users.PeerUser;
@ -23,6 +24,8 @@ public class ChatFooterController implements Initializable {
private HBox container;
@FXML
private JFXTextField textField;
@FXML
private JFXButton sendButton;
private List<ButtonPressEvent> attachmentListeners;
private List<ErrorCallback> sendErrorListeners;
@ -42,12 +45,14 @@ public class ChatFooterController implements Initializable {
attachmentListeners.forEach(ButtonPressEvent::onPress);
}
public void onSend() {
if (remoteUser != null) {
remoteUser.sendTextMessage(textField.getText(), this::onSendError);
} else {
Log.e(this.getClass().getSimpleName(), "Error: remote user not set");
if(!textField.getText().isEmpty()) {
if (remoteUser != null) {
remoteUser.sendTextMessage(textField.getText(), this::onSendError);
} else {
Log.e(this.getClass().getSimpleName(), "Error: remote user not set");
}
textField.setText("");
}
textField.setText("");
}
public void onSendError(Exception e) {
@ -75,6 +80,7 @@ public class ChatFooterController implements Initializable {
public void onTextChange(ObservableValue<? extends String> observable, String oldText, String newText) {
saveText(newText);
sendButton.setDisable(isTextFieldEmpty());
}
@Override
@ -86,19 +92,18 @@ public class ChatFooterController implements Initializable {
textField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
event.consume();
if(!textField.getText().isEmpty()){
onSend();
}
onSend();
}
});
}
private boolean isTextFieldEmpty() {
return textField.getText().equals("");
}
public void setRemoteUser(PeerUser remoteUser) {
this.remoteUser = remoteUser;
textField.setText(findSavedText());
}
public interface SendMessageEvent {
void onSend(String text);
sendButton.setDisable(isTextFieldEmpty());
}
}

View file

@ -18,7 +18,7 @@
<FontIcon iconLiteral="fas-paperclip" iconSize="24"/>
</graphic>
</JFXButton>
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSend">
<JFXButton mnemonicParsing="false" text="Envoyer" onMouseClicked="#onSend" fx:id="sendButton">
<graphic>
<FontIcon iconLiteral="fas-paper-plane" iconSize="24"/>
</graphic>