Fixed reading messages

Fixes #23
This commit is contained in:
DieGurke 2019-11-09 14:06:09 +01:00
parent 42f3a97e8a
commit cd8a92c619
3 changed files with 57 additions and 80 deletions

View File

@ -1,6 +1,5 @@
package envoy.client; package envoy.client;
import java.time.Instant;
import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity; import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget; 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.JAXBContext;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; 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.ObjectFactory;
import envoy.schema.Sync; import envoy.schema.Sync;
import envoy.schema.User; import envoy.schema.User;

View File

@ -10,18 +10,15 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory; import javax.xml.datatype.DatatypeFactory;
import envoy.exception.EnvoyException; import envoy.exception.EnvoyException;
import envoy.schema.Message; import envoy.schema.Message;
import envoy.schema.User;
import envoy.schema.Message.Metadata.MessageState; import envoy.schema.Message.Metadata.MessageState;
import envoy.schema.ObjectFactory; import envoy.schema.ObjectFactory;
import envoy.schema.Sync; import envoy.schema.Sync;
import envoy.schema.User;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -29,6 +26,7 @@ import envoy.schema.Sync;
* Created: <strong>27.10.2019</strong><br> * Created: <strong>27.10.2019</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public class LocalDB { public class LocalDB {
@ -130,27 +128,26 @@ public class LocalDB {
return message; return message;
} }
private Sync unreadMessagesSync = objectFactory.createSync(); private Sync unreadMessagesSync = objectFactory.createSync();
public Sync sync = objectFactory.createSync(); public Sync sync = objectFactory.createSync();
public Sync readMessages = objectFactory.createSync(); public Sync readMessages = objectFactory.createSync();
public Sync fillSync(long userId) {
addWaitingMessagesToSync();
getSentStateMessagesFromLocalDB(); public Sync fillSync(long userId) {
for (int i = 0; i < readMessages.getMessages().size(); i++) {
sync.getMessages().add(readMessages.getMessages().get(i)); addWaitingMessagesToSync();
}
readMessages.getMessages().clear(); 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()); System.out.println(sync.getMessages().size());
return sync; return sync;
} }
public void applySync (Sync returnSync) { public void applySync(Sync returnSync) {
for (int i = 0; i < returnSync.getMessages().size(); i++) { for (int i = 0; i < returnSync.getMessages().size(); i++) {
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) {
@ -167,37 +164,34 @@ public class LocalDB {
.getMetadata() .getMetadata()
.setState(returnSync.getMessages().get(j).getMetadata().getState()); .setState(returnSync.getMessages().get(j).getMetadata().getState());
} }
} }
} }
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
&& returnSync.getMessages().get(i).getMetadata().getSender() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
// these are the unread Messages from the server // these are the unread Messages from the server
unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i)); unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
} }
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
&& returnSync.getMessages().get(i).getMetadata().getSender() == 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
// Update Messages in localDB to state RECEIVED // Update Messages in localDB to state RECEIVED
for (int j = 0; j < getChats().size(); j++) { for (int j = 0; j < getChats().size(); j++) {
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() if (getChats().get(j)
.get(i) .getRecipient()
.getMetadata() .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
.getRecipient()) {
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
if (getChats() if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
.get(j) .getMessages()
.getModel() .get(i)
.get(k)
.getMetadata() .getMetadata()
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { .getMessageId()) {
// Update Message in LocalDB // Update Message in LocalDB
getChats() getChats().get(j)
.get(j)
.getModel() .getModel()
.get(k) .get(k)
.getMetadata() .getMetadata()
@ -206,50 +200,40 @@ public class LocalDB {
} }
} }
} }
} }
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { && returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
// Update local Messages to state READ // Update local Messages to state READ
System.out System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() + "was initialized to be set to READ in localDB.");
+ "was initialized to be set to READ in localDB.");
for (int j = 0; j < getChats().size(); j++) { for (int j = 0; j < getChats().size(); j++) {
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages() if (getChats().get(j)
.get(i) .getRecipient()
.getMetadata() .getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
.getRecipient()) { System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
System.out.println(
"Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
if (getChats() if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
.get(j) .getMessages()
.getModel() .get(i)
.get(k)
.getMetadata() .getMetadata()
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) { .getMessageId()) {
System.out.println("Message with ID: " System.out.println("Message with ID: "
+ getChats().get(j).getModel().get(k).getMetadata().getMessageId() + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
+ "was selected."); + "was selected.");
getChats() getChats().get(j)
.get(j)
.getModel() .getModel()
.get(k) .get(k)
.getMetadata() .getMetadata()
.setState(returnSync.getMessages().get(i).getMetadata().getState()); .setState(returnSync.getMessages().get(i).getMetadata().getState());
System.out.println("Message State is now: " + getChats() System.out.println("Message State is now: "
.get(j) + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
.getModel()
.get(k)
.getMetadata()
.getState()
.toString());
} }
} }
} }
} }
} }
} }
@ -257,13 +241,13 @@ public class LocalDB {
for (int j = 0; j < returnSync.getUsers().size(); j++) { for (int j = 0; j < returnSync.getUsers().size(); j++) {
for (int k = 0; k < getChats().size(); k++) { for (int k = 0; k < getChats().size(); k++) {
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) {
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus());
System.out.println(getChats().get(k).getRecipient().getStatus().toString()); System.out.println(getChats().get(k).getRecipient().getStatus().toString());
} }
} }
} }
sync.getMessages().clear(); sync.getMessages().clear();
sync.getUsers().clear(); sync.getUsers().clear();
@ -296,10 +280,9 @@ public class LocalDB {
Sync unreadMessages = unreadMessagesSync; Sync unreadMessages = unreadMessagesSync;
for (int i = 0; i < unreadMessages.getMessages().size(); i++) for (int i = 0; i < unreadMessages.getMessages().size(); i++)
for (int j = 0; j < getChats().size(); j++) for (int j = 0; j < getChats().size(); j++)
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages() if (getChats().get(j)
.get(i) .getRecipient()
.getMetadata() .getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
.getSender()) {
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
} }
} }
@ -330,14 +313,12 @@ public class LocalDB {
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void setMessagesToRead(Chat currentChat) { public void setMessagesToRead(Chat currentChat) {
for (int j = 0; j < currentChat.getModel().getSize(); j++) { for (int i = currentChat.getModel().size() - 1; i >= 0; --i)
if (currentChat.getModel().get(j).getMetadata().getRecipient() != currentChat.getRecipient().getID()) { if (currentChat.getModel().get(i).getMetadata().getRecipient() != currentChat.getRecipient().getID())
if (currentChat.getModel().get(j).getMetadata().getState() == MessageState.RECEIVED) { if (currentChat.getModel().get(i).getMetadata().getState() == MessageState.RECEIVED) {
currentChat.getModel().get(j).getMetadata().setState(MessageState.READ); currentChat.getModel().get(i).getMetadata().setState(MessageState.READ);
readMessages.getMessages().add(currentChat.getModel().get(j)); readMessages.getMessages().add(currentChat.getModel().get(i));
} } else break;
}
}
} }
/** /**
@ -373,7 +354,7 @@ public class LocalDB {
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); }
/** /**
* @return all saves {@link Chat} objects that list the client user as the * @return all saves {@link Chat} objects that list the client user as the
* sender * sender

View File

@ -205,6 +205,9 @@ public class ChatWindow extends JFrame {
.findFirst() .findFirst()
.get(); .get();
// Set all unread messages in the chat to read
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
client.setRecipient(user); client.setRecipient(user);
textPane.setText(currentChat.getRecipient().getName()); textPane.setText(currentChat.getRecipient().getName());
@ -265,8 +268,6 @@ public class ChatWindow extends JFrame {
*/ */
private void startSyncThread(int timeout) { private void startSyncThread(int timeout) {
new Timer(timeout, (evt) -> { new Timer(timeout, (evt) -> {
if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
new Thread(() -> { new Thread(() -> {
// Synchronize // Synchronize