Reduced the number of user list requests to one

Fixes #16
This commit is contained in:
Kai S. K. Engelbart 2019-10-26 17:49:45 +02:00
parent 608f900681
commit 09b995ea0f

View File

@ -48,18 +48,13 @@ public class ChatWindow extends JFrame {
private Client client;
private DefaultListModel<Message> messageListModel = new DefaultListModel<>();
private List<Chat> partnerChatList = new ArrayList<Chat>();
private Chat currentChat;
private JList<User> userList = new JList<>();
private List<Chat> partnerChatList = new ArrayList<Chat>();
private Chat currentChat;
public ChatWindow(Client client) {
this.client = client;
// Initialize chat list and current chat
Users chatUsers = client.getUsersListXml();
chatUsers.getUser().forEach(user -> partnerChatList.add(new Chat(user)));
if (partnerChatList.size() > 0) currentChat = partnerChatList.get(0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 800);
setTitle("Envoy");
@ -86,6 +81,7 @@ public class ChatWindow extends JFrame {
messageList.setForeground(new Color(255, 255, 255));
messageList.setBackground(new Color(51, 51, 51));
DefaultListModel<Message> messageListModel = new DefaultListModel<>();
messageList.setModel(messageListModel);
messageList.setFont(new Font("Arial", Font.PLAIN, 17));
messageList.setFixedCellHeight(60);
@ -142,10 +138,7 @@ public class ChatWindow extends JFrame {
postButton.addActionListener((evt) -> {
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;
}
@ -189,7 +182,6 @@ public class ChatWindow extends JFrame {
gbc_partnerName.insets = new Insets(0, 10, 0, 10);
contentPane.add(textPane, gbc_partnerName);
JList<User> userList = new JList<>();
userList.setCellRenderer(new UserListRenderer());
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
userList.addListSelectionListener((listSelectionEvent) -> {
@ -199,10 +191,7 @@ public class ChatWindow extends JFrame {
final User user = selectedUserList.getSelectedValue();
client.setRecipient(user);
currentChat = partnerChatList.stream()
.filter(chat -> chat.getRecipient().getID() == user.getID())
.findFirst()
.get();
currentChat = partnerChatList.stream().filter(chat -> chat.getRecipient().getID() == user.getID()).findFirst().get();
client.setRecipient(user);
@ -229,32 +218,38 @@ public class ChatWindow extends JFrame {
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
loadUserList(userList);
new Timer(5000, (evt) -> {
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
for (int j = 0; j < partnerChatList.size(); j++)
if (partnerChatList.get(j)
.getRecipient()
.getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
}).start();
loadUsersAndChats();
startReceiverThread(5000);
contentPane.revalidate();
}
/**
* Initializes the elements of the user list by downloading them from the
* server.
*
* @param userList The {@link JList} to put the elements in
*/
private void loadUserList(JList<User> userList) {
private void loadUsersAndChats() {
new Thread(() -> {
Users users = client.getUsersListXml();
DefaultListModel<User> userListModel = new DefaultListModel<>();
users.getUser().forEach(user -> userListModel.addElement(user));
users.getUser().forEach(user -> { userListModel.addElement(user); partnerChatList.add(new Chat(user)); });
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
}).start();
}
/**
* Checks for new messages and adds them to the chat list.
*
* @param timeout the amount of time that passes between two requests sent to
* the server
*/
private void startReceiverThread(int timeout) {
new Timer(timeout, (evt) -> {
Messages unreadMessages = client.getUnreadMessages(client.getSender().getID());
for (int i = 0; i < unreadMessages.getMessage().size(); i++)
for (int j = 0; j < partnerChatList.size(); j++)
if (partnerChatList.get(j).getRecipient().getID() == unreadMessages.getMessage().get(i).getMetaData().getSender())
partnerChatList.get(j).appendMessage(unreadMessages.getMessage().get(i));
}).start();
}
}