Fixed state errors in offline mode (#116)
* Display all contacts as offline while in offline mode * Update message status to sent after relaying message cache
This commit is contained in:
parent
198e81608b
commit
bd1563b439
@ -143,4 +143,19 @@ public abstract class LocalDb {
|
||||
* @since Envoy v0.3-alpha
|
||||
*/
|
||||
public void setStatusCache(Cache<MessageStatusChangeEvent> statusCache) { this.statusCache = statusCache; }
|
||||
|
||||
/**
|
||||
* Searches for a message by ID.
|
||||
*
|
||||
* @param id the ID of the message to search for
|
||||
* @return the message with the corresponding ID, or {@code null} if no message
|
||||
* has been found
|
||||
* @since Envoy v0.1-beta
|
||||
*/
|
||||
public Message getMessage(long id) {
|
||||
for (Chat c : chats)
|
||||
for (Message m : c.getModel())
|
||||
if (m.getId() == id) return m;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,9 @@ public class WriteProxy {
|
||||
try {
|
||||
logger.finer("Sending cached " + msg);
|
||||
client.sendMessage(msg);
|
||||
|
||||
// Update message state to SENT in local db
|
||||
localDb.getMessage(msg.getId()).nextStatus();
|
||||
} catch (IOException e) {
|
||||
logger.log(Level.SEVERE, "Could not send cached message", e);
|
||||
}
|
||||
|
@ -522,28 +522,6 @@ public class ChatWindow extends JFrame {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the elements of the user list by downloading them from the
|
||||
* server.
|
||||
*
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
private void loadUsersAndChats() {
|
||||
new Thread(() -> {
|
||||
localDb.getUsers().values().forEach(user -> {
|
||||
userListModel.addElement(user);
|
||||
|
||||
// Check if user exists in local DB
|
||||
if (localDb.getChats().stream().filter(c -> c.getRecipient().getId() == user.getId()).count() == 0)
|
||||
localDb.getChats().add(new Chat(user));
|
||||
});
|
||||
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
||||
|
||||
revalidate();
|
||||
repaint();
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void readCurrentChat() {
|
||||
try {
|
||||
currentChat.read(writeProxy);
|
||||
@ -590,7 +568,21 @@ public class ChatWindow extends JFrame {
|
||||
this.client = client;
|
||||
this.localDb = localDb;
|
||||
this.writeProxy = writeProxy;
|
||||
loadUsersAndChats();
|
||||
|
||||
// Load users and chats
|
||||
new Thread(() -> {
|
||||
localDb.getUsers().values().forEach(user -> {
|
||||
userListModel.addElement(user);
|
||||
|
||||
// Check if user exists in local DB
|
||||
if (localDb.getChats().stream().noneMatch(c -> c.getRecipient().getId() == user.getId()))
|
||||
localDb.getChats().add(new Chat(user));
|
||||
});
|
||||
SwingUtilities.invokeLater(() -> userList.setModel(userListModel));
|
||||
|
||||
revalidate();
|
||||
repaint();
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@ import envoy.client.net.Client;
|
||||
import envoy.client.net.WriteProxy;
|
||||
import envoy.data.Config;
|
||||
import envoy.data.Message;
|
||||
import envoy.data.User.UserStatus;
|
||||
import envoy.exception.EnvoyException;
|
||||
import envoy.util.EnvoyLog;
|
||||
|
||||
@ -113,7 +114,7 @@ public class Startup {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
JOptionPane.showMessageDialog(null,
|
||||
"Error while loading local database: " + e + "\nChats might not be stored locally.",
|
||||
"Error while loading local database: " + e + "\nChats will not be stored locally.",
|
||||
"Local DB error",
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
@ -121,10 +122,15 @@ public class Startup {
|
||||
// Initialize write proxy
|
||||
final WriteProxy writeProxy = client.createWriteProxy(localDb);
|
||||
|
||||
// Save all users to the local database and flush cache
|
||||
if (client.isOnline()) {
|
||||
|
||||
// Save all users to the local database and flush cache
|
||||
localDb.setUsers(client.getUsers());
|
||||
writeProxy.flushCache();
|
||||
} else {
|
||||
|
||||
// Set all contacts to offline mode
|
||||
localDb.getUsers().values().stream().filter(u -> u != localDb.getUser()).forEach(u -> u.setStatus(UserStatus.OFFLINE));
|
||||
}
|
||||
|
||||
// Display ChatWindow and StatusTrayIcon
|
||||
|
Reference in New Issue
Block a user