From 02ca6960f71d8ad4017d5f4bbc9dc7bdb9ba4bb9 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 12 Jun 2020 10:48:33 +0200 Subject: [PATCH 1/2] Reading the current chat if it changes or a message is received Closes #144 --- src/main/java/envoy/client/data/Chat.java | 22 +++++++++++++++++++ .../envoy/client/ui/controller/ChatScene.java | 18 ++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index a9148e2..7efbd7b 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import envoy.client.net.WriteProxy; import envoy.data.*; @@ -42,6 +43,27 @@ public final class Chat implements Serializable { @Override public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); } + /** + * Generates a hash code based on the recipient. + * + * @since Envoy Client v0.1-beta + */ + @Override + public int hashCode() { return Objects.hash(recipient); } + + /** + * Tests equality to another object based on the recipient. + * + * @since Envoy Client v0.1-beta + */ + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof Chat)) return false; + Chat other = (Chat) obj; + return Objects.equals(recipient, other.recipient); + } + /** * Sets the status of all chat messages received from the recipient to * {@code READ} starting from the bottom and stopping once a read message is diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index 8f7b487..a3a9591 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -91,9 +91,14 @@ public final class ChatScene { localDB.getChat(message.getSenderID()).ifPresent(chat -> { chat.getMessages().add(message); - // Update UI if in current chat - if (chat == currentChat) + if (chat.equals(currentChat)) { + try { + currentChat.read(writeProxy); + } catch (IOException e1) { + logger.log(Level.WARNING, "Could not read current chat.", e1); + } Platform.runLater(messageList::refresh); + } }); }); @@ -163,7 +168,7 @@ public final class ChatScene { @FXML private void userListClicked() { final Contact user = userList.getSelectionModel().getSelectedItem(); - if (user != null && (currentChat == null || user.getID() != currentChat.getRecipient().getID())) { + if (user != null && (currentChat == null || !user.equals(currentChat.getRecipient()))) { contactLabel.setText(user.getName()); // LEON: JFC <===> JAVA FRIED CHICKEN <=/=> Java Foundation Classes @@ -175,6 +180,13 @@ public final class ChatScene { messageList.setItems(FXCollections.observableList(currentChat.getMessages())); + // Read the current chat + try { + currentChat.read(writeProxy); + } catch (IOException e) { + logger.log(Level.WARNING, "Could not read current chat.", e); + } + remainingChars.setVisible(true); remainingChars .setText(String.format("remaining chars: %d/%d", MAX_MESSAGE_LENGTH - messageTextArea.getText().length(), MAX_MESSAGE_LENGTH)); From 2a26ce0cad32dd8d62eac10782bfe4177041efcc Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 13 Jun 2020 17:00:44 +0200 Subject: [PATCH 2/2] Reformat a logger statement Co-authored-by: delvh --- src/main/java/envoy/client/ui/controller/ChatScene.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index a3a9591..c25b2f8 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -95,7 +95,7 @@ public final class ChatScene { try { currentChat.read(writeProxy); } catch (IOException e1) { - logger.log(Level.WARNING, "Could not read current chat.", e1); + logger.log(Level.WARNING, "Could not read current chat: ", e1); } Platform.runLater(messageList::refresh); }