add message timestamp
This commit is contained in:
parent
c4b2b55887
commit
73bf24fa81
6 changed files with 32 additions and 17 deletions
|
@ -6,6 +6,7 @@ import fr.insa.clavardator.users.PeerUser;
|
||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
@ -44,10 +45,10 @@ public class ChatHistory {
|
||||||
// TODO remove after tests
|
// TODO remove after tests
|
||||||
CurrentUser currentUser = CurrentUser.getInstance();
|
CurrentUser currentUser = CurrentUser.getInstance();
|
||||||
history = new ArrayList<>();
|
history = new ArrayList<>();
|
||||||
history.add(new Message(user, currentUser, "Coucou toi"));
|
history.add(new Message(user, currentUser, new Date(), "Coucou toi"));
|
||||||
history.add(new Message(currentUser, user, "Coucou " + user.getUsername()));
|
history.add(new Message(currentUser, user, new Date(),"Coucou " + user.getUsername()));
|
||||||
history.add(new Message(user, currentUser, "oui"));
|
history.add(new Message(user, currentUser, new Date(),"oui"));
|
||||||
history.add(new Message(currentUser, user, "merci"));
|
history.add(new Message(currentUser, user, new Date(),"merci"));
|
||||||
// db.getChatHistory(new Date(), new Date(), // TODO: put actual date
|
// db.getChatHistory(new Date(), new Date(), // TODO: put actual date
|
||||||
// newHistory -> {
|
// newHistory -> {
|
||||||
// history = newHistory;
|
// history = newHistory;
|
||||||
|
|
|
@ -5,6 +5,7 @@ import fr.insa.clavardator.users.User;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class FileMessage extends Message {
|
public class FileMessage extends Message {
|
||||||
public static final long MAX_FILE_SIZE = 20 * 1024 * 1024; // 20 Mo
|
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 byte[] rawFile;
|
||||||
private final String fileName;
|
private final String fileName;
|
||||||
|
|
||||||
public FileMessage(User sender, User recipient, String filePath, String text) throws IOException {
|
public FileMessage(User sender, User recipient, String filePath, Date date, String text) throws IOException {
|
||||||
super(sender, recipient, text);
|
super(sender, recipient, date, text);
|
||||||
|
|
||||||
File file = new File(filePath);
|
File file = new File(filePath);
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
|
|
|
@ -2,12 +2,14 @@ package fr.insa.clavardator.chat;
|
||||||
|
|
||||||
import fr.insa.clavardator.users.User;
|
import fr.insa.clavardator.users.User;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class ImageMessage extends Message {
|
public class ImageMessage extends Message {
|
||||||
public ImageMessage(User sender, User recipient) {
|
public ImageMessage(User sender, User recipient, Date date) {
|
||||||
super(sender, recipient);
|
super(sender, recipient, date);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageMessage(User sender, User recipient, String text) {
|
public ImageMessage(User sender, User recipient, Date date, String text) {
|
||||||
super(sender, recipient, text);
|
super(sender, recipient, date, text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,29 +4,33 @@ import fr.insa.clavardator.users.User;
|
||||||
import fr.insa.clavardator.users.UserInformation;
|
import fr.insa.clavardator.users.UserInformation;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class Message implements Serializable {
|
public class Message implements Serializable {
|
||||||
private final String text;
|
private final String text;
|
||||||
|
private final Date date;
|
||||||
private final UserInformation sender;
|
private final UserInformation sender;
|
||||||
private final UserInformation recipient;
|
private final UserInformation recipient;
|
||||||
|
|
||||||
public Message(UserInformation sender, UserInformation recipient) {
|
public Message(UserInformation sender, UserInformation recipient, Date date) {
|
||||||
this(sender, recipient, "");
|
this(sender, recipient, date, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Message(User sender, User recipient) {
|
public Message(User sender, User recipient, Date date) {
|
||||||
this(sender, recipient, "");
|
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.sender = sender;
|
||||||
this.recipient = recipient;
|
this.recipient = recipient;
|
||||||
|
this.date = date;
|
||||||
this.text = text;
|
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.sender = new UserInformation(sender);
|
||||||
this.recipient = new UserInformation(recipient);
|
this.recipient = new UserInformation(recipient);
|
||||||
|
this.date = date;
|
||||||
this.text = text;
|
this.text = text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,4 +45,8 @@ public class Message implements Serializable {
|
||||||
public UserInformation getRecipient() {
|
public UserInformation getRecipient() {
|
||||||
return recipient;
|
return recipient;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import javafx.scene.control.Label;
|
||||||
import javafx.scene.layout.VBox;
|
import javafx.scene.layout.VBox;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.text.DateFormat;
|
||||||
import java.util.ResourceBundle;
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
public class MessageListItemController implements Initializable {
|
public class MessageListItemController implements Initializable {
|
||||||
|
@ -28,6 +29,7 @@ public class MessageListItemController implements Initializable {
|
||||||
|
|
||||||
public void setMessage(Message message) {
|
public void setMessage(Message message) {
|
||||||
button.setText(message.getText());
|
button.setText(message.getText());
|
||||||
|
timestamp.setText(DateFormat.getTimeInstance().format(message.getDate()));
|
||||||
if (message.getSender().id == CurrentUser.getInstance().getId()) {
|
if (message.getSender().id == CurrentUser.getInstance().getId()) {
|
||||||
container.setAlignment(Pos.CENTER_RIGHT);
|
container.setAlignment(Pos.CENTER_RIGHT);
|
||||||
button.getStyleClass().add("message-self");
|
button.getStyleClass().add("message-self");
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
public class PeerUser extends User implements Comparable<PeerUser> {
|
public class PeerUser extends User implements Comparable<PeerUser> {
|
||||||
protected transient ChatHistory history;
|
protected transient ChatHistory history;
|
||||||
|
@ -72,7 +73,7 @@ public class PeerUser extends User implements Comparable<PeerUser> {
|
||||||
if (connection != null) {
|
if (connection != null) {
|
||||||
Log.v(this.getClass().getSimpleName(),
|
Log.v(this.getClass().getSimpleName(),
|
||||||
"Sending message to " + this.getUsername() + " / " + this.getId() + ": " + msg);
|
"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(
|
connection.send(
|
||||||
message,
|
message,
|
||||||
() -> history.addMessage(message),
|
() -> history.addMessage(message),
|
||||||
|
|
Loading…
Reference in a new issue