add message timestamp

This commit is contained in:
Arnaud Vergnet 2020-12-18 10:47:29 +01:00
parent c4b2b55887
commit 73bf24fa81
6 changed files with 32 additions and 17 deletions

View file

@ -6,6 +6,7 @@ import fr.insa.clavardator.users.PeerUser;
import javafx.application.Platform;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
@ -44,10 +45,10 @@ public class ChatHistory {
// TODO remove after tests
CurrentUser currentUser = CurrentUser.getInstance();
history = new ArrayList<>();
history.add(new Message(user, currentUser, "Coucou toi"));
history.add(new Message(currentUser, user, "Coucou " + user.getUsername()));
history.add(new Message(user, currentUser, "oui"));
history.add(new Message(currentUser, user, "merci"));
history.add(new Message(user, currentUser, new Date(), "Coucou toi"));
history.add(new Message(currentUser, user, new Date(),"Coucou " + user.getUsername()));
history.add(new Message(user, currentUser, new Date(),"oui"));
history.add(new Message(currentUser, user, new Date(),"merci"));
// db.getChatHistory(new Date(), new Date(), // TODO: put actual date
// newHistory -> {
// history = newHistory;

View file

@ -5,6 +5,7 @@ import fr.insa.clavardator.users.User;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
public class FileMessage extends Message {
public static final long MAX_FILE_SIZE = 20 * 1024 * 1024; // 20 Mo
@ -12,8 +13,8 @@ public class FileMessage extends Message {
private final byte[] rawFile;
private final String fileName;
public FileMessage(User sender, User recipient, String filePath, String text) throws IOException {
super(sender, recipient, text);
public FileMessage(User sender, User recipient, String filePath, Date date, String text) throws IOException {
super(sender, recipient, date, text);
File file = new File(filePath);
if (!file.exists())

View file

@ -2,12 +2,14 @@ package fr.insa.clavardator.chat;
import fr.insa.clavardator.users.User;
import java.util.Date;
public class ImageMessage extends Message {
public ImageMessage(User sender, User recipient) {
super(sender, recipient);
public ImageMessage(User sender, User recipient, Date date) {
super(sender, recipient, date);
}
public ImageMessage(User sender, User recipient, String text) {
super(sender, recipient, text);
public ImageMessage(User sender, User recipient, Date date, String text) {
super(sender, recipient, date, text);
}
}

View file

@ -4,29 +4,33 @@ import fr.insa.clavardator.users.User;
import fr.insa.clavardator.users.UserInformation;
import java.io.Serializable;
import java.util.Date;
public class Message implements Serializable {
private final String text;
private final Date date;
private final UserInformation sender;
private final UserInformation recipient;
public Message(UserInformation sender, UserInformation recipient) {
this(sender, recipient, "");
public Message(UserInformation sender, UserInformation recipient, Date date) {
this(sender, recipient, date, "");
}
public Message(User sender, User recipient) {
this(sender, recipient, "");
public Message(User sender, User recipient, Date date) {
this(sender, recipient, date,"");
}
public Message(UserInformation sender, UserInformation recipient, String text) {
public Message(UserInformation sender, UserInformation recipient, Date date, String text) {
this.sender = sender;
this.recipient = recipient;
this.date = date;
this.text = text;
}
public Message(User sender, User recipient, String text) {
public Message(User sender, User recipient, Date date, String text) {
this.sender = new UserInformation(sender);
this.recipient = new UserInformation(recipient);
this.date = date;
this.text = text;
}
@ -41,4 +45,8 @@ public class Message implements Serializable {
public UserInformation getRecipient() {
return recipient;
}
public Date getDate() {
return date;
}
}

View file

@ -10,6 +10,7 @@ import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import java.net.URL;
import java.text.DateFormat;
import java.util.ResourceBundle;
public class MessageListItemController implements Initializable {
@ -28,6 +29,7 @@ public class MessageListItemController implements Initializable {
public void setMessage(Message message) {
button.setText(message.getText());
timestamp.setText(DateFormat.getTimeInstance().format(message.getDate()));
if (message.getSender().id == CurrentUser.getInstance().getId()) {
container.setAlignment(Pos.CENTER_RIGHT);
button.getStyleClass().add("message-self");

View file

@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.EOFException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
public class PeerUser extends User implements Comparable<PeerUser> {
protected transient ChatHistory history;
@ -72,7 +73,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
if (connection != null) {
Log.v(this.getClass().getSimpleName(),
"Sending message to " + this.getUsername() + " / " + this.getId() + ": " + msg);
final Message message = new Message(CurrentUser.getInstance(), this, msg);
final Message message = new Message(CurrentUser.getInstance(), this, new Date(), msg);
connection.send(
message,
() -> history.addMessage(message),