From 817117905dc8c0b2fa2fbf1c30446b8c1bb9632e Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 9 Nov 2019 14:06:09 +0100 Subject: [PATCH] Fixed reading messages Fixes #23 --- src/main/java/envoy/client/Client.java | 5 - src/main/java/envoy/client/LocalDB.java | 127 ++++++++---------- src/main/java/envoy/client/ui/ChatWindow.java | 5 +- 3 files changed, 57 insertions(+), 80 deletions(-) diff --git a/src/main/java/envoy/client/Client.java b/src/main/java/envoy/client/Client.java index ed2ff22..f56cfe4 100644 --- a/src/main/java/envoy/client/Client.java +++ b/src/main/java/envoy/client/Client.java @@ -1,6 +1,5 @@ package envoy.client; -import java.time.Instant; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.WebTarget; @@ -8,11 +7,7 @@ import javax.ws.rs.core.Response; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; -import javax.xml.datatype.DatatypeConfigurationException; -import javax.xml.datatype.DatatypeFactory; -import envoy.schema.Message; -import envoy.schema.Message.Metadata.MessageState; import envoy.schema.ObjectFactory; import envoy.schema.Sync; import envoy.schema.User; diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index fb69a94..e0ff7c1 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -10,18 +10,15 @@ import java.time.Instant; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; import javax.xml.datatype.DatatypeConfigurationException; import javax.xml.datatype.DatatypeFactory; import envoy.exception.EnvoyException; import envoy.schema.Message; -import envoy.schema.User; import envoy.schema.Message.Metadata.MessageState; import envoy.schema.ObjectFactory; import envoy.schema.Sync; +import envoy.schema.User; /** * Project: envoy-client
@@ -29,6 +26,7 @@ import envoy.schema.Sync; * Created: 27.10.2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy v0.1-alpha */ public class LocalDB { @@ -130,27 +128,26 @@ public class LocalDB { return message; } - + private Sync unreadMessagesSync = objectFactory.createSync(); public Sync sync = objectFactory.createSync(); public Sync readMessages = objectFactory.createSync(); - - public Sync fillSync(long userId) { - - addWaitingMessagesToSync(); - getSentStateMessagesFromLocalDB(); - for (int i = 0; i < readMessages.getMessages().size(); i++) { - sync.getMessages().add(readMessages.getMessages().get(i)); - } - readMessages.getMessages().clear(); - + public Sync fillSync(long userId) { + + addWaitingMessagesToSync(); + + getSentStateMessagesFromLocalDB(); + for (int i = 0; i < readMessages.getMessages().size(); i++) { + sync.getMessages().add(readMessages.getMessages().get(i)); + } + readMessages.getMessages().clear(); System.out.println(sync.getMessages().size()); return sync; } - - public void applySync (Sync returnSync) { + + public void applySync(Sync returnSync) { for (int i = 0; i < returnSync.getMessages().size(); i++) { if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { @@ -167,37 +164,34 @@ public class LocalDB { .getMetadata() .setState(returnSync.getMessages().get(j).getMetadata().getState()); } - + } - + } if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { // these are the unread Messages from the server unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); - + } - + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { // Update Messages in localDB to state RECEIVED for (int j = 0; j < getChats().size(); j++) { - if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { + if (getChats().get(j) + .getRecipient() + .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { - if (getChats() - .get(j) - .getModel() - .get(k) + if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync + .getMessages() + .get(i) .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + .getMessageId()) { // Update Message in LocalDB - getChats() - .get(j) + getChats().get(j) .getModel() .get(k) .getMetadata() @@ -206,50 +200,40 @@ public class LocalDB { } } } - + } - + if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { // Update local Messages to state READ - System.out - .println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() - + "was initialized to be set to READ in localDB."); + System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + + "was initialized to be set to READ in localDB."); for (int j = 0; j < getChats().size(); j++) { - if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() - .get(i) - .getMetadata() - .getRecipient()) { - System.out.println( - "Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); + if (getChats().get(j) + .getRecipient() + .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { + System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { - if (getChats() - .get(j) - .getModel() - .get(k) + if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync + .getMessages() + .get(i) .getMetadata() - .getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { + .getMessageId()) { System.out.println("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); - getChats() - .get(j) + getChats().get(j) .getModel() .get(k) .getMetadata() .setState(returnSync.getMessages().get(i).getMetadata().getState()); - System.out.println("Message State is now: " + getChats() - .get(j) - .getModel() - .get(k) - .getMetadata() - .getState() - .toString()); + System.out.println("Message State is now: " + + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); } } } } - + } } @@ -257,13 +241,13 @@ public class LocalDB { for (int j = 0; j < returnSync.getUsers().size(); j++) { for (int k = 0; k < getChats().size(); k++) { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { - + getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); System.out.println(getChats().get(k).getRecipient().getStatus().toString()); } } } - + sync.getMessages().clear(); sync.getUsers().clear(); @@ -296,10 +280,9 @@ public class LocalDB { Sync unreadMessages = unreadMessagesSync; for (int i = 0; i < unreadMessages.getMessages().size(); i++) for (int j = 0; j < getChats().size(); j++) - if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() - .get(i) - .getMetadata() - .getSender()) { + if (getChats().get(j) + .getRecipient() + .getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) { getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); } } @@ -330,14 +313,12 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public void setMessagesToRead(Chat currentChat) { - for (int j = 0; j < currentChat.getModel().getSize(); j++) { - if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { - if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { - currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); - readMessages.getMessages().add(currentChat.getModel().get(j)); - } - } - } + for (int i = currentChat.getModel().size() - 1; i >= 0; --i) + if (currentChat.getModel().get(i).getMetadata().getRecipient() != currentChat.getRecipient().getID()) + if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) { + currentChat.getModel().get(i).getMetadata().setState(MessageState.READ); + readMessages.getMessages().add(currentChat.getModel().get(i)); + } else break; } /** @@ -373,7 +354,7 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } - + /** * @return all saves {@link Chat} objects that list the client user as the * sender diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 545715e..c6e8c37 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -205,6 +205,9 @@ public class ChatWindow extends JFrame { .findFirst() .get(); + // Set all unread messages in the chat to read + if (currentChat != null) { localDB.setMessagesToRead(currentChat); } + client.setRecipient(user); textPane.setText(currentChat.getRecipient().getName()); @@ -265,8 +268,6 @@ public class ChatWindow extends JFrame { */ private void startSyncThread(int timeout) { new Timer(timeout, (evt) -> { - if (currentChat != null) { localDB.setMessagesToRead(currentChat); } - new Thread(() -> { // Synchronize