From dd8e88d28cb5d49789c5e234e4f9578590331771 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sun, 5 Jul 2020 14:38:19 +0200 Subject: [PATCH] Implemented GroupChat as subclass of Chat and adjusted calls of such --- src/main/java/envoy/client/data/Chat.java | 34 +++---------- .../java/envoy/client/data/GroupChat.java | 51 +++++++++++++++++++ src/main/java/envoy/client/data/LocalDB.java | 6 ++- .../envoy/client/ui/controller/ChatScene.java | 6 +-- 4 files changed, 66 insertions(+), 31 deletions(-) create mode 100644 src/main/java/envoy/client/data/GroupChat.java diff --git a/src/main/java/envoy/client/data/Chat.java b/src/main/java/envoy/client/data/Chat.java index 318cf24..739d335 100644 --- a/src/main/java/envoy/client/data/Chat.java +++ b/src/main/java/envoy/client/data/Chat.java @@ -2,7 +2,6 @@ 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; @@ -10,7 +9,6 @@ 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; /** @@ -26,11 +24,10 @@ import envoy.event.MessageStatusChange; * @author Kai S. K. Engelbart * @since Envoy Client v0.1-alpha */ -public final class Chat implements Serializable { +public class Chat implements Serializable { - private final Contact recipient; - private final Contact sender; - private final List messages = new ArrayList<>(); + protected final Contact recipient; + protected final List messages = new ArrayList<>(); private static final long serialVersionUID = 1L; @@ -40,13 +37,10 @@ public final class Chat implements Serializable { * 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, Contact sender) { + public Chat(Contact recipient) { this.recipient = recipient; - this.sender = sender; } @Override @@ -87,22 +81,10 @@ 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.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)); - } + 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/GroupChat.java b/src/main/java/envoy/client/data/GroupChat.java new file mode 100644 index 0000000..4f29eb6 --- /dev/null +++ b/src/main/java/envoy/client/data/GroupChat.java @@ -0,0 +1,51 @@ +package envoy.client.data; + +import java.io.IOException; +import java.time.LocalDateTime; + +import envoy.client.net.WriteProxy; +import envoy.data.Contact; +import envoy.data.GroupMessage; +import envoy.data.Message.MessageStatus; +import envoy.data.User; +import envoy.event.GroupMessageStatusChange; + +/** + * Project: envoy-client
+ * File: GroupChat.java
+ * Created: 05.07.2020
+ * + * @author Maximilian Käfer + * @since Envoy Client v0.1-beta + */ +public class GroupChat extends Chat { + + private final User sender; + + private static final long serialVersionUID = 1L; + + /** + * @param sender the user sending the messages + * @param recipient the user who receives the messages + * @since Envoy Client v0.1-beta + */ + public GroupChat(User sender, Contact recipient) { + super(recipient); + this.sender = sender; + } + + @Override + public void read(WriteProxy writeProxy) throws IOException { + for (int i = messages.size() - 1; i >= 0; --i) { + final GroupMessage g = (GroupMessage) messages.get(i); + 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())); + } + } + } + } +} diff --git a/src/main/java/envoy/client/data/LocalDB.java b/src/main/java/envoy/client/data/LocalDB.java index 315b27d..7899510 100644 --- a/src/main/java/envoy/client/data/LocalDB.java +++ b/src/main/java/envoy/client/data/LocalDB.java @@ -82,7 +82,11 @@ 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(u -> new Chat(u, user)).forEach(chats::add); + user.getContacts() + .stream() + .filter(c -> !c.equals(user) && getChat(c.getID()).isEmpty()) + .map(c -> c instanceof User ? new Chat(c) : new GroupChat(user, c)) + .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 d89a28c..68449b4 100644 --- a/src/main/java/envoy/client/ui/controller/ChatScene.java +++ b/src/main/java/envoy/client/ui/controller/ChatScene.java @@ -17,9 +17,7 @@ import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.Color; -import envoy.client.data.Chat; -import envoy.client.data.LocalDB; -import envoy.client.data.Settings; +import envoy.client.data.*; import envoy.client.event.MessageCreationEvent; import envoy.client.net.Client; import envoy.client.net.WriteProxy; @@ -143,7 +141,7 @@ public final class ChatScene implements Restorable { switch (e.getOperationType()) { case ADD: localDB.getUsers().put(contact.getName(), contact); - localDB.getChats().add(new Chat(contact, client.getSender())); + localDB.getChats().add(contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact)); Platform.runLater(() -> userList.getItems().add(contact)); break; case REMOVE: