Added IsWriting event on common, server and partially on client side

additionally fixed NullPointerException in ContactSearchScene and typo
in Javadoc

PS: this is the 1000th commit in Envoy! 🥳 🎉
This commit is contained in:
delvh
2020-07-25 16:26:13 +02:00
parent 72d1e074f4
commit 6f8859c3fd
9 changed files with 151 additions and 18 deletions

View File

@ -9,6 +9,7 @@ import java.util.Objects;
import envoy.client.net.WriteProxy;
import envoy.data.*;
import envoy.data.Message.MessageStatus;
import envoy.event.IsWriting;
import envoy.event.MessageStatusChange;
/**
@ -31,6 +32,12 @@ public class Chat implements Serializable {
protected int unreadAmount;
/**
* Counts the milliseconds until another {@link IsWriting} event will be sent if
* something is typed.
*/
protected transient long lastWritingEvent;
private static final long serialVersionUID = 1L;
/**
@ -41,16 +48,14 @@ public class Chat implements Serializable {
* @param recipient the user who receives the messages
* @since Envoy Client v0.1-alpha
*/
public Chat(Contact recipient) {
this.recipient = recipient;
}
public Chat(Contact recipient) { this.recipient = recipient; }
@Override
public String toString() { return String.format("Chat[recipient=%s,messages=%d]", recipient, messages.size()); }
/**
* Generates a hash code based on the recipient.
*
*
* @since Envoy Client v0.1-beta
*/
@Override
@ -58,14 +63,14 @@ public class Chat implements Serializable {
/**
* Tests equality to another object based on the recipient.
*
*
* @since Envoy Client v0.1-beta
*/
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof Chat)) return false;
Chat other = (Chat) obj;
final Chat other = (Chat) obj;
return Objects.equals(recipient, other.recipient);
}
@ -101,7 +106,7 @@ public class Chat implements Serializable {
/**
* Inserts a message at the correct place according to its creation date.
*
*
* @param message the message to insert
* @since Envoy Client v0.1-beta
*/
@ -116,13 +121,13 @@ public class Chat implements Serializable {
/**
* Increments the amount of unread messages.
*
*
* @since Envoy Client v0.1-beta
*/
public void incrementUnreadAmount() { unreadAmount++; }
/**
* @return the amount of unread mesages in this chat
* @return the amount of unread messages in this chat
* @since Envoy Client v0.1-beta
*/
public int getUnreadAmount() { return unreadAmount; }
@ -150,4 +155,17 @@ public class Chat implements Serializable {
* @since Envoy Client v0.1-beta
*/
public boolean isGroupChat() { return recipient instanceof Group; }
/**
* @return the last known time a {@link IsWriting} event has been sent
* @since Envoy Client v0.2-beta
*/
public long getLastWritingEvent() { return lastWritingEvent; }
/**
* Sets the {@code lastWritingEvent} to {@code System#currentTimeMillis()}.
*
* @since Envoy Client v0.2-beta
*/
public void lastWritingEventWasNow() { lastWritingEvent = System.currentTimeMillis(); }
}

View File

@ -155,6 +155,9 @@ public class Client implements Closeable {
// Process group size changes
receiver.registerProcessor(GroupResize.class, evt -> { localDB.updateGroup(evt); eventBus.dispatch(evt); });
// Process IsWriting events
receiver.registerProcessor(IsWriting.class, eventBus::dispatch);
// Send event
eventBus.register(SendEvent.class, evt -> {
try {

View File

@ -33,6 +33,7 @@ import envoy.client.data.audio.AudioRecorder;
import envoy.client.data.commands.SystemCommandBuilder;
import envoy.client.data.commands.SystemCommandsMap;
import envoy.client.event.MessageCreationEvent;
import envoy.client.event.SendEvent;
import envoy.client.net.Client;
import envoy.client.net.WriteProxy;
import envoy.client.ui.*;
@ -444,10 +445,28 @@ public final class ChatScene implements Restorable {
private void checkKeyCombination(KeyEvent e) {
// Checks whether the text is too long
messageTextUpdated();
// Sending an IsWriting event if none has been sent for
// IsWriting#millisecondsActive
if (client.isOnline() && currentChat.getLastWritingEvent() + IsWriting.millisecondsActive <= System.currentTimeMillis()) {
eventBus.dispatch(new SendEvent(new IsWriting(getChatID(), currentChat.getRecipient().getID(), client.getSender().getName())));
currentChat.lastWritingEventWasNow();
}
// Automatic sending of messages via (ctrl +) enter
checkPostConditions(e);
}
/**
* Returns the id that should be used to send things to the server:
* the id of 'our' {@link User} if the recipient of that object is another User,
* else the id of the {@link Group} 'our' user is sending to.
*
* @return an id that can be sent to the server
* @since Envoy Client v0.2-beta
*/
private long getChatID() {
return currentChat.getRecipient() instanceof User ? client.getSender().getID() : currentChat.getRecipient().getID();
}
/**
* @param e the keys that have been pressed
* @since Envoy Client v0.1-beta
@ -515,7 +534,7 @@ public final class ChatScene implements Restorable {
updateInfoLabel("You need to go online to send more messages", "infoLabel-error");
return;
}
final var text = messageTextArea.getText().strip();
final var text = messageTextArea.getText().strip();
if (!messageTextAreaCommands.executeIfAnyPresent(text)) try {
// Creating the message and its metadata
final var builder = new MessageBuilder(localDB.getUser().getID(), currentChat.getRecipient().getID(), localDB.getIDGenerator())

View File

@ -60,8 +60,10 @@ public class ContactSearchScene {
private final Consumer<ContactOperation> handler = e -> {
final var contact = e.get();
if (e.getOperationType() == ElementOperation.ADD) Platform
.runLater(() -> { userList.getItems().remove(contact); if (currentlySelectedUser.equals(contact) && alert.isShowing()) alert.close(); });
if (e.getOperationType() == ElementOperation.ADD) Platform.runLater(() -> {
userList.getItems().remove(contact);
if (currentlySelectedUser != null && currentlySelectedUser.equals(contact) && alert.isShowing()) alert.close();
});
};
private static final EventBus eventBus = EventBus.getInstance();

View File

@ -138,7 +138,7 @@ public final class LoginScene {
localDB.setUser(localDB.getUsers().get(identifier));
localDB.initializeUserStorage();
localDB.loadUserData();
} catch (Exception e) {
} catch (final Exception e) {
// User storage empty, wrong user name etc. -> default lastSync
}
return localDB.getLastSync();
@ -161,7 +161,7 @@ public final class LoginScene {
try {
// Try entering offline mode
localDB.loadUsers();
final User clientUser = (User) localDB.getUsers().get(credentials.getIdentifier());
final User clientUser = localDB.getUsers().get(credentials.getIdentifier());
if (clientUser == null) throw new EnvoyException("Could not enter offline mode: user name unknown");
client.setSender(clientUser);
loadChatScene();
@ -223,7 +223,7 @@ public final class LoginScene {
// Initialize status tray icon
final var trayIcon = new StatusTrayIcon(sceneContext.getStage());
settings.getItems().get("hideOnClose").setChangeHandler(c -> {
if (((Boolean) c)) trayIcon.show();
if ((Boolean) c) trayIcon.show();
else trayIcon.hide();
});
}

View File

@ -31,12 +31,12 @@ public class ChatControl extends HBox {
// Unread messages
if (chat.getUnreadAmount() != 0) {
Region spacing = new Region();
final Region spacing = new Region();
setHgrow(spacing, Priority.ALWAYS);
getChildren().add(spacing);
final var unreadMessagesLabel = new Label(Integer.toString(chat.getUnreadAmount()));
unreadMessagesLabel.setMinSize(15, 15);
var vBox2 = new VBox();
final var vBox2 = new VBox();
vBox2.setAlignment(Pos.CENTER_RIGHT);
unreadMessagesLabel.setAlignment(Pos.CENTER);
unreadMessagesLabel.getStyleClass().add("unreadMessagesAmount");