From 70da595a4951a5151135dea063e2df5e9c9c235d Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 10 Jul 2020 23:25:55 +0200 Subject: [PATCH] UnreadMessagesAmount gets displayed correctly now (despite refresh bug) --- src/main/java/envoy/client/data/Chat.java | 2 +- src/main/java/envoy/client/data/GroupChat.java | 10 ++++++++++ .../java/envoy/client/ui/controller/ChatScene.java | 4 +++- .../java/envoy/client/ui/listcell/ContactControl.java | 4 ++-- .../client/ui/listcell/ContactListCellFactory.java | 5 ++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 68d55fd..69f5d5e 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -114,7 +114,7 @@ public class Chat implements Serializable { public int getUnreadAmount() { int unreadMessagesAmount = 0; for (int i = messages.size() - 1; i >= 0; i--) { - if (messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++; + if (messages.get(i).getSenderID() == recipient.getID() && messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++; else break; } return unreadMessagesAmount; diff --git a/src/main/java/envoy/client/data/GroupChat.java b/src/main/java/envoy/client/data/GroupChat.java index 3c1e98d..d664124 100644 --- a/src/main/java/envoy/client/data/GroupChat.java +++ b/src/main/java/envoy/client/data/GroupChat.java @@ -37,6 +37,16 @@ public class GroupChat extends Chat { this.sender = sender; } + @Override + public int getUnreadAmount() { + int unreadMessagesAmount = 0; + for (int i = messages.size() - 1; i >= 0; i--) { + if (messages.get(i).getSenderID() != sender.getID() && messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++; + else break; + } + return unreadMessagesAmount; + } + @Override public void read(WriteProxy writeProxy) throws IOException { for (int i = messages.size() - 1; i >= 0; --i) { diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index d3940eb..36f4a7f 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -121,7 +121,8 @@ public final class ChatScene implements Restorable { logger.log(Level.WARNING, "Could not read current chat: ", e1); } Platform.runLater(() -> { messageList.refresh(); scrollToMessageListEnd(); }); - } // TODO set lable to getunreadmessages return value + } + userList.refresh(); }); }); @@ -236,6 +237,7 @@ public final class ChatScene implements Restorable { } messageTextArea.setDisable(currentChat == null || postingPermanentlyDisabled); voiceButton.setDisable(!recorder.isSupported()); + userList.refresh(); } /** diff --git a/src/main/java/envoy/client/ui/listcell/ContactControl.java b/src/main/java/envoy/client/ui/listcell/ContactControl.java index 077068c..b13b51e 100644 --- a/src/main/java/envoy/client/ui/listcell/ContactControl.java +++ b/src/main/java/envoy/client/ui/listcell/ContactControl.java @@ -23,7 +23,7 @@ public class ContactControl extends VBox { * @param contact the contact that should be formatted * @since Envoy Client v0.1-beta */ - public ContactControl(Contact contact) { + public ContactControl(Contact contact, int unreadMessagesAmount) { // Container with contact name final var nameLabel = new Label(contact.getName()); nameLabel.setWrapText(true); @@ -37,7 +37,7 @@ public class ContactControl extends VBox { } else // Member count getChildren().add(new Label(((Group) contact).getContacts().size() + " members")); - final var unreadMessagesLabel = new Label("5"); + final var unreadMessagesLabel = new Label("" + unreadMessagesAmount); getChildren().add(unreadMessagesLabel); } } diff --git a/src/main/java/envoy/client/ui/listcell/ContactListCellFactory.java b/src/main/java/envoy/client/ui/listcell/ContactListCellFactory.java index fd0b1b9..117a8b4 100644 --- a/src/main/java/envoy/client/ui/listcell/ContactListCellFactory.java +++ b/src/main/java/envoy/client/ui/listcell/ContactListCellFactory.java @@ -16,6 +16,7 @@ import envoy.client.data.Chat; public class ContactListCellFactory extends ListCell { private final ListView listView; + private Boolean displayUnreadMessagesAmount = false; /** * @param listView the list view inside which this cell is contained @@ -23,6 +24,8 @@ public class ContactListCellFactory extends ListCell { */ public ContactListCellFactory(ListView listView) { this.listView = listView; } + public void displayUnreadMessagesAmount() { displayUnreadMessagesAmount = true; } + /** * Displays the name of a contact. If the contact is a user, their online status * is displayed as well. @@ -36,7 +39,7 @@ public class ContactListCellFactory extends ListCell { setText(null); setGraphic(null); } else { - final var control = new ContactControl(chat.getRecipient()); + final var control = new ContactControl(chat.getRecipient(), chat.getUnreadAmount()); prefWidthProperty().bind(listView.widthProperty().subtract(40)); setGraphic(control); }