From 9e024ede099be3c46539002f904ea5ea68b8d88e Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Wed, 5 Feb 2020 17:23:30 +0100 Subject: [PATCH] Reading current chat when a new message is received --- .../MessageStatusChangeEventProcessor.java | 2 +- src/main/java/envoy/client/ui/ChatWindow.java | 15 +++++++++++- .../envoy/client/ui/list/ComponentList.java | 24 +++++++++---------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java index 1ceecf1..1cf457f 100644 --- a/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java +++ b/src/main/java/envoy/client/net/MessageStatusChangeEventProcessor.java @@ -29,7 +29,7 @@ public class MessageStatusChangeEventProcessor implements Consumer { Message message = ((MessageCreationEvent) evt).get(); - localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get().appendMessage(message); + Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get(); + chat.appendMessage(message); + + // Read message and update UI if in current chat + if (chat == currentChat) { + try { + currentChat.read(client); + } catch (IOException e) { + e.printStackTrace(); + logger.log(Level.WARNING, "Could notify server about message status change", e); + } + messageList.synchronizeModel(); + } + revalidate(); repaint(); }); diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 8cbf2db..f62dc7b 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -66,6 +66,18 @@ public class ComponentList extends JPanel { synchronizeModel(); } + /** + * Removes all child components and then adds all components representing the + * elements of the {@link ComponentListModel}. + * + * @since Envoy v0.3-alpha + */ + public void synchronizeModel() { + removeAll(); + if (model != null) for (E elem : model) + add(elem); + } + /** * Adds an object to the list by rendering it with the current * {@link ComponentListCellRenderer}. @@ -76,16 +88,4 @@ public class ComponentList extends JPanel { void add(E elem) { add(renderer.getListCellComponent(this, elem, false)); } - - /** - * Removes all child components and then adds all components representing the - * elements of the {@link ComponentListModel}. - * - * @since Envoy v0.3-alpha - */ - void synchronizeModel() { - removeAll(); - if (model != null) for (E elem : model) - add(elem); - } }