From 73eb312a908a52db6c3fa401ab9e44896139423d Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 16 Nov 2019 08:06:07 +0100 Subject: [PATCH 1/4] Mark all newly received messages in the current chat as read Fixes #24 --- src/main/java/envoy/client/LocalDB.java | 95 ++++++------------- src/main/java/envoy/client/ui/ChatWindow.java | 12 ++- 2 files changed, 42 insertions(+), 65 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 142b687..29d7a5c 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -30,12 +30,17 @@ import envoy.schema.User; * @since Envoy v0.1-alpha */ public class LocalDB { + private File localDB; private User sender; private List chats = new ArrayList<>(); private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; + private Sync unreadMessagesSync = objectFactory.createSync(); + private Sync sync = objectFactory.createSync(); + private Sync readMessages = objectFactory.createSync(); + /** * Constructs an empty local database. * @@ -51,7 +56,6 @@ public class LocalDB { e.printStackTrace(); } } - /** * Initializes the local database and fills it with values @@ -62,8 +66,8 @@ public class LocalDB { * @since Envoy v0.1-alpha **/ public void initializeDBFile(File localDBDir) throws EnvoyException { - if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException( - String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); + if (localDBDir.exists() && !localDBDir.isDirectory()) + throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); localDB = new File(localDBDir, sender.getID() + ".db"); if (localDB.exists()) loadFromLocalDB(); } @@ -130,12 +134,7 @@ public class LocalDB { return message; } - private Sync unreadMessagesSync = objectFactory.createSync(); - public Sync sync = objectFactory.createSync(); - public Sync readMessages = objectFactory.createSync(); - public Sync fillSync(long userId) { - addWaitingMessagesToSync(); getSentStateMessagesFromLocalDB(); @@ -156,47 +155,32 @@ public class LocalDB { // 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()); + 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 + 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)); } - if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 - && returnSync.getMessages().get(i).getMetadata().getSender() == 0 + 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 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).getMetadata().getMessageId() == returnSync - .getMessages() + 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()); + getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState()); } } } @@ -210,26 +194,18 @@ public class 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()) { + 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() + if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages() .get(i) .getMetadata() .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("Message State is now: " - + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); + 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("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString()); } } } @@ -260,15 +236,7 @@ public class LocalDB { * @param message * @since Envoy v0.1-alpha */ - public void addMessageToSync(Message message) { sync.getMessages().add(message); } - - /** - * Adds a user to the {@code sync} {@link Sync} object. - * - * @param user - * @since Envoy v0.1-alpha - */ - public void addUserToSync(User user) { sync.getUsers().add(user); } + private void addMessageToSync(Message message) { sync.getMessages().add(message); } /** * Adds the unread messages returned from the server in the latest sync to the @@ -281,21 +249,19 @@ 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)); } } /** - * Gets all messages with state SENT from the LocalDB and adds them to the - * "sync" Sync object. + * Gets all messages with state {@code SENT} from the LocalDB and adds them to + * the {@code sync} {@link Sync} object. * * @param localDB * @since Envoy v0.1-alpha */ - public void getSentStateMessagesFromLocalDB() { + private void getSentStateMessagesFromLocalDB() { for (int i = 0; i < getChats().size(); i++) { for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) { @@ -306,9 +272,10 @@ public class LocalDB { } /** - * Changes all messages with State RECEIVED of a specific chat to State READ. + * Changes all messages with state {@code RECEIVED} of a specific chat to state + * {@code READ}. *
- * Adds these Messages to the {@code readMessages} {@link Sync} object. + * Adds these messages to the {@code readMessages} {@link Sync} object. * * @param currentChat * @since Envoy v0.1-alpha @@ -337,7 +304,7 @@ public class LocalDB { * @param localDB * @since Envoy v0.1-alpha */ - public void addWaitingMessagesToSync() { + private void addWaitingMessagesToSync() { for (int i = 0; i < getChats().size(); i++) { for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { @@ -364,7 +331,7 @@ public class LocalDB { public List getChats() { return chats; } /** - * @return the User who initialised the local Database + * @return the {@link User} who initialized the local database * @since Envoy v0.1-alpha */ public User getUser() { return sender; } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 2a7ae11..66b750f 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -223,7 +223,7 @@ public class ChatWindow extends JFrame { .get(); // Set all unread messages in the chat to read - if (currentChat != null) { localDB.setMessagesToRead(currentChat); } + readCurrentChat(); client.setRecipient(user); @@ -319,9 +319,14 @@ public class ChatWindow extends JFrame { // Synchronize localDB.applySync( client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID()))); + + // Process unread messages localDB.addUnreadMessagesToLocalDB(); localDB.clearUnreadMessagesSync(); + // Mark unread messages as read when they are in the current chat + readCurrentChat(); + // Update UI SwingUtilities .invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); }); @@ -335,4 +340,9 @@ public class ChatWindow extends JFrame { if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID()) userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus()); } + + /** + * Marks messages in the current chat as {@code READ}. + */ + private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } } \ No newline at end of file From eadccd71266f9c512261e8d1119ae7c6bc4cfe7e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 16 Nov 2019 08:23:04 +0100 Subject: [PATCH 2/4] Preventing sent messages from being added to the sync again Fixed #25 --- src/main/java/envoy/client/LocalDB.java | 45 +++---------------- src/main/java/envoy/client/ui/ChatWindow.java | 3 +- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index 29d7a5c..ace6881 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -137,10 +137,7 @@ 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)); - } + sync.getMessages().addAll(readMessages.getMessages()); readMessages.getMessages().clear(); System.out.println(sync.getMessages().size()); @@ -230,14 +227,6 @@ public class LocalDB { } - /** - * Adds a message to the "sync" Sync object. - * - * @param message - * @since Envoy v0.1-alpha - */ - private void addMessageToSync(Message message) { sync.getMessages().add(message); } - /** * Adds the unread messages returned from the server in the latest sync to the * right chats in the LocalDB. @@ -254,23 +243,6 @@ public class LocalDB { } } - /** - * Gets all messages with state {@code SENT} from the LocalDB and adds them to - * the {@code sync} {@link Sync} object. - * - * @param localDB - * @since Envoy v0.1-alpha - */ - private void getSentStateMessagesFromLocalDB() { - for (int i = 0; i < getChats().size(); i++) { - for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { - if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) { - addMessageToSync(getChats().get(i).getModel().get(j)); - } - } - } - } - /** * Changes all messages with state {@code RECEIVED} of a specific chat to state * {@code READ}. @@ -299,21 +271,18 @@ public class LocalDB { public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } /** - * Adds all messages with State WAITING from the {@link LocalDB} to the Sync. + * Adds all messages with state {@code WAITING} from the {@link LocalDB} to the + * {@link Sync} object. * - * @param localDB * @since Envoy v0.1-alpha */ private void addWaitingMessagesToSync() { - for (int i = 0; i < getChats().size(); i++) { - for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) { - if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) { - // addMessageToSync(localDB.getChats().get(i).getModel().get(j)); + for (Chat chat : getChats()) + for (int i = 0; i < chat.getModel().size(); i++) + if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) { System.out.println("Got Waiting Message"); - sync.getMessages().add(0, getChats().get(i).getModel().get(j)); + sync.getMessages().add(chat.getModel().get(i)); } - } - } } /** diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 66b750f..48d3bdc 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -306,7 +306,8 @@ public class ChatWindow extends JFrame { } /** - * Updates the data model and the ui every x seconds. + * Updates the data model and the UI every repeatedly after a certain amount of + * time. * * @param timeout the amount of time that passes between two requests sent to * the server From b657f2a516ef3799a2bbfbda0910359aedf67894 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 16 Nov 2019 10:47:32 +0100 Subject: [PATCH 3/4] Removed LocalDB#addWaitingMessageToLocalDB, fixed formatting --- src/main/java/envoy/client/LocalDB.java | 22 ++++--------------- src/main/java/envoy/client/ui/ChatWindow.java | 2 +- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index ace6881..3cc96a2 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -46,7 +46,7 @@ public class LocalDB { * * @param client the user that is logged in with this client * @since Envoy v0.1-alpha - **/ + */ public LocalDB(User sender) { this.sender = sender; @@ -64,7 +64,7 @@ public class LocalDB { * @param localDBDir the directory where we wish to save/load the database from. * @throws EnvoyException if the directory selected is not an actual directory. * @since Envoy v0.1-alpha - **/ + */ public void initializeDBFile(File localDBDir) throws EnvoyException { if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); @@ -77,7 +77,7 @@ public class LocalDB { * * @throws IOException if something went wrong during saving * @since Envoy v0.1-alpha - **/ + */ public void saveToLocalDB() { try { localDB.getParentFile().mkdirs(); @@ -99,7 +99,7 @@ public class LocalDB { * * @throws EnvoyException if something fails while loading. * @since Envoy v0.1-alpha - **/ + */ @SuppressWarnings("unchecked") private void loadFromLocalDB() throws EnvoyException { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { @@ -155,15 +155,12 @@ public class LocalDB { 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)); - } if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0 @@ -207,7 +204,6 @@ public class LocalDB { } } } - } } @@ -224,7 +220,6 @@ public class LocalDB { sync.getMessages().clear(); sync.getUsers().clear(); - } /** @@ -261,15 +256,6 @@ public class LocalDB { } else break; } - /** - * Adds a message with State WAITING to a specific chat in the LocalDB. - * - * @param message - * @param currentChat - * @since Envoy v0.1-alpha - */ - public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); } - /** * Adds all messages with state {@code WAITING} from the {@link LocalDB} to the * {@link Sync} object. diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 48d3bdc..b9aea74 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -269,7 +269,7 @@ public class ChatWindow extends JFrame { // Create and send message object final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient()); - localDB.addWaitingMessageToLocalDB(message, currentChat); + currentChat.appendMessage(message); messageList.setModel(currentChat.getModel()); // Clear text field From 5e4e1449d2a079d58d9b4306369f42d38963ab51 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 16 Nov 2019 14:16:27 +0100 Subject: [PATCH 4/4] updated Javadoc --- src/main/java/envoy/client/ui/ChatWindow.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index b9aea74..32b024b 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -306,7 +306,7 @@ public class ChatWindow extends JFrame { } /** - * Updates the data model and the UI every repeatedly after a certain amount of + * Updates the data model and the UI repeatedly after a certain amount of * time. * * @param timeout the amount of time that passes between two requests sent to @@ -346,4 +346,4 @@ public class ChatWindow extends JFrame { * Marks messages in the current chat as {@code READ}. */ private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } } -} \ No newline at end of file +}