From dbf944b5cbc442a08256a9ce199598ecc1d4ed83 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sun, 29 Mar 2020 22:15:05 +0200 Subject: [PATCH] Copied event listeners from ChatWindow to ChatSceneController --- src/main/java/envoy/client/data/LocalDB.java | 5 +-- .../envoy/client/ui/ChatSceneController.java | 34 +++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 1e36307..7de7383 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -153,9 +153,6 @@ public abstract class LocalDB { * @since Envoy Client v0.1-beta */ public Message getMessage(long id) { - for (Chat c : chats) - for (Message m : c.getMessages()) - if (m.getID() == id) return m; - return null; + return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null); } } diff --git a/src/main/java/envoy/client/ui/ChatSceneController.java b/src/main/java/envoy/client/ui/ChatSceneController.java index 588f407..805f9f7 100644 --- a/src/main/java/envoy/client/ui/ChatSceneController.java +++ b/src/main/java/envoy/client/ui/ChatSceneController.java @@ -6,11 +6,15 @@ import java.util.logging.Logger; import envoy.client.data.Chat; import envoy.client.data.LocalDB; +import envoy.client.event.MessageCreationEvent; import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.data.Message; import envoy.data.MessageBuilder; import envoy.data.User; +import envoy.event.EventBus; +import envoy.event.MessageStatusChangeEvent; +import envoy.event.UserStatusChangeEvent; import envoy.util.EnvoyLog; import javafx.collections.FXCollections; import javafx.fxml.FXML; @@ -52,12 +56,37 @@ public final class ChatSceneController { private Chat currentChat; - private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class); + private static final EventBus eventBus = EventBus.getInstance(); + private static final Logger logger = EnvoyLog.getLogger(ChatSceneController.class); @FXML private void initialize() { + + // Initialize message and user rendering messageList.setCellFactory(listView -> new MessageListCell()); userList.setCellFactory(listView -> new UserListCell()); + + // Listen to received messages + eventBus.register(MessageCreationEvent.class, e -> { + final var message = e.get(); + final var chat = localDB.getChats().stream().filter(c -> c.getRecipient().getID() == message.getSenderID()).findAny().get(); + chat.getMessages().add(message); + + // Update UI if in current chat + if (chat == currentChat) messageList.getItems().add(message); + }); + + // Listen to message status changes + eventBus.register(MessageStatusChangeEvent.class, e -> { + final var message = localDB.getMessage(e.getID()); + message.setStatus(e.get()); + + // Update UI if in current chat + if (currentChat != null && message.getSenderID() == currentChat.getRecipient().getID()) messageList.refresh(); + }); + + // Listen to user status changes + eventBus.register(UserStatusChangeEvent.class, e -> userList.refresh()); } void initializeData(LocalDB localDB, Client client, WriteProxy writeProxy) { @@ -87,8 +116,7 @@ public final class ChatSceneController { // Load the chat or create a new one and add it to the LocalDB currentChat = localDB.getChats() .stream() - .filter(c -> c.getRecipient().getID() == user - .getID()) + .filter(c -> c.getRecipient().getID() == user.getID()) .findAny() .orElseGet(() -> { var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); messageList.setItems(FXCollections.observableArrayList(currentChat.getMessages()));