vaadin / collaboration-engine

The simplest way to build real-time collaboration into web apps
https://vaadin.com/collaboration
Other
3 stars 1 forks source link

Enable reacting to changes in the users in CollaborationAvatarGroup #9

Closed pekam closed 2 years ago

pekam commented 3 years ago

Is your feature request related to a use case? Please describe.

In issue #6, @chrosim described a use case. He would like to display a label above the avatar group conditionally when there are more than zero avatars in the group.

Describe the solution you'd like A generic API for reacting to changes in the list of users would solve this use case and potentially other cases as well.

The proposal is an event listener API, with an event class like this:

class UsersChangedEvent extends ComponentEvent {
    Collection<UserInfo> getUsers();
}

The type for users would be Collection instead of List, because we haven't made any promises of how the avatars are ordered.

The listener interface would be:

interface UsersChangedListener extends ComponentEventListener<UsersChangedEvent> {
}

The method to add to CollaborationAvatarGroup would be:

class CollaborationAvatarGroup {
    Registration addUsersChangedListener(UsersChangedListener listener);
}

The usage for the described use case would look like this:

Label label = new Label("Currently editing:");

collaborationAvatarGroup.addUsersChangedListener(e -> {
    label.setVisible(e.getUsers().size() > 0);
}

To accompany this event listener API, it would make sense to also have a getter method in CollaborationAvatarGroup. I propose we add that in the scope of this ticket:

class CollaborationAvatarGroup {
    Collection<UserInfo> getUsers();
}
heruan commented 2 years ago

This is now provided by the PresenceManager:

presenceManager.setPresenceHandler(context -> {
    UserInfo user = context.getUser();
    log(user.getName() + " joined");
    return () -> log(user.getName() + " left");
});