Add Ability to Change the User Status Freely #90

Merged
delvh merged 7 commits from f/change-user-status into develop 2020-10-10 14:16:29 +02:00
3 changed files with 40 additions and 8 deletions
Showing only changes of commit 08bd915f04 - Show all commits

View File

@ -15,11 +15,14 @@ import envoy.data.*;
*/ */
public final class ContactControl extends VBox { public final class ContactControl extends VBox {
private final Contact contact;
/** /**
* @param contact the contact to display * @param contact the contact to display
* @since Envoy Client v0.2-beta * @since Envoy Client v0.2-beta
*/ */
public ContactControl(Contact contact) { public ContactControl(Contact contact) {
this.contact = contact;
// Name label // Name label
final var nameLabel = new Label(contact.getName()); final var nameLabel = new Label(contact.getName());
@ -30,4 +33,19 @@ public final class ContactControl extends VBox {
getStyleClass().add("list-element"); 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) { private void onUserStatusChange(UserStatusChange statusChange) {
Platform.runLater(() -> { Platform.runLater(() -> {
chatList.refresh(); chatList.refresh();
// Replacing the display in the top bar
if (currentChat != null && currentChat.getRecipient().getID() == statusChange.getID()) { if (currentChat != null && currentChat.getRecipient().getID() == statusChange.getID()) {
topBarStatusLabel.getStyleClass().clear(); topBarStatusLabel.getStyleClass().clear();
topBarStatusLabel.setText(statusChange.get().toString()); topBarStatusLabel.setText(statusChange.get().toString());
@ -718,10 +720,17 @@ public final class ChatScene implements EventListener, Restorable {
@Event(eventType = OwnStatusChange.class, priority = 50) @Event(eventType = OwnStatusChange.class, priority = 50)
private void generateOwnStatusControl() { private void generateOwnStatusControl() {
delvh marked this conversation as resolved
Review

You could annotate this as an event handler directly.

You could annotate this as an event handler directly.
final var ownUserControl = new ContactControl(localDB.getUser());
Outdated
Review

I do not see the point of doing this as you can just check if it is null if I understand that correctly. If not please correct and explane the exact purpose to me.

I do not see the point of doing this as you can just check if it is null if I understand that correctly. If not please correct and explane the exact purpose to me.
Outdated
Review

As you can see a few lines above, this method is annotated with an Event: It gets called every time the user changes his own status.
Hence I always have to replace it then.
And for the first part: I cannot do that because initially this component is no part of the HBox (you remember, the thing with importing the by now outdated jar into the SceneBuilder...). To avoid that, it gets dynamically added.

But thinking about it, it may be possible, however I do not know if it'll work as bug-free as this implementation: As soon as you initialize this variable somewhere else, you might encounter a bug that duplicates it.

As you can see a few lines above, this method is annotated with an Event: It gets called every time the user changes his own status. Hence I always have to replace it then. And for the first part: I cannot do that because initially this component is no part of the HBox (you remember, the thing with importing the by now outdated jar into the SceneBuilder...). To avoid that, it gets dynamically added. But thinking about it, it may be possible, however I do not know if it'll work as bug-free as this implementation: As soon as you initialize this variable somewhere else, you might encounter a bug that duplicates it.
Outdated
Review

What if, instead of regenerating the whole control, we would just change the displayed information in the event handler?

What if, instead of regenerating the whole control, we would just change the displayed information in the event handler?
Outdated
Review

It could be possible if we make some changes to the underlying ContactControl.
The question however is: Should it be done?

It could be possible if we make some changes to the underlying `ContactControl`. The question however is: Should it be done?
Outdated
Review

Oh, and also: there is a difference between ownUserControl and ownContactControl.

Oh, and also: there is a difference between ownUserControl and ownContactControl.
Outdated
Review

Better?

Better?
Outdated
Review

Yes better

Yes better
ownUserControl.setAlignment(Pos.CENTER_LEFT); // Update the own user status if present
if (ownContactControl.getChildren().get(0) instanceof ContactControl) ownContactControl.getChildren().set(0, ownUserControl); if (ownContactControl.getChildren().get(0) instanceof ContactControl)
else ownContactControl.getChildren().add(0, ownUserControl); ((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 // 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 * Notifies the application that the status of the currently logged in user has
* changed * changed.
* *
* @param newStatus the new status * @param newStatus the new status
* @since Envoy Client v0.3-beta * @since Envoy Client v0.3-beta
*/ */
public static void changeStatus(UserStatus newStatus) { public static void changeStatus(UserStatus newStatus) {
EventBus.getInstance().dispatch(new OwnStatusChange(newStatus));
if (Context.getInstance().getClient().isOnline()) // Sending the already active status is a valid action
Context.getInstance().getClient().send(new UserStatusChange(Context.getInstance().getLocalDB().getUser().getID(), newStatus)); 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));
}
} }
} }