From 182a1900997a1328562efa56f4c804043de4fa56 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 3 Jul 2020 13:47:40 +0200 Subject: [PATCH] Implemented GroupMessage reading (sending the right event) Note: changed a lambda expression in LocalDB and not 100% sure if it is correct --- src/main/java/envoy/client/data/Chat.java | 33 +++++++++++++++---- src/main/java/envoy/client/data/LocalDB.java | 2 +- .../envoy/client/ui/controller/ChatScene.java | 2 +- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 7ca11ce..207fb1f 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -2,6 +2,7 @@ package envoy.client.data; import java.io.IOException; import java.io.Serializable; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -9,6 +10,7 @@ import java.util.Objects; import envoy.client.net.WriteProxy; import envoy.data.*; import envoy.data.Message.MessageStatus; +import envoy.event.GroupMessageStatusChange; import envoy.event.MessageStatusChange; /** @@ -27,18 +29,25 @@ import envoy.event.MessageStatusChange; public final class Chat implements Serializable { private final Contact recipient; + private final Contact sender; private final List messages = new ArrayList<>(); 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 + * @param sender the user who is logged in as sender on this instance of + * envoy.client * @since Envoy Client v0.1-alpha */ - public Chat(Contact recipient) { this.recipient = recipient; } + public Chat(Contact recipient, Contact sender) { + this.recipient = recipient; + this.sender = sender; + } @Override public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); } @@ -78,10 +87,22 @@ public final class Chat implements Serializable { public void read(WriteProxy writeProxy) throws IOException { for (int i = messages.size() - 1; i >= 0; --i) { final Message m = messages.get(i); - if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break; - else { - m.setStatus(MessageStatus.READ); - writeProxy.writeMessageStatusChange(new MessageStatusChange(m)); + if(m.getClass().equals(GroupMessage.class)) { + GroupMessage g = (GroupMessage) m; + if (g.getSenderID() != sender.getID()) { + if (g.getMemberStatuses().get(sender.getID()) == MessageStatus.READ) break; + else { + g.getMemberStatuses().replace(sender.getID(), MessageStatus.READ); + writeProxy.writeMessageStatusChange( + new GroupMessageStatusChange(g.getID(), MessageStatus.READ, LocalDateTime.now(), sender.getID())); + } + } + }else { + if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break; + else { + m.setStatus(MessageStatus.READ); + writeProxy.writeMessageStatusChange(new MessageStatusChange(m)); + } } } } diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index d8993f5..315b27d 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -82,7 +82,7 @@ public abstract class LocalDB { getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); }); // Create missing chats - user.getContacts().stream().filter(u -> !u.equals(user) && getChat(u.getID()).isEmpty()).map(Chat::new).forEach(chats::add); + user.getContacts().stream().filter(u -> !u.equals(user) && getChat(u.getID()).isEmpty()).map(u -> new Chat(u, user)).forEach(chats::add); } /** diff --git a/src/main/java/envoy/client/ui/controller/ChatScene.java b/src/main/java/envoy/client/ui/controller/ChatScene.java index a092234..c3aa608 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -146,7 +146,7 @@ public final class ChatScene { switch (e.getOperationType()) { case ADD: localDB.getUsers().put(contact.getName(), contact); - localDB.getChats().add(new Chat(contact)); + localDB.getChats().add(new Chat(contact, client.getSender())); Platform.runLater(() -> userList.getItems().add(contact)); break; case REMOVE: