Reading current chat when a new message is received

This commit is contained in:
DieGurke 2020-02-05 17:23:30 +01:00
parent 58b9ac8081
commit 8f4cf1428a
3 changed files with 27 additions and 14 deletions

View File

@ -29,7 +29,7 @@ public class MessageStatusChangeEventProcessor implements Consumer<MessageStatus
*/ */
@Override @Override
public void accept(MessageStatusChangeEvent evt) { 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 { else {
logger.info("Received " + evt.toString()); logger.info("Received " + evt.toString());
EventBus.getInstance().dispatch(evt); EventBus.getInstance().dispatch(evt);

View File

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

View File

@ -66,6 +66,18 @@ public class ComponentList<E> extends JPanel {
synchronizeModel(); 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 * Adds an object to the list by rendering it with the current
* {@link ComponentListCellRenderer}. * {@link ComponentListCellRenderer}.
@ -76,16 +88,4 @@ public class ComponentList<E> extends JPanel {
void add(E elem) { void add(E elem) {
add(renderer.getListCellComponent(this, elem, false)); 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);
}
} }