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
@ -21,8 +21,8 @@ import envoy.event.MessageStatusChangeEvent;
|
|||||||
public abstract class LocalDb {
|
public abstract class LocalDb {
|
||||||
|
|
||||||
protected User user;
|
protected User user;
|
||||||
protected Map<String, User> users = new HashMap<>();
|
protected Map<String, User> users = new HashMap<>();
|
||||||
protected List<Chat> chats = new ArrayList<>();
|
protected List<Chat> chats = new ArrayList<>();
|
||||||
protected IdGenerator idGenerator;
|
protected IdGenerator idGenerator;
|
||||||
protected Cache<Message> messageCache = new Cache<>();
|
protected Cache<Message> messageCache = new Cache<>();
|
||||||
protected Cache<MessageStatusChangeEvent> statusCache = new Cache<>();
|
protected Cache<MessageStatusChangeEvent> statusCache = new Cache<>();
|
||||||
@ -143,4 +143,19 @@ public abstract class LocalDb {
|
|||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public void setStatusCache(Cache<MessageStatusChangeEvent> statusCache) { this.statusCache = statusCache; }
|
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 {
|
try {
|
||||||
logger.finer("Sending cached " + msg);
|
logger.finer("Sending cached " + msg);
|
||||||
client.sendMessage(msg);
|
client.sendMessage(msg);
|
||||||
|
|
||||||
|
// Update message state to SENT in local db
|
||||||
|
localDb.getMessage(msg.getId()).nextStatus();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.log(Level.SEVERE, "Could not send cached message", 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() {
|
private void readCurrentChat() {
|
||||||
try {
|
try {
|
||||||
currentChat.read(writeProxy);
|
currentChat.read(writeProxy);
|
||||||
@ -590,7 +568,21 @@ public class ChatWindow extends JFrame {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
this.localDb = localDb;
|
this.localDb = localDb;
|
||||||
this.writeProxy = writeProxy;
|
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.client.net.WriteProxy;
|
||||||
import envoy.data.Config;
|
import envoy.data.Config;
|
||||||
import envoy.data.Message;
|
import envoy.data.Message;
|
||||||
|
import envoy.data.User.UserStatus;
|
||||||
import envoy.exception.EnvoyException;
|
import envoy.exception.EnvoyException;
|
||||||
import envoy.util.EnvoyLog;
|
import envoy.util.EnvoyLog;
|
||||||
|
|
||||||
@ -113,7 +114,7 @@ public class Startup {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
JOptionPane.showMessageDialog(null,
|
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",
|
"Local DB error",
|
||||||
JOptionPane.WARNING_MESSAGE);
|
JOptionPane.WARNING_MESSAGE);
|
||||||
}
|
}
|
||||||
@ -121,10 +122,15 @@ public class Startup {
|
|||||||
// Initialize write proxy
|
// Initialize write proxy
|
||||||
final WriteProxy writeProxy = client.createWriteProxy(localDb);
|
final WriteProxy writeProxy = client.createWriteProxy(localDb);
|
||||||
|
|
||||||
// Save all users to the local database and flush cache
|
|
||||||
if (client.isOnline()) {
|
if (client.isOnline()) {
|
||||||
|
|
||||||
|
// Save all users to the local database and flush cache
|
||||||
localDb.setUsers(client.getUsers());
|
localDb.setUsers(client.getUsers());
|
||||||
writeProxy.flushCache();
|
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
|
// Display ChatWindow and StatusTrayIcon
|
||||||
|
Reference in New Issue
Block a user