Added ChatSynchronizer class to store chats locally
This commit is contained in:
parent
6f6388f595
commit
d2dbf91b8a
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/target/
|
/target/
|
||||||
|
/local_chats.db
|
||||||
|
@ -1,12 +1,16 @@
|
|||||||
package envoy.client;
|
package envoy.client;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
|
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.User;
|
import envoy.schema.User;
|
||||||
|
|
||||||
public class Chat {
|
public class Chat implements Serializable {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -7751248474547242056L;
|
||||||
|
|
||||||
private User recipient;
|
private User recipient;
|
||||||
private DefaultListModel<Message> model = new DefaultListModel<>();
|
private DefaultListModel<Message> model = new DefaultListModel<>();
|
||||||
|
|
||||||
|
47
src/main/java/envoy/client/ChatSynchronizer.java
Normal file
47
src/main/java/envoy/client/ChatSynchronizer.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package envoy.client;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>envoy-client</strong><br>
|
||||||
|
* File: <strong>ChatSynchronizer.java</strong><br>
|
||||||
|
* Created: <strong>27.10.2019</strong><br>
|
||||||
|
*
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
|
* @since Envoy v0.1-alpha
|
||||||
|
*/
|
||||||
|
public class ChatSynchronizer {
|
||||||
|
|
||||||
|
private File localDB;
|
||||||
|
private List<Chat> chats = new ArrayList<>();
|
||||||
|
|
||||||
|
public ChatSynchronizer(String localDBPath) {
|
||||||
|
localDB = new File(localDBPath);
|
||||||
|
if (localDB.exists() && !localDB.isDirectory()) loadFromLocalDB();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveToLocalDB() {
|
||||||
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
||||||
|
out.writeObject(chats);
|
||||||
|
} catch(IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private void loadFromLocalDB() {
|
||||||
|
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
|
||||||
|
Object obj = in.readObject();
|
||||||
|
if (obj instanceof ArrayList<?>) chats = (ArrayList<Chat>) obj;
|
||||||
|
} catch (ClassNotFoundException | IOException e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Chat> getChats() { return chats; }
|
||||||
|
}
|
@ -6,8 +6,8 @@ import java.awt.Font;
|
|||||||
import java.awt.GridBagConstraints;
|
import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.util.ArrayList;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.util.List;
|
import java.awt.event.WindowEvent;
|
||||||
|
|
||||||
import javax.swing.DefaultListModel;
|
import javax.swing.DefaultListModel;
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
@ -24,6 +24,7 @@ import javax.swing.Timer;
|
|||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
import envoy.client.Chat;
|
import envoy.client.Chat;
|
||||||
|
import envoy.client.ChatSynchronizer;
|
||||||
import envoy.client.Client;
|
import envoy.client.Client;
|
||||||
import envoy.schema.Message;
|
import envoy.schema.Message;
|
||||||
import envoy.schema.Messages;
|
import envoy.schema.Messages;
|
||||||
@ -46,20 +47,30 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
private JPanel contentPane = new JPanel();
|
private JPanel contentPane = new JPanel();
|
||||||
|
|
||||||
private Client client;
|
private Client client;
|
||||||
|
private ChatSynchronizer chatSynchronizer;
|
||||||
|
|
||||||
private JList<User> userList = new JList<>();
|
private JList<User> userList = new JList<>();
|
||||||
private List<Chat> partnerChatList = new ArrayList<Chat>();
|
|
||||||
private Chat currentChat;
|
private Chat currentChat;
|
||||||
|
|
||||||
public ChatWindow(Client client) {
|
public ChatWindow(Client client, ChatSynchronizer chatSynchronizer) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
|
this.chatSynchronizer = chatSynchronizer;
|
||||||
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setBounds(100, 100, 600, 800);
|
setBounds(100, 100, 600, 800);
|
||||||
setTitle("Envoy");
|
setTitle("Envoy");
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// Save chats when window closes
|
||||||
|
addWindowListener(new WindowAdapter() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void windowClosing(WindowEvent e) {
|
||||||
|
chatSynchronizer.saveToLocalDB();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
contentPane.setBackground(new Color(0, 0, 0));
|
contentPane.setBackground(new Color(0, 0, 0));
|
||||||
contentPane.setForeground(Color.white);
|
contentPane.setForeground(Color.white);
|
||||||
contentPane.setBorder(new EmptyBorder(0, 5, 0, 0));
|
contentPane.setBorder(new EmptyBorder(0, 5, 0, 0));
|
||||||
@ -138,7 +149,8 @@ public class ChatWindow extends JFrame {
|
|||||||
|
|
||||||
postButton.addActionListener((evt) -> {
|
postButton.addActionListener((evt) -> {
|
||||||
if (!client.hasRecipient()) {
|
if (!client.hasRecipient()) {
|
||||||
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message", JOptionPane.INFORMATION_MESSAGE);
|
JOptionPane.showMessageDialog(this, "Please select a recipient!", "Cannot send message",
|
||||||
|
JOptionPane.INFORMATION_MESSAGE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,8 +170,7 @@ public class ChatWindow extends JFrame {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
JOptionPane.showMessageDialog(this,
|
JOptionPane.showMessageDialog(this,
|
||||||
"An exception occured while sending a message. See the log for more details.",
|
"An exception occured while sending a message. See the log for more details.",
|
||||||
"Exception occured",
|
"Exception occured", JOptionPane.ERROR_MESSAGE);
|
||||||
JOptionPane.ERROR_MESSAGE);
|
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -186,12 +197,18 @@ public class ChatWindow extends JFrame {
|
|||||||
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
||||||
userList.addListSelectionListener((listSelectionEvent) -> {
|
userList.addListSelectionListener((listSelectionEvent) -> {
|
||||||
if (!listSelectionEvent.getValueIsAdjusting()) {
|
if (!listSelectionEvent.getValueIsAdjusting()) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings(
|
||||||
|
"unchecked"
|
||||||
|
)
|
||||||
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
|
final JList<User> selectedUserList = (JList<User>) listSelectionEvent.getSource();
|
||||||
final User user = selectedUserList.getSelectedValue();
|
final User user = selectedUserList.getSelectedValue();
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
currentChat = partnerChatList.stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
|
currentChat = chatSynchronizer.getChats()
|
||||||
|
.stream()
|
||||||
|
.filter(chat -> chat.getRecipient().getID() == user.getID())
|
||||||
|
.findFirst()
|
||||||
|
.get();
|
||||||
|
|
||||||
client.setRecipient(user);
|
client.setRecipient(user);
|
||||||
|
|
||||||
@ -232,7 +249,15 @@ public class ChatWindow extends JFrame {
|
|||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
Users users = client.getUsersListXml();
|
Users users = client.getUsersListXml();
|
||||||
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
DefaultListModel<User> userListModel = new DefaultListModel<>();
|
||||||
users.getUser().forEach(user -> { userListModel.addElement(user); partnerChatList.add(new Chat(user)); });
|
users.getUser().forEach(user -> {
|
||||||
|
userListModel.addElement(user);
|
||||||
|
|
||||||
|
// Check if user exists in local DB
|
||||||
|
if (chatSynchronizer.getChats()
|
||||||
|
.stream()
|
||||||
|
.filter(c -> c.getRecipient().getID() == user.getID())
|
||||||
|
.count() == 0) chatSynchronizer.getChats().add(new Chat(user));
|
||||||
|
});
|
||||||
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
@ -247,9 +272,11 @@ public class ChatWindow extends JFrame {
|
|||||||
new Timer(timeout, (evt) -> {
|
new Timer(timeout, (evt) -> {
|
||||||
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
|
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
|
||||||
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
|
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
|
||||||
for (int j = 0; j < partnerChatList.size(); j++)
|
for (int j = 0; j < chatSynchronizer.getChats().size(); j++)
|
||||||
if (partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
|
if (chatSynchronizer.getChats().get(j)
|
||||||
partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
|
.getRecipient()
|
||||||
|
.getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
|
||||||
|
chatSynchronizer.getChats().get(j).appendMessage(unreadMessages.getMessage().get(i));
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -6,6 +6,7 @@ import java.util.Properties;
|
|||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
|
import envoy.client.ChatSynchronizer;
|
||||||
import envoy.client.Client;
|
import envoy.client.Client;
|
||||||
import envoy.client.Config;
|
import envoy.client.Config;
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ public class Startup {
|
|||||||
|
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
try {
|
try {
|
||||||
ChatWindow frame = new ChatWindow(client);
|
ChatWindow frame = new ChatWindow(client, new ChatSynchronizer("local_chats.db"));
|
||||||
frame.setVisible(true);
|
frame.setVisible(true);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Reference in New Issue
Block a user