Removed old sync thread, implemented chat reading
This commit is contained in:
parent
9323b0878f
commit
ab6d27651e
@ -4,6 +4,7 @@ import java.io.Serializable;
|
||||
|
||||
import envoy.client.ui.list.ComponentListModel;
|
||||
import envoy.data.Message;
|
||||
import envoy.data.Message.MessageStatus;
|
||||
import envoy.data.User;
|
||||
|
||||
/**
|
||||
@ -36,22 +37,34 @@ public class Chat implements Serializable {
|
||||
public Chat(User recipient) { this.recipient = recipient; }
|
||||
|
||||
/**
|
||||
* @return the recipient of a message
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public User getRecipient() { return recipient; }
|
||||
|
||||
/**
|
||||
* Adds the received message at the current Point in the current chat
|
||||
* Appends a message to the bottom of this chat
|
||||
*
|
||||
* @param message the message to add in said chat
|
||||
* @param message the message to append
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public void appendMessage(Message message) { model.add(message); }
|
||||
|
||||
/**
|
||||
* Sets the status of all chat messages to {@code READ} starting from the bottom
|
||||
* and stopping once a read message is found.
|
||||
*
|
||||
* @since Envoy v0.3-alpha
|
||||
*/
|
||||
public void read() {
|
||||
for (int i = model.size() - 1; i >= 0; --i)
|
||||
if (model.get(i).getStatus() == MessageStatus.READ) break;
|
||||
else model.get(i).setStatus(MessageStatus.READ);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return all messages in the current chat
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public ComponentListModel<Message> getModel() { return model; }
|
||||
|
||||
/**
|
||||
* @return the recipient of a message
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public User getRecipient() { return recipient; }
|
||||
}
|
@ -31,7 +31,6 @@ public class Config {
|
||||
items.put("server", new ConfigItem<>("server", "s", (input) -> input, null));
|
||||
items.put("port", new ConfigItem<>("port", "p", (input) -> Integer.parseInt(input), null));
|
||||
items.put("localDB", new ConfigItem<>("localDB", "db", (input) -> new File(input), new File("localDB")));
|
||||
items.put("syncTimeout", new ConfigItem<>("syncTimeout", "st", (input) -> Integer.parseInt(input), 1000));
|
||||
items.put("homeDirectory",
|
||||
new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy")));
|
||||
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", (input) -> Level.parse(input), Level.CONFIG));
|
||||
@ -112,7 +111,7 @@ public class Config {
|
||||
* @return the port at which the Envoy server is located on the host
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public int getPort() { return (int) items.get("port").get(); }
|
||||
public Integer getPort() { return (Integer) items.get("port").get(); }
|
||||
|
||||
/**
|
||||
* @return the local database specific to the client user
|
||||
@ -120,12 +119,6 @@ public class Config {
|
||||
*/
|
||||
public File getLocalDB() { return (File) items.get("localDB").get(); }
|
||||
|
||||
/**
|
||||
* @return the current time (milliseconds) that is waited between Syncs
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public int getSyncTimeout() { return (int) items.get("syncTimeout").get(); }
|
||||
|
||||
/**
|
||||
* @return the directory in which all local files are saves
|
||||
* @since Envoy v0.2-alpha
|
||||
|
@ -135,7 +135,7 @@ public class Settings {
|
||||
* {@code Control} key.
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public boolean isEnterToSend() { return (boolean) items.get("enterToSend").get(); }
|
||||
public Boolean isEnterToSend() { return (Boolean) items.get("enterToSend").get(); }
|
||||
|
||||
/**
|
||||
* Changes the keystrokes performed by the user to send a message.
|
||||
@ -152,7 +152,7 @@ public class Settings {
|
||||
* @return the current on close mode.
|
||||
* @since Envoy v0.3-alpha
|
||||
*/
|
||||
public boolean getCurrentOnCloseMode() { return (boolean) items.get("onCloseMode").get(); }
|
||||
public Boolean getCurrentOnCloseMode() { return (Boolean) items.get("onCloseMode").get(); }
|
||||
|
||||
/**
|
||||
* Sets the current on close mode.
|
||||
|
@ -169,7 +169,7 @@ public class ChatWindow extends JFrame {
|
||||
currentChat = localDB.getChats().stream().filter(chat -> chat.getRecipient().getId() == user.getId()).findFirst().get();
|
||||
|
||||
// Set all unread messages in the chat to read
|
||||
readCurrentChat();
|
||||
currentChat.read();
|
||||
|
||||
client.setRecipient(user);
|
||||
textPane.setText(currentChat.getRecipient().getName());
|
||||
@ -204,9 +204,11 @@ public class ChatWindow extends JFrame {
|
||||
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
|
||||
Message message = ((MessageCreationEvent) evt).get();
|
||||
localDB.getChats().stream().filter(c -> c.getRecipient().getId() == message.getRecipientId()).findFirst().get().appendMessage(message);
|
||||
revalidate();
|
||||
repaint();
|
||||
});
|
||||
|
||||
contentPane.revalidate();
|
||||
revalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -309,64 +311,13 @@ public class ChatWindow extends JFrame {
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the data model and the UI repeatedly after a certain amount of
|
||||
* time.
|
||||
*
|
||||
* @param timeout the amount of time that passes between two requests sent to
|
||||
* the server
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void startSyncThread(int timeout) {
|
||||
new Timer(timeout, (evt) -> {
|
||||
new Thread(() -> {
|
||||
|
||||
// Synchronize
|
||||
try {
|
||||
// localDB.applySync(client.sendSync(client.getSender().getId(),
|
||||
// localDB.fillSync(client.getSender().getId())));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Could not perform sync", e);
|
||||
}
|
||||
|
||||
// TODO: Process unread messages
|
||||
// localDB.addUnreadMessagesToLocalDB();
|
||||
// localDB.clearUnreadMessagesSync();
|
||||
|
||||
// Mark unread messages as read when they are in the current chat
|
||||
readCurrentChat();
|
||||
|
||||
// Update UI
|
||||
SwingUtilities.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
|
||||
}).start();
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void updateUserStates() {
|
||||
for (int i = 0; i < userList.getModel().getSize(); i++)
|
||||
for (int j = 0; j < localDB.getChats().size(); j++)
|
||||
if (userList.getModel().getElementAt(i).getId() == localDB.getChats().get(j).getRecipient().getId())
|
||||
userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks messages in the current chat as {@code READ}.
|
||||
*/
|
||||
private void readCurrentChat() {
|
||||
if (currentChat != null) {
|
||||
// TODO: localDB.setMessagesToRead(currentChat);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link Client} used by this {@link ChatWindow}. If the client is
|
||||
* online, the sync thread is started.
|
||||
* Sets the {@link Client} used by this {@link ChatWindow}.
|
||||
*
|
||||
* @param client the {@link Client} used to send and receive messages
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public void setClient(Client client) {
|
||||
this.client = client;
|
||||
if (client.isOnline() && localDB != null) startSyncThread(Config.getInstance().getSyncTimeout());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -379,6 +330,5 @@ public class ChatWindow extends JFrame {
|
||||
public void setLocalDB(LocalDB localDB) {
|
||||
this.localDB = localDB;
|
||||
loadUsersAndChats();
|
||||
if (client != null && client.isOnline()) startSyncThread(Config.getInstance().getSyncTimeout());
|
||||
}
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ public class Startup {
|
||||
.getItems()
|
||||
.get("onCloseMode")
|
||||
.setChangeHandler((onCloseMode) -> chatWindow
|
||||
.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));
|
||||
.setDefaultCloseOperation((Boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));
|
||||
} catch (EnvoyException e) {
|
||||
logger.warning("The StatusTrayIcon is not supported on this platform!");
|
||||
}
|
||||
|
@ -71,6 +71,13 @@ public final class ComponentListModel<E> implements Iterable<E>, Serializable {
|
||||
return elements.remove(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the amount of elements in this list model
|
||||
* @see java.util.List#size()
|
||||
* @since Envoy v0.3-alpha
|
||||
*/
|
||||
public int size() { return elements.size(); }
|
||||
|
||||
/**
|
||||
* @return an iterator over the elements of this list model
|
||||
* @see java.util.List#iterator()
|
||||
|
Reference in New Issue
Block a user