Simplified and optimized sync related code in LocalDB

This commit is contained in:
Kai S. K. Engelbart 2019-12-05 15:05:05 +01:00
parent cf6735f621
commit 25181a1408

View File

@ -147,28 +147,26 @@ public class LocalDB {
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
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.SENT) { // The message has an ID
// Update Local Messages with State WAITING (add Message ID and change State to if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0) {
// SENT)
for (int j = 0; j < sync.getMessages().size(); j++) { // Messages are processes differently corresponding to their state
if (j == i) { switch (returnSync.getMessages().get(i).getMetadata().getState()) {
sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId()); case SENT:
sync.getMessages().get(j).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState()); // Update previously waiting and now sent messages that were assigned an ID by
} // the server
} sync.getMessages().get(i).getMetadata().setMessageId(returnSync.getMessages().get(i).getMetadata().getMessageId());
} sync.getMessages().get(i).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0 break;
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { case RECEIVED:
if (returnSync.getMessages().get(i).getMetadata().getSender() != 0) {
// 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));
// Create and dispatch message creation event // Create and dispatch message creation event
EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i))); EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i)));
} } else {
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 // 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().get(i).getMetadata().getRecipient()) { if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
@ -178,16 +176,18 @@ public class LocalDB {
.getMetadata() .getMetadata()
.getMessageId()) { .getMessageId()) {
// Update Message in LocalDB // Update Message in LocalDB
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState()); getChats().get(j)
.getModel()
.get(k)
.getMetadata()
.setState(returnSync.getMessages().get(j).getMetadata().getState());
} }
} }
} }
} }
} }
break;
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 case READ:
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) {
// Update local Messages to state READ // Update local Messages to state READ
System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() System.out.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.");
@ -199,27 +199,30 @@ public class LocalDB {
.get(i) .get(i)
.getMetadata() .getMetadata()
.getMessageId()) { .getMessageId()) {
System.out.println("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
+ "was selected.");
getChats().get(j)
.getModel()
.get(k)
.getMetadata()
.setState(returnSync.getMessages().get(i).getMetadata().getState());
System.out.println( System.out.println(
"Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); "Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
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());
} }
} }
} }
} }
break;
}
} }
} }
// Updating UserStatus of all users in LocalDB // Updating UserStatus of all users in LocalDB
for (int j = 0; j < returnSync.getUsers().size(); j++) { for (User user : returnSync.getUsers())
for (int k = 0; k < getChats().size(); k++) { for (Chat chat : getChats())
if (getChats().get(k).getRecipient().getID() == returnSync.getUsers().get(j).getID()) { if (user.getID() == chat.getRecipient().getID()) {
chat.getRecipient().setStatus(user.getStatus());
getChats().get(k).getRecipient().setStatus(returnSync.getUsers().get(j).getStatus()); System.out.println(chat.getRecipient().getStatus());
System.out.println(getChats().get(k).getRecipient().getStatus().toString());
}
}
} }
sync.getMessages().clear(); sync.getMessages().clear();
@ -234,11 +237,11 @@ public class LocalDB {
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public void addUnreadMessagesToLocalDB() { public void addUnreadMessagesToLocalDB() {
Sync unreadMessages = unreadMessagesSync; for(Message message : unreadMessagesSync.getMessages())
for (int i = 0; i < unreadMessages.getMessages().size(); i++) for(Chat chat : getChats())
for (int j = 0; j < getChats().size(); j++) if(message.getMetadata().getSender() == chat.getRecipient().getID()) {
if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) { chat.appendMessage(message);
getChats().get(j).appendMessage(unreadMessages.getMessages().get(i)); break;
} }
} }
@ -283,8 +286,8 @@ public class LocalDB {
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 saved {@link Chat} objects that list the client user as the
* client * sender
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
**/ **/
public List<Chat> getChats() { return chats; } public List<Chat> getChats() { return chats; }