Implemented GroupChat as subclass of Chat and adjusted calls of such
This commit is contained in:
parent
85e7f28839
commit
dd8e88d28c
@ -2,7 +2,6 @@ package envoy.client.data;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -10,7 +9,6 @@ import java.util.Objects;
|
|||||||
import envoy.client.net.WriteProxy;
|
import envoy.client.net.WriteProxy;
|
||||||
import envoy.data.*;
|
import envoy.data.*;
|
||||||
import envoy.data.Message.MessageStatus;
|
import envoy.data.Message.MessageStatus;
|
||||||
import envoy.event.GroupMessageStatusChange;
|
|
||||||
import envoy.event.MessageStatusChange;
|
import envoy.event.MessageStatusChange;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -26,11 +24,10 @@ import envoy.event.MessageStatusChange;
|
|||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy Client v0.1-alpha
|
* @since Envoy Client v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public final class Chat implements Serializable {
|
public class Chat implements Serializable {
|
||||||
|
|
||||||
private final Contact recipient;
|
protected final Contact recipient;
|
||||||
private final Contact sender;
|
protected final List<Message> messages = new ArrayList<>();
|
||||||
private final List<Message> messages = new ArrayList<>();
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
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.
|
* Saves the Messages in the corresponding chat at that Point.
|
||||||
*
|
*
|
||||||
* @param recipient the user who receives the messages
|
* @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
|
* @since Envoy Client v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public Chat(Contact recipient, Contact sender) {
|
public Chat(Contact recipient) {
|
||||||
this.recipient = recipient;
|
this.recipient = recipient;
|
||||||
this.sender = sender;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -87,17 +81,6 @@ public final class Chat implements Serializable {
|
|||||||
public void read(WriteProxy writeProxy) throws IOException {
|
public void read(WriteProxy writeProxy) throws IOException {
|
||||||
for (int i = messages.size() - 1; i >= 0; --i) {
|
for (int i = messages.size() - 1; i >= 0; --i) {
|
||||||
final Message m = messages.get(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;
|
if (m.getSenderID() == recipient.getID()) if (m.getStatus() == MessageStatus.READ) break;
|
||||||
else {
|
else {
|
||||||
m.setStatus(MessageStatus.READ);
|
m.setStatus(MessageStatus.READ);
|
||||||
@ -105,7 +88,6 @@ public final class Chat implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return {@code true} if the newest message received in the chat doesn't have
|
* @return {@code true} if the newest message received in the chat doesn't have
|
||||||
|
51
src/main/java/envoy/client/data/GroupChat.java
Normal file
51
src/main/java/envoy/client/data/GroupChat.java
Normal 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ä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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -82,7 +82,11 @@ public abstract class LocalDB {
|
|||||||
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
|
getChat(contact.getID()).ifPresent(chat -> { ((User) chat.getRecipient()).setStatus(((User) contact).getStatus()); });
|
||||||
|
|
||||||
// Create missing chats
|
// 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -17,9 +17,7 @@ import javafx.scene.input.KeyCode;
|
|||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
import javafx.scene.paint.Color;
|
import javafx.scene.paint.Color;
|
||||||
|
|
||||||
import envoy.client.data.Chat;
|
import envoy.client.data.*;
|
||||||
import envoy.client.data.LocalDB;
|
|
||||||
import envoy.client.data.Settings;
|
|
||||||
import envoy.client.event.MessageCreationEvent;
|
import envoy.client.event.MessageCreationEvent;
|
||||||
import envoy.client.net.Client;
|
import envoy.client.net.Client;
|
||||||
import envoy.client.net.WriteProxy;
|
import envoy.client.net.WriteProxy;
|
||||||
@ -143,7 +141,7 @@ public final class ChatScene implements Restorable {
|
|||||||
switch (e.getOperationType()) {
|
switch (e.getOperationType()) {
|
||||||
case ADD:
|
case ADD:
|
||||||
localDB.getUsers().put(contact.getName(), contact);
|
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));
|
Platform.runLater(() -> userList.getItems().add(contact));
|
||||||
break;
|
break;
|
||||||
case REMOVE:
|
case REMOVE:
|
||||||
|
Reference in New Issue
Block a user