Reading the current chat if it changes or a message is received

Closes #144
This commit is contained in:
Kai S. K. Engelbart 2020-06-12 10:48:33 +02:00
parent 1a8c7dcfea
commit 6c7a80c708
2 changed files with 37 additions and 3 deletions

View File

@ -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

View File

@ -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));