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 dca65df9bd
commit 50945a45a2

View File

@ -147,80 +147,83 @@ 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) {
// Update Local Messages with State WAITING (add Message ID and change State to
// SENT)
for (int j = 0; j < sync.getMessages().size(); j++) {
if (j == i) {
sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
sync.getMessages().get(j).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));
// Create and dispatch message creation event // The message has an ID
EventBus.getInstance().dispatch(new MessageCreationEvent(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 // Messages are processes differently corresponding to their state
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) { switch (returnSync.getMessages().get(i).getMetadata().getState()) {
// Update Messages in localDB to state RECEIVED case SENT:
for (int j = 0; j < getChats().size(); j++) { // Update previously waiting and now sent messages that were assigned an ID by
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { // the server
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { sync.getMessages().get(i).getMetadata().setMessageId(returnSync.getMessages().get(i).getMetadata().getMessageId());
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages() sync.getMessages().get(i).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
.get(i) break;
.getMetadata() case RECEIVED:
.getMessageId()) { if (returnSync.getMessages().get(i).getMetadata().getSender() != 0) {
// Update Message in LocalDB // these are the unread Messages from the server
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState()); unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
// Create and dispatch message creation event
EventBus.getInstance().dispatch(new MessageCreationEvent(returnSync.getMessages().get(i)));
} else {
// 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()) {
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
getChats().get(j)
.getModel()
.get(k)
.getMetadata()
.setState(returnSync.getMessages().get(j).getMetadata().getState());
}
}
}
} }
} }
} break;
} case 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.");
if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 for (int j = 0; j < getChats().size(); j++) {
&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.READ) { if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
// Update local Messages to state READ System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId() for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
+ "was initialized to be set to READ in localDB."); if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
for (int j = 0; j < getChats().size(); j++) { .get(i)
if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) { .getMetadata()
System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected."); .getMessageId()) {
for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) { System.out.println("Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId()
if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages() + "was selected.");
.get(i) getChats().get(j)
.getMetadata() .getModel()
.getMessageId()) { .get(k)
System.out.println( .getMetadata()
"Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected."); .setState(returnSync.getMessages().get(i).getMetadata().getState());
getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState()); System.out.println(
System.out "Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
.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();
sync.getUsers().clear(); sync.getUsers().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; }