From a6efe7cd9be27dd767e4818a8bbef413a6e89e95 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 12 Jul 2020 14:34:07 +0200 Subject: [PATCH] Implemented good suggestion by @delvh regarding performance increase --- src/main/java/envoy/client/data/Chat.java | 14 ++++++-------- src/main/java/envoy/client/data/GroupChat.java | 11 +---------- .../java/envoy/client/ui/controller/ChatScene.java | 2 +- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 69f5d5e..b08fb25 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -29,6 +29,8 @@ public class Chat implements Serializable { protected final Contact recipient; protected final List messages = new ArrayList<>(); + protected int unreadAmount; + private static final long serialVersionUID = 1L; /** @@ -87,6 +89,7 @@ public class Chat implements Serializable { writeProxy.writeMessageStatusChange(new MessageStatusChange(m)); } } + unreadAmount = 0; } /** @@ -111,14 +114,9 @@ public class Chat implements Serializable { messages.add(0, message); } - public int getUnreadAmount() { - int unreadMessagesAmount = 0; - for (int i = messages.size() - 1; i >= 0; i--) { - if (messages.get(i).getSenderID() == recipient.getID() && messages.get(i).getStatus() == MessageStatus.RECEIVED) unreadMessagesAmount++; - else break; - } - return unreadMessagesAmount; - } + public void incrementUnreadAmount() { unreadAmount++; } + + public int getUnreadAmount() { return unreadAmount; } /** * @return all messages in the current chat diff --git a/src/main/java/envoy/client/data/GroupChat.java b/src/main/java/envoy/client/data/GroupChat.java index d664124..1bdbde3 100644 --- a/src/main/java/envoy/client/data/GroupChat.java +++ b/src/main/java/envoy/client/data/GroupChat.java @@ -37,16 +37,6 @@ 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) { @@ -60,5 +50,6 @@ public class GroupChat extends Chat { } } } + super.unreadAmount = 0; } } diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index ef88dea..851833e 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -134,7 +134,7 @@ public final class ChatScene implements Restorable { logger.log(Level.WARNING, "Could not read current chat: ", e1); } Platform.runLater(() -> { messageList.refresh(); scrollToMessageListEnd(); }); - } + } else chat.incrementUnreadAmount(); // Moving chat with most recent unreadMessages to the top Platform.runLater(() -> { userList.getItems().remove(chat);