2020-02-05 20:58:30 +01:00
|
|
|
package envoy.client.data;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-02-05 07:09:25 +01:00
|
|
|
import java.io.IOException;
|
2019-12-21 21:23:19 +01:00
|
|
|
import java.io.Serializable;
|
|
|
|
|
2020-02-06 21:28:02 +01:00
|
|
|
import envoy.client.net.WriteProxy;
|
2020-01-26 22:10:15 +01:00
|
|
|
import envoy.client.ui.list.ComponentListModel;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.Message;
|
2020-02-01 10:20:06 +01:00
|
|
|
import envoy.data.Message.MessageStatus;
|
2019-12-28 21:20:43 +01:00
|
|
|
import envoy.data.User;
|
2020-02-05 07:09:25 +01:00
|
|
|
import envoy.event.MessageStatusChangeEvent;
|
2019-12-21 21:23:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a chat between two {@link User}s <br>
|
|
|
|
* as a list of {@link Message} objects.
|
|
|
|
* <br>
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Chat.java</strong><br>
|
|
|
|
* Created: <strong>19 Oct 2019</strong><br>
|
|
|
|
*
|
|
|
|
* @author Maximilian Käfer
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
public class Chat implements Serializable {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -7751248474547242056L;
|
|
|
|
|
2020-02-04 19:13:31 +01:00
|
|
|
private final User recipient;
|
|
|
|
private final ComponentListModel<Message> model = new ComponentListModel<>();
|
2019-12-21 21:23:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides the list of messages that the recipient receives.<br>
|
|
|
|
* Saves the Messages in the corresponding chat at that Point.
|
|
|
|
*
|
|
|
|
* @param recipient the user who receives the messages
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
public Chat(User recipient) { this.recipient = recipient; }
|
|
|
|
|
|
|
|
/**
|
2020-02-01 10:20:06 +01:00
|
|
|
* Appends a message to the bottom of this chat
|
|
|
|
*
|
|
|
|
* @param message the message to append
|
2019-12-21 21:23:19 +01:00
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2020-02-01 10:20:06 +01:00
|
|
|
public void appendMessage(Message message) { model.add(message); }
|
2019-12-21 21:23:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-02-03 06:57:19 +01:00
|
|
|
* Sets the status of all chat messages received from the recipient to
|
|
|
|
* {@code READ} starting from the bottom and stopping once a read message is
|
|
|
|
* found.
|
|
|
|
*
|
2020-02-06 21:28:02 +01:00
|
|
|
* @param writeProxy the write proxy instance used to notify the server about
|
|
|
|
* the message status changes
|
2020-02-05 07:09:25 +01:00
|
|
|
* @throws IOException if a {@link MessageStatusChangeEvent} could not be
|
|
|
|
* delivered to the server
|
2020-02-01 10:20:06 +01:00
|
|
|
* @since Envoy v0.3-alpha
|
2019-12-21 21:23:19 +01:00
|
|
|
*/
|
2020-02-06 21:28:02 +01:00
|
|
|
public void read(WriteProxy writeProxy) throws IOException {
|
2020-02-05 07:09:25 +01:00
|
|
|
for (int i = model.size() - 1; i >= 0; --i) {
|
|
|
|
final Message m = model.get(i);
|
|
|
|
if (m.getSenderId() == recipient.getId()) {
|
|
|
|
if (m.getStatus() == MessageStatus.READ) break;
|
|
|
|
else {
|
|
|
|
m.setStatus(MessageStatus.READ);
|
2020-02-06 21:28:02 +01:00
|
|
|
writeProxy.writeMessageStatusChangeEvent(new MessageStatusChangeEvent(m));
|
2020-02-05 07:09:25 +01:00
|
|
|
}
|
2020-02-03 06:57:19 +01:00
|
|
|
}
|
2020-02-05 07:09:25 +01:00
|
|
|
}
|
2020-02-01 10:20:06 +01:00
|
|
|
}
|
2019-12-21 21:23:19 +01:00
|
|
|
|
2020-02-03 22:06:56 +01:00
|
|
|
/**
|
|
|
|
* @return {@code true} if the newest message received in the chat doesn't have
|
|
|
|
* the status {@code READ}
|
|
|
|
* @since Envoy v0.3-alpha
|
|
|
|
*/
|
|
|
|
public boolean isUnread() { return !model.isEmpty() && model.get(model.size() - 1).getStatus() != MessageStatus.READ; }
|
|
|
|
|
2019-12-21 21:23:19 +01:00
|
|
|
/**
|
|
|
|
* @return all messages in the current chat
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
2020-01-26 22:10:15 +01:00
|
|
|
public ComponentListModel<Message> getModel() { return model; }
|
2020-02-01 10:20:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the recipient of a message
|
|
|
|
* @since Envoy v0.1-alpha
|
|
|
|
*/
|
|
|
|
public User getRecipient() { return recipient; }
|
2019-10-19 12:10:52 +02:00
|
|
|
}
|