From 6c7a80c7083508e9c1f2102cfac7ac6e88563d2a Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 12 Jun 2020 10:48:33 +0200 Subject: [PATCH] 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));