Copied event listeners from ChatWindow to ChatSceneController
This commit is contained in:
parent
00ce296812
commit
dbf944b5cb
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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()));
|
||||
|
Reference in New Issue
Block a user