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;
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;

View File

@ -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: <strong>envoy-client</strong><br>
@ -29,6 +26,7 @@ import envoy.schema.Sync;
* Created: <strong>27.10.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy v0.1-alpha
*/
public class LocalDB {
@ -137,20 +135,19 @@ public class LocalDB {
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();
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) {
@ -184,20 +181,17 @@ public class LocalDB {
&& 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()
@ -212,39 +206,29 @@ 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());
}
}
}
@ -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;
}
/**

View File

@ -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