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 {
@ -145,12 +143,11 @@ public class LocalDB {
} }
readMessages.getMessages().clear(); 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) {
@ -184,20 +181,17 @@ public class LocalDB {
&& 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)
.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).getMetadata().getMessageId() == returnSync
.getMessages()
.get(i) .get(i)
.getMetadata() .getMetadata()
.getRecipient()) { .getMessageId()) {
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
if (getChats()
.get(j)
.getModel()
.get(k)
.getMetadata()
.getMessageId() == returnSync.getMessages().get(i).getMetadata().getMessageId()) {
// Update Message in LocalDB // Update Message in LocalDB
getChats() getChats().get(j)
.get(j)
.getModel() .getModel()
.get(k) .get(k)
.getMetadata() .getMetadata()
@ -212,39 +206,29 @@ 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)
.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).getMetadata().getMessageId() == returnSync
.getMessages()
.get(i) .get(i)
.getMetadata() .getMetadata()
.getRecipient()) { .getMessageId()) {
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)
.getMetadata()
.getMessageId() == returnSync.getMessages().get(i).getMetadata().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());
} }
} }
} }
@ -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;
}
}
} }
/** /**

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