Merge branch 'develop' into f/system_commands

This commit is contained in:
delvh
2020-07-18 11:27:59 +02:00
40 changed files with 323 additions and 328 deletions

View File

@ -1,5 +1,7 @@
package envoy.client.ui.controller;
import static envoy.data.Message.MessageStatus.RECEIVED;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.io.ByteArrayInputStream;
@ -134,7 +136,13 @@ public final class ChatScene implements Restorable {
// Listen to received messages
eventBus.register(MessageCreationEvent.class, e -> {
final var message = e.get();
localDB.getChat(message instanceof GroupMessage ? message.getRecipientID() : message.getSenderID()).ifPresent(chat -> {
// The sender of the message is the recipient of the chat
// Exceptions: this user is the sender (sync) or group message (group is
// recipient)
final long recipientID = message instanceof GroupMessage || message.getSenderID() == localDB.getUser().getID() ? message.getRecipientID()
: message.getSenderID();
localDB.getChat(recipientID).ifPresent(chat -> {
chat.insert(message);
if (chat.equals(currentChat)) {
try {
@ -143,8 +151,10 @@ public final class ChatScene implements Restorable {
logger.log(Level.WARNING, "Could not read current chat: ", e1);
}
Platform.runLater(() -> { ListViewRefresh.deepRefresh(messageList); scrollToMessageListEnd(); });
} else chat.incrementUnreadAmount();
// Moving chat with most recent unreadMessages to the top
// TODO: Increment unread counter for group messages with status < RECEIVED
} else if (message.getSenderID() != localDB.getUser().getID() && message.getStatus() == RECEIVED) chat.incrementUnreadAmount();
// Move chat with most recent unread messages to the top
Platform.runLater(() -> {
chatList.getItems().remove(chat);
chatList.getItems().add(0, chat);
@ -182,12 +192,11 @@ public final class ChatScene implements Restorable {
final var contact = e.get();
switch (e.getOperationType()) {
case ADD:
localDB.getUsers().put(contact.getName(), contact);
final Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
if (contact instanceof User) localDB.getUsers().put(contact.getName(), (User) contact);
Chat chat = contact instanceof User ? new Chat(contact) : new GroupChat(client.getSender(), contact);
Platform.runLater(() -> chatList.getItems().add(chat));
break;
case REMOVE:
localDB.getUsers().remove(contact.getName());
Platform.runLater(() -> chatList.getItems().removeIf(c -> c.getRecipient().equals(contact)));
break;
}

View File

@ -2,6 +2,7 @@ package envoy.client.ui.controller;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.Instant;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -94,7 +95,8 @@ public final class LoginScene {
userTextField.requestFocus();
// Perform automatic login if configured
if (config.hasLoginCredentials()) performHandshake(config.getLoginCredentials());
if (config.hasLoginCredentials())
performHandshake(new LoginCredentials(config.getUser(), config.getPassword(), false, Startup.VERSION, loadLastSync(config.getUser())));
}
@FXML
@ -108,12 +110,13 @@ public final class LoginScene {
new Alert(AlertType.ERROR, "The entered user name is not valid (" + Bounds.CONTACT_NAME_PATTERN + ")").showAndWait();
userTextField.getTextField().clear();
} else performHandshake(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), registerCheckBox.isSelected(),
Startup.VERSION));
Startup.VERSION, loadLastSync(userTextField.getTextField().getText())));
}
@FXML
private void offlineModeButtonPressed() {
attemptOfflineMode(new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), false, Startup.VERSION));
attemptOfflineMode(
new LoginCredentials(userTextField.getTextField().getText(), passwordField.getText(), false, Startup.VERSION, localDB.getLastSync()));
}
@FXML
@ -130,6 +133,18 @@ public final class LoginScene {
System.exit(0);
}
private Instant loadLastSync(String identifier) {
try {
localDB.loadUsers();
localDB.setUser(localDB.getUsers().get(identifier));
localDB.initializeUserStorage();
localDB.loadUserData();
} catch (Exception e) {
// User storage empty, wrong user name etc. -> default lastSync
}
return localDB.getLastSync();
}
private void performHandshake(LoginCredentials credentials) {
try {
client.performHandshake(credentials, cacheMap);