From c839ef01ba6ceeb712ed7495bbd2ebc845056ebc Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Wed, 10 Jun 2020 13:05:44 +0200 Subject: [PATCH] Add received messages to their chat --- src/main/java/envoy/client/data/LocalDB.java | 18 ++++-- .../java/envoy/client/net/WriteProxy.java | 2 +- .../envoy/client/ui/controller/ChatScene.java | 61 +++++++------------ src/main/resources/fxml/ChatScene.fxml | 2 +- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index a9b0665..60bf7c0 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -148,12 +148,22 @@ public abstract class LocalDB { * Searches for a message by ID. * * @param id the ID of the message to search for - * @return the message with the corresponding ID, or {@code null} if no message - * has been found + * @return an optional containing the message * @since Envoy Client v0.1-beta */ - public Message getMessage(long id) { - return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null); + public Optional getMessage(long id) { + return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny(); + } + + /** + * Searches for a chat by recipient ID. + * + * @param recipientID the ID of the chat's recipient + * @return an optional containing the chat + * @since Envoy Client v0.1-beta + */ + public Optional getChat(long recipientID) { + return chats.stream().filter(c -> c.getRecipient().getID() == recipientID).findAny(); } /** diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 6e192f8..97848df 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -49,7 +49,7 @@ public class WriteProxy { client.sendMessage(msg); // Update message state to SENT in localDB - localDB.getMessage(msg.getID()).nextStatus(); + localDB.getMessage(msg.getID()).ifPresent(Message::nextStatus); } catch (IOException e) { logger.log(Level.SEVERE, "Could not send cached message", e); } diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index e57dedf..d74b82f 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -9,6 +9,7 @@ import javafx.application.Platform; import javafx.collections.FXCollections; import javafx.fxml.FXML; import javafx.scene.control.*; +import javafx.scene.control.Alert.AlertType; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; @@ -86,21 +87,24 @@ public final class ChatScene { // Listen to received messages eventBus.register(MessageCreationEvent.class, e -> { - final var message = e.get(); - final var chat = localDB.getChats().stream().filter(c -> c.getRecipient().getID() == message.getSenderID()).findAny().get(); + final var message = e.get(); + localDB.getChat(message.getSenderID()).ifPresent(chat -> { + chat.getMessages().add(message); - // Update UI if in current chat - if (chat == currentChat) Platform.runLater(() -> messageList.getItems().add(message)); + // Update UI if in current chat + if (chat == currentChat) + Platform.runLater(messageList::refresh); + }); }); // Listen to message status changes - eventBus.register(MessageStatusChangeEvent.class, e -> { - final var message = localDB.getMessage(e.getID()); + eventBus.register(MessageStatusChangeEvent.class, e -> + localDB.getMessage(e.getID()).ifPresent(message -> { message.setStatus(e.get()); // Update UI if in current chat if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) Platform.runLater(messageList::refresh); - }); + })); // Listen to user status changes eventBus.register(UserStatusChangeEvent.class, e -> @@ -165,10 +169,8 @@ public final class ChatScene { // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes // Load the chat or create a new one and add it to the LocalDB - currentChat = localDB.getChats() - .stream() - .filter(c -> c.getRecipient().getID() == user.getID()) - .findAny() + currentChat = localDB + .getChat(user.getID()) .orElseGet(() -> { final var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); messageList.setItems(FXCollections.observableList(currentChat.getMessages())); @@ -180,14 +182,6 @@ public final class ChatScene { messageTextArea.setDisable(currentChat == null); } - /** - * Actions to perform when the Post Button has been clicked. - * - * @since Envoy Client v0.1-beta - */ - @FXML - private void postButtonClicked() { postMessage(); } - /** * Actions to perform when the Settings Button has been clicked. * @@ -252,27 +246,13 @@ public final class ChatScene { * * @since Envoy Client v0.1-beta */ + @FXML private void postMessage() { - - // Create and send message - sendMessage(new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) - .setText(messageTextArea.getText().strip()) - .build()); - - // Clear text field and disable post button - messageTextArea.setText(""); - postButton.setDisable(true); - } - - /** - * Sends a message to the server and appends it to the current chat. If all - * message IDs have been used, a new ID generator is requested. - * - * @param message the message to send - * @since Envoy Client v0.1-beta - */ - private void sendMessage(Message message) { try { + // Create and send message + final var message = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator()) + .setText(messageTextArea.getText().strip()) + .build(); // Send message writeProxy.writeMessage(message); @@ -285,6 +265,11 @@ public final class ChatScene { } catch (final IOException e) { logger.log(Level.SEVERE, "Error sending message", e); + new Alert(AlertType.ERROR, "An error occured while sending the message!").showAndWait(); } + + // Clear text field and disable post button + messageTextArea.setText(""); + postButton.setDisable(true); } } diff --git a/src/main/resources/fxml/ChatScene.fxml b/src/main/resources/fxml/ChatScene.fxml index c134894..aab77a6 100644 --- a/src/main/resources/fxml/ChatScene.fxml +++ b/src/main/resources/fxml/ChatScene.fxml @@ -26,7 +26,7 @@