From bd1563b439fcaefd34c91e84088d599fc90b7771 Mon Sep 17 00:00:00 2001 From: Haramus Samsamus Date: Sat, 14 Mar 2020 16:58:19 +0100 Subject: [PATCH] Fixed state errors in offline mode (#116) * Display all contacts as offline while in offline mode * Update message status to sent after relaying message cache --- src/main/java/envoy/client/data/LocalDb.java | 19 +++++++++- .../java/envoy/client/net/WriteProxy.java | 3 ++ src/main/java/envoy/client/ui/ChatWindow.java | 38 ++++++++----------- src/main/java/envoy/client/ui/Startup.java | 10 ++++- 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/main/java/envoy/client/data/LocalDb.java b/src/main/java/envoy/client/data/LocalDb.java index 8034e2a..dd1db3a 100644 --- a/src/main/java/envoy/client/data/LocalDb.java +++ b/src/main/java/envoy/client/data/LocalDb.java @@ -21,8 +21,8 @@ import envoy.event.MessageStatusChangeEvent; public abstract class LocalDb { protected User user; - protected Map users = new HashMap<>(); - protected List chats = new ArrayList<>(); + protected Map users = new HashMap<>(); + protected List chats = new ArrayList<>(); protected IdGenerator idGenerator; protected Cache messageCache = new Cache<>(); protected Cache statusCache = new Cache<>(); @@ -143,4 +143,19 @@ public abstract class LocalDb { * @since Envoy v0.3-alpha */ public void setStatusCache(Cache statusCache) { this.statusCache = statusCache; } + + /** + * Searches for a message by ID. + * + * @param id the ID of the message to search for + * @return the message with the corresponding ID, or {@code null} if no message + * has been found + * @since Envoy v0.1-beta + */ + public Message getMessage(long id) { + for (Chat c : chats) + for (Message m : c.getModel()) + if (m.getId() == id) return m; + return null; + } } diff --git a/src/main/java/envoy/client/net/WriteProxy.java b/src/main/java/envoy/client/net/WriteProxy.java index 7d25bf1..0ba5110 100644 --- a/src/main/java/envoy/client/net/WriteProxy.java +++ b/src/main/java/envoy/client/net/WriteProxy.java @@ -47,6 +47,9 @@ public class WriteProxy { try { logger.finer("Sending cached " + msg); client.sendMessage(msg); + + // Update message state to SENT in local db + localDb.getMessage(msg.getId()).nextStatus(); } catch (IOException e) { logger.log(Level.SEVERE, "Could not send cached message", e); } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 70fe5f9..bfafc06 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -522,28 +522,6 @@ public class ChatWindow extends JFrame { } } - /** - * Initializes the elements of the user list by downloading them from the - * server. - * - * @since Envoy v0.1-alpha - */ - private void loadUsersAndChats() { - new Thread(() -> { - localDb.getUsers().values().forEach(user -> { - userListModel.addElement(user); - - // Check if user exists in local DB - if (localDb.getChats().stream().filter(c -> c.getRecipient().getId() == user.getId()).count() == 0) - localDb.getChats().add(new Chat(user)); - }); - SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); - - revalidate(); - repaint(); - }).start(); - } - private void readCurrentChat() { try { currentChat.read(writeProxy); @@ -590,7 +568,21 @@ public class ChatWindow extends JFrame { this.client = client; this.localDb = localDb; this.writeProxy = writeProxy; - loadUsersAndChats(); + + // Load users and chats + new Thread(() -> { + localDb.getUsers().values().forEach(user -> { + userListModel.addElement(user); + + // Check if user exists in local DB + if (localDb.getChats().stream().noneMatch(c -> c.getRecipient().getId() == user.getId())) + localDb.getChats().add(new Chat(user)); + }); + SwingUtilities.invokeLater(() -> userList.setModel(userListModel)); + + revalidate(); + repaint(); + }).start(); } /** diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 8202941..98a8ffc 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -17,6 +17,7 @@ import envoy.client.net.Client; import envoy.client.net.WriteProxy; import envoy.data.Config; import envoy.data.Message; +import envoy.data.User.UserStatus; import envoy.exception.EnvoyException; import envoy.util.EnvoyLog; @@ -113,7 +114,7 @@ public class Startup { } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, - "Error while loading local database: " + e + "\nChats might not be stored locally.", + "Error while loading local database: " + e + "\nChats will not be stored locally.", "Local DB error", JOptionPane.WARNING_MESSAGE); } @@ -121,10 +122,15 @@ public class Startup { // Initialize write proxy final WriteProxy writeProxy = client.createWriteProxy(localDb); - // Save all users to the local database and flush cache if (client.isOnline()) { + + // Save all users to the local database and flush cache localDb.setUsers(client.getUsers()); writeProxy.flushCache(); + } else { + + // Set all contacts to offline mode + localDb.getUsers().values().stream().filter(u -> u != localDb.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE)); } // Display ChatWindow and StatusTrayIcon