Reading current chat when a new message is received

This commit is contained in:
DieGurke 2020-02-05 17:23:30 +01:00
parent 7d7dd02ceb
commit 9e024ede09
3 changed files with 27 additions and 14 deletions

View File

@ -29,7 +29,7 @@ public class MessageStatusChangeEventProcessor implements Consumer<MessageStatus
*/
@Override
public void accept(MessageStatusChangeEvent evt) {
if (evt.get().ordinal() <= MessageStatus.RECEIVED.ordinal()) logger.info("Received invalid message status change " + evt);
if (evt.get().ordinal() < MessageStatus.RECEIVED.ordinal()) logger.info("Received invalid message status change " + evt);
else {
logger.info("Received " + evt.toString());
EventBus.getInstance().dispatch(evt);

View File

@ -216,7 +216,20 @@ public class ChatWindow extends JFrame {
// Listen to received messages
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
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();
});

View File

@ -66,6 +66,18 @@ public class ComponentList<E> 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<E> 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);
}
}