Improve performance of a status update

Additionally fixed a bug causing unnecessary warnings on the server
This commit is contained in:
Leon Hofmeister 2020-10-10 11:10:17 +02:00
parent fa2a5d0b24
commit 08bd915f04
Signed by: delvh
GPG Key ID: 3DECE05F6D9A647C
3 changed files with 40 additions and 8 deletions

View File

@ -15,11 +15,14 @@ import envoy.data.*;
*/
public final class ContactControl extends VBox {
private final Contact contact;
/**
* @param contact the contact to display
* @since Envoy Client v0.2-beta
*/
public ContactControl(Contact contact) {
this.contact = contact;
// Name label
final var nameLabel = new Label(contact.getName());
@ -30,4 +33,19 @@ public final class ContactControl extends VBox {
getStyleClass().add("list-element");
}
/**
* Replaces the info label of this {@code ContactControl} with an updated
* version.
* <p>
* This method should be called when the status of the underlying user or the
* size of the underlying group has changed.
*
* @since Envoy Client v0.3-beta
* @apiNote will produce buggy results if contact control gets updated so that
* the info label is no longer on index 1.
*/
public void replaceInfoLabel() {
getChildren().set(1, contact instanceof User ? new UserStatusLabel((User) contact) : new GroupSizeLabel((Group) contact));
}
}

View File

@ -261,6 +261,8 @@ public final class ChatScene implements EventListener, Restorable {
private void onUserStatusChange(UserStatusChange statusChange) {
Platform.runLater(() -> {
chatList.refresh();
// Replacing the display in the top bar
if (currentChat != null && currentChat.getRecipient().getID() == statusChange.getID()) {
topBarStatusLabel.getStyleClass().clear();
topBarStatusLabel.setText(statusChange.get().toString());
@ -718,10 +720,17 @@ public final class ChatScene implements EventListener, Restorable {
@Event(eventType = OwnStatusChange.class, priority = 50)
private void generateOwnStatusControl() {
final var ownUserControl = new ContactControl(localDB.getUser());
ownUserControl.setAlignment(Pos.CENTER_LEFT);
if (ownContactControl.getChildren().get(0) instanceof ContactControl) ownContactControl.getChildren().set(0, ownUserControl);
else ownContactControl.getChildren().add(0, ownUserControl);
// Update the own user status if present
if (ownContactControl.getChildren().get(0) instanceof ContactControl)
((ContactControl) ownContactControl.getChildren().get(0)).replaceInfoLabel();
else {
// Else prepend it to the HBox children
final var ownUserControl = new ContactControl(localDB.getUser());
ownUserControl.setAlignment(Pos.CENTER_LEFT);
ownContactControl.getChildren().add(0, ownUserControl);
}
}
// Context menu actions

View File

@ -46,14 +46,19 @@ public final class UserUtil {
/**
* Notifies the application that the status of the currently logged in user has
* changed
* changed.
*
* @param newStatus the new status
* @since Envoy Client v0.3-beta
*/
public static void changeStatus(UserStatus newStatus) {
EventBus.getInstance().dispatch(new OwnStatusChange(newStatus));
if (Context.getInstance().getClient().isOnline())
Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus));
// Sending the already active status is a valid action
if (newStatus.equals(Context.getInstance().getLocalDB().getUser().getStatus())) return;
else {
EventBus.getInstance().dispatch(new OwnStatusChange(newStatus));
if (Context.getInstance().getClient().isOnline())
Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus));
}
}
}