Removed old sync thread, implemented chat reading

This commit is contained in:
2020-02-01 10:20:06 +01:00
parent c62d7a8812
commit 8e449d150b
6 changed files with 37 additions and 74 deletions

View File

@ -169,7 +169,7 @@ public class ChatWindow extends JFrame {
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get();
// Set all unread messages in the chat to read
readCurrentChat();
currentChat.read();
client.setRecipient(user);
textPane.setText(currentChat.getRecipient().getName());
@ -204,9 +204,11 @@ public class ChatWindow extends JFrame {
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
Message message = ((MessageCreationEvent) evt).get();
localDB.getChats().stream().filter(c -> c.getRecipient().getId() == message.getRecipientId()).findFirst().get().appendMessage(message);
revalidate();
repaint();
});
contentPane.revalidate();
revalidate();
}
/**
@ -309,64 +311,13 @@ public class ChatWindow extends JFrame {
}
/**
* Updates the data model and the UI repeatedly after a certain amount of
* time.
*
* @param timeout the amount of time that passes between two requests sent to
* the server
* @since Envoy v0.1-alpha
*/
private void startSyncThread(int timeout) {
new Timer(timeout, (evt) -> {
new Thread(() -> {
// Synchronize
try {
// localDB.applySync(client.sendSync(client.getSender().getId(),
// localDB.fillSync(client.getSender().getId())));
} catch (Exception e) {
logger.log(Level.SEVERE, "Could not perform sync", e);
}
// TODO: Process unread messages
// localDB.addUnreadMessagesToLocalDB();
// localDB.clearUnreadMessagesSync();
// Mark unread messages as read when they are in the current chat
readCurrentChat();
// Update UI
SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
}).start();
}).start();
}
private void updateUserStates() {
for (int i = 0; i < userList.getModel().getSize(); i++)
for (int j = 0; j < localDB.getChats().size(); j++)
if (userList.getModel().getElementAt(i).getId() == localDB.getChats().get(j).getRecipient().getId())
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
}
/**
* Marks messages in the current chat as {@code READ}.
*/
private void readCurrentChat() {
if (currentChat != null) {
// TODO: localDB.setMessagesToRead(currentChat);
}
}
/**
* Sets the {@link Client} used by this {@link ChatWindow}. If the client is
* online, the sync thread is started.
* Sets the {@link Client} used by this {@link ChatWindow}.
*
* @param client the {@link Client} used to send and receive messages
* @since Envoy v0.2-alpha
*/
public void setClient(Client client) {
this.client = client;
if (client.isOnline() && localDB != null) startSyncThread(Config.getInstance().getSyncTimeout());
}
/**
@ -379,6 +330,5 @@ public class ChatWindow extends JFrame {
public void setLocalDB(LocalDB localDB) {
this.localDB = localDB;
loadUsersAndChats();
if (client != null && client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
}
}