Copied event listeners from ChatWindow to ChatSceneController

This commit is contained in:
Kai S. K. Engelbart 2020-03-29 22:15:05 +02:00
parent 00ce296812
commit dbf944b5cb
2 changed files with 32 additions and 7 deletions

View File

@ -153,9 +153,6 @@ public abstract class LocalDB {
* @since Envoy Client v0.1-beta * @since Envoy Client v0.1-beta
*/ */
public Message getMessage(long id) { public Message getMessage(long id) {
for (Chat c : chats) return chats.stream().map(Chat::getMessages).flatMap(List::stream).filter(m -> m.getID() == id).findAny().orElse(null);
for (Message m : c.getMessages())
if (m.getID() == id) return m;
return null;
} }
} }

View File

@ -6,11 +6,15 @@ import java.util.logging.Logger;
import envoy.client.data.Chat; import envoy.client.data.Chat;
import envoy.client.data.LocalDB; import envoy.client.data.LocalDB;
import envoy.client.event.MessageCreationEvent;
import envoy.client.net.Client; import envoy.client.net.Client;
import envoy.client.net.WriteProxy; import envoy.client.net.WriteProxy;
import envoy.data.Message; import envoy.data.Message;
import envoy.data.MessageBuilder; import envoy.data.MessageBuilder;
import envoy.data.User; import envoy.data.User;
import envoy.event.EventBus;
import envoy.event.MessageStatusChangeEvent;
import envoy.event.UserStatusChangeEvent;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -52,12 +56,37 @@ public final class ChatSceneController {
private Chat currentChat; 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 @FXML
private void initialize() { private void initialize() {
// Initialize message and user rendering
messageList.setCellFactory(listView -> new MessageListCell()); messageList.setCellFactory(listView -> new MessageListCell());
userList.setCellFactory(listView -> new UserListCell()); 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) { 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 // Load the chat or create a new one and add it to the LocalDB
currentChat = localDB.getChats() currentChat = localDB.getChats()
.stream() .stream()
.filter(c -> c.getRecipient().getID() == user .filter(c -> c.getRecipient().getID() == user.getID())
.getID())
.findAny() .findAny()
.orElseGet(() -> { var chat = new Chat(user); localDB.getChats().add(chat); return chat; }); .orElseGet(() -> { var chat = new Chat(user); localDB.getChats().add(chat); return chat; });
messageList.setItems(FXCollections.observableArrayList(currentChat.getMessages())); messageList.setItems(FXCollections.observableArrayList(currentChat.getMessages()));