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

View file

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