From 26a8650353d28805746e47558297a4c00b10893a Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 1 Jul 2020 08:36:21 +0200 Subject: [PATCH 1/3] Fix message order with insertion method Fixes #168 --- src/main/java/envoy/client/data/Chat.java | 14 ++++++++++++++ .../java/envoy/client/ui/controller/ChatScene.java | 2 +- .../envoy/client/ui/controller/LoginScene.java | 3 +-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 7ca11ce..f26fa15 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -93,6 +93,20 @@ public final class Chat implements Serializable { */ public boolean isUnread() { return !messages.isEmpty() && messages.get(messages.size() - 1).getStatus() != MessageStatus.READ; } + /** + * Inserts a message at the correct place according to its creation date. + * + * @param message the message to insert + * @since Envoy Client v0.1-beta + */ + public void insert(Message message) { + for (int i = messages.size() - 1; i >= 0; --i) + if (message.getCreationDate().isAfter(messages.get(i).getCreationDate())) { + messages.add(i + 1, message); + break; + } + } + /** * @return all messages in the current chat * @since Envoy Client v0.1-beta diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index c27a857..e31ebcb 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -101,7 +101,7 @@ public final class ChatScene { eventBus.register(MessageCreationEvent.class, e -> { final var message = e.get(); localDB.getChat(message.getSenderID()).ifPresent(chat -> { - chat.getMessages().add(message); + chat.insert(message); if (chat.equals(currentChat)) { try { diff --git a/src/main/java/envoy/client/ui/controller/LoginScene.java b/src/main/java/envoy/client/ui/controller/LoginScene.java index 380842a..3ec2338 100644 --- a/src/main/java/envoy/client/ui/controller/LoginScene.java +++ b/src/main/java/envoy/client/ui/controller/LoginScene.java @@ -151,8 +151,7 @@ public final class LoginScene { loadChatScene(); } } catch (IOException | InterruptedException | TimeoutException e) { - logger.log(Level.WARNING, "Could not connect to server: ", e); - logger.log(Level.FINER, "Attempting offline mode..."); + logger.log(Level.INFO, "Could not connect to server. Entering offline mode..."); attemptOfflineMode(credentials); } } From c54e7085ced220fe6e7cf0763e0e0430e3d840a6 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 1 Jul 2020 08:45:39 +0200 Subject: [PATCH 2/3] Fix message insertion into empty list and from sender --- src/main/java/envoy/client/data/Chat.java | 6 ++++-- src/main/java/envoy/client/ui/controller/ChatScene.java | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index f26fa15..51c2b1b 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -32,7 +32,8 @@ public final class Chat implements Serializable { private static final long serialVersionUID = 1L; /** - * Provides the list of messages that the recipient receives.

+ * Provides the list of messages that the recipient receives. + *

* Saves the Messages in the corresponding chat at that Point. * * @param recipient the user who receives the messages @@ -100,7 +101,8 @@ public final class Chat implements Serializable { * @since Envoy Client v0.1-beta */ public void insert(Message message) { - for (int i = messages.size() - 1; i >= 0; --i) + if (messages.isEmpty()) messages.add(message); + else for (int i = messages.size() - 1; i >= 0; --i) if (message.getCreationDate().isAfter(messages.get(i).getCreationDate())) { messages.add(i + 1, message); break; diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index e31ebcb..38f3aa1 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -317,7 +317,8 @@ public final class ChatScene { writeProxy.writeMessage(message); // Add message to LocalDB and update UI - messageList.getItems().add(message); + currentChat.insert(message); + messageList.refresh(); scrollToMessageListEnd(); // Request a new ID generator if all IDs were used From 7f536eacc6df25d2db0a9a230782a04b41d23462 Mon Sep 17 00:00:00 2001 From: kske Date: Wed, 1 Jul 2020 08:58:02 +0200 Subject: [PATCH 3/3] Fix insertion of oldest message --- src/main/java/envoy/client/data/Chat.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 51c2b1b..1eeed6a 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -101,12 +101,12 @@ public final class Chat implements Serializable { * @since Envoy Client v0.1-beta */ public void insert(Message message) { - if (messages.isEmpty()) messages.add(message); - else for (int i = messages.size() - 1; i >= 0; --i) + for (int i = messages.size() - 1; i >= 0; --i) if (message.getCreationDate().isAfter(messages.get(i).getCreationDate())) { messages.add(i + 1, message); - break; + return; } + messages.add(0, message); } /**