Integrated WriteProxy into the sending process

This commit is contained in:
Kai S. K. Engelbart 2020-02-06 21:28:02 +01:00
parent bf38d2f19f
commit 4afe073e79
3 changed files with 37 additions and 33 deletions

View File

@ -3,7 +3,7 @@ package envoy.client.data;
import java.io.IOException;
import java.io.Serializable;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.ui.list.ComponentListModel;
import envoy.data.Message;
import envoy.data.Message.MessageStatus;
@ -52,22 +52,20 @@ public class Chat implements Serializable {
* {@code READ} starting from the bottom and stopping once a read message is
* found.
*
* @param client the client instance used to notify the server about the message
* status changes
* @param writeProxy the write proxy instance used to notify the server about
* the message status changes
* @throws IOException if a {@link MessageStatusChangeEvent} could not be
* delivered to the server
* @since Envoy v0.3-alpha
*/
public void read(Client client) throws IOException {
public void read(WriteProxy writeProxy) throws IOException {
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);
// TODO: Cache events in offline mode
client.sendEvent(new MessageStatusChangeEvent(m));
writeProxy.writeMessageStatusChangeEvent(new MessageStatusChangeEvent(m));
}
}
}

View File

@ -16,6 +16,7 @@ import envoy.client.data.LocalDb;
import envoy.client.event.MessageCreationEvent;
import envoy.client.event.ThemeChangeEvent;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.ui.list.ComponentList;
import envoy.client.ui.settings.SettingsScreen;
import envoy.client.util.EnvoyLog;
@ -39,8 +40,9 @@ import envoy.event.MessageStatusChangeEvent;
public class ChatWindow extends JFrame {
// User specific objects
private Client client;
private LocalDb localDb;
private Client client;
private WriteProxy writeProxy;
private LocalDb localDb;
// GUI components
private JPanel contentPane = new JPanel();
@ -211,7 +213,7 @@ public class ChatWindow extends JFrame {
// Listen to received messages
EventBus.getInstance().register(MessageCreationEvent.class, (evt) -> {
Message message = ((MessageCreationEvent) evt).get();
Message message = ((MessageCreationEvent) evt).get();
Chat chat = localDb.getChats().stream().filter(c -> c.getRecipient().getId() == message.getSenderId()).findFirst().get();
chat.appendMessage(message);
@ -301,8 +303,7 @@ public class ChatWindow extends JFrame {
.build();
// Send message
// TODO: Store offline messages
client.sendMessage(message);
writeProxy.writeMessage(message);
// Add message to PersistentLocalDb and update UI
currentChat.appendMessage(message);
@ -345,7 +346,7 @@ public class ChatWindow extends JFrame {
private void readCurrentChat() {
try {
currentChat.read(client);
currentChat.read(writeProxy);
messageList.synchronizeModel();
} catch (IOException e) {
e.printStackTrace();
@ -354,24 +355,23 @@ public class ChatWindow extends JFrame {
}
/**
* Sets the {@link Client} used by this {@link ChatWindow}.
* Initializes the components responsible server communication and
* persistence.<br>
* <br>
* This will trigger the display of the contact list.
*
* @param client the {@link Client} used to send and receive messages
* @since Envoy v0.2-alpha
* @param client the client used to send and receive messages
* @param localDb the local database used to manage stored messages
* and users
* @param writeProxy the write proxy used to send messages and status change
* events to the server or cache them inside the local
* database
* @since Envoy v0.3-alpha
*/
public void setClient(Client client) { this.client = client; }
/**
* Sets the {@link LocalDb} used by this {@link ChatWindow}. After
* invoking this
* method, users and chats will be loaded from the database into the GUI.
*
* @param localDb the {@link LocalDb} used to manage stored messages
* and users
* @since Envoy v0.2-alpha
*/
public void setLocalDB(LocalDb localDb) {
this.localDb = localDb;
public void initContent(Client client, LocalDb localDb, WriteProxy writeProxy) {
this.client = client;
this.localDb = localDb;
this.writeProxy = writeProxy;
loadUsersAndChats();
}
}

View File

@ -15,6 +15,7 @@ import envoy.client.Config;
import envoy.client.Settings;
import envoy.client.data.*;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.util.EnvoyLog;
import envoy.data.LoginCredentials;
import envoy.data.Message;
@ -148,14 +149,19 @@ public class Startup {
JOptionPane.WARNING_MESSAGE);
}
// Save all users to the local database
if (client.isOnline()) localDb.setUsers(client.getUsers());
// Initialize write proxy
final WriteProxy writeProxy = client.createWriteProxy(localDb);
// Save all users to the local database and flush cache
if (client.isOnline()) {
localDb.setUsers(client.getUsers());
writeProxy.flushCache();
}
// Display ChatWindow and StatusTrayIcon
EventQueue.invokeLater(() -> {
try {
chatWindow.setClient(client);
chatWindow.setLocalDB(localDb);
chatWindow.initContent(client, localDb, writeProxy);
try {
new StatusTrayIcon(chatWindow).show();