Implemented GroupChat as subclass of Chat and adjusted calls of such

This commit is contained in:
DieGurke 2020-07-05 14:38:19 +02:00
parent 85e7f28839
commit dd8e88d28c
4 changed files with 66 additions and 31 deletions

View File

@ -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<Message> messages = new ArrayList<>();
protected final Contact recipient;
protected final List<Message> 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));
}
}
}

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>GroupChat.java</strong><br>
* Created: <strong>05.07.2020</strong><br>
*
* @author Maximilian K&auml;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()));
}
}
}
}
}

View File

@ -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);
}
/**

View File

@ -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: