Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align reactions display components #2409

Merged
merged 5 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class DrawerMenu extends HBox {
private static final String SLIDE_LEFT_CSS_STYLE = "slide-left";

private final Button menuButton = new Button();
private final HBox itemsHBox = new HBox();
protected final HBox itemsHBox = new HBox();
private final ImageView defaultIcon, hoverIcon, activeIcon;
@Getter
private final BooleanProperty isMenuShowing = new SimpleBooleanProperty(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public final class ChatMessageListItem<M extends ChatMessage, C extends ChatChan
private final Set<Pin> mapPins = new HashSet<>();
private final Set<Pin> statusPins = new HashSet<>();
private final BooleanProperty shouldShowTryAgain = new SimpleBooleanProperty();
private final BooleanProperty hasFailedDeliveryStatus = new SimpleBooleanProperty();
private final SimpleObjectProperty<Node> messageDeliveryStatusNode = new SimpleObjectProperty<>();
private Optional<ResendMessageService> resendMessageService;
private ImageView successfulDeliveryIcon, pendingDeliveryIcon, addedToMailboxIcon, failedDeliveryIcon;
private BisqMenuItem tryAgainMenuItem;

// Reactions
private Optional<Pin> userReactionsPin = Optional.empty();
private final Pin userIdentityPin;
private final HashMap<Reaction, ReactionItem> userReactions = new HashMap<>();
private Optional<Pin> userReactionsPin = Optional.empty();

public ChatMessageListItem(M chatMessage,
C chatChannel,
Expand Down Expand Up @@ -173,7 +173,8 @@ public ChatMessageListItem(M chatMessage,
lastSeen = senderUserProfile.map(userProfileService::getLastSeen).orElse(-1L);
lastSeenAsString = TimeFormatter.formatAge(lastSeen);

// TODO: Release all the listeners when destroying this object
userIdentityPin = userIdentityService.getSelectedUserIdentityObservable().addObserver(userIdentity -> UIThread.run(this::onUserIdentity));

createAndAddSubscriptionToUserReactions(userProfileService);
initializeDeliveryStatusIcons();
addSubscriptionToMessageDeliveryStatus(networkService);
Expand All @@ -197,6 +198,7 @@ public void dispose() {
mapPins.forEach(Pin::unbind);
statusPins.forEach(Pin::unbind);
userReactionsPin.ifPresent(Pin::unbind);
userIdentityPin.unbind();
}

public boolean hasTradeChatOffer() {
Expand Down Expand Up @@ -344,7 +346,6 @@ private void updateMessageStatus(String messageId, Observable<MessageDeliverySta
UIThread.run(() -> {
ChatMessageListItem.this.messageId = messageId;
boolean shouldShowTryAgain = false;
boolean hasFailedDeliveryStatus = false;
if (status != null) {
Label statusLabel = new Label();
statusLabel.setTooltip(new BisqTooltip(Res.get("chat.message.deliveryState." + status.name())));
Expand All @@ -366,13 +367,11 @@ private void updateMessageStatus(String messageId, Observable<MessageDeliverySta
case FAILED:
statusLabel.setGraphic(failedDeliveryIcon);
shouldShowTryAgain = resendMessageService.map(service -> service.canManuallyResendMessage(messageId)).orElse(false);
hasFailedDeliveryStatus = true;
break;
}
messageDeliveryStatusNode.set(statusLabel);
}
this.shouldShowTryAgain.set(shouldShowTryAgain);
this.hasFailedDeliveryStatus.set(hasFailedDeliveryStatus);
});
}));
});
Expand All @@ -384,7 +383,8 @@ private void createAndAddSubscriptionToUserReactions(UserProfileService userProf
}

// Create all the ReactionItems
Arrays.stream(Reaction.values()).forEach(reaction -> userReactions.put(reaction, new ReactionItem(reaction)));
UserProfile selectedUserProfile = userIdentityService.getSelectedUserIdentity().getUserProfile();
Arrays.stream(Reaction.values()).forEach(reaction -> userReactions.put(reaction, new ReactionItem(reaction, selectedUserProfile)));

// Subscribe to changes
userReactionsPin = Optional.ofNullable(chatMessage.getChatMessageReactions().addObserver(new CollectionObserver<>() {
Expand All @@ -395,7 +395,7 @@ public void add(ChatMessageReaction element) {
Optional<UserProfile> userProfile = userProfileService.findUserProfile(element.getUserProfileId());
userProfile.ifPresent(profile -> {
if (!userProfileService.isChatUserIgnored(profile)) {
userReactions.get(reaction).addUser(element, profile, isMyUser(profile));
userReactions.get(reaction).addUser(element, profile);
}
});
}
Expand All @@ -407,7 +407,7 @@ public void remove(Object element) {
Reaction reaction = getReactionFromOrdinal(chatMessageReaction.getReactionId());
if (userReactions.containsKey(reaction)) {
Optional<UserProfile> userProfile = userProfileService.findUserProfile(chatMessageReaction.getUserProfileId());
userProfile.ifPresent(profile -> userReactions.get(reaction).removeUser(profile, isMyUser(profile)));
userProfile.ifPresent(profile -> userReactions.get(reaction).removeUser(profile));
}
}

Expand All @@ -423,8 +423,8 @@ private static Reaction getReactionFromOrdinal(int ordinal) {
return Reaction.values()[ordinal];
}

private boolean isMyUser(UserProfile profile) {
UserProfile myProfile = userIdentityService.getSelectedUserIdentity().getUserProfile();
return myProfile.equals(profile);
private void onUserIdentity() {
UserProfile selectedUserProfile = userIdentityService.getSelectedUserIdentity().getUserProfile();
userReactions.forEach((key, value) -> value.setSelectedUserProfile(selectedUserProfile));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,32 @@ public class ReactionItem {
private long firstAdded;
private final SimpleIntegerProperty count = new SimpleIntegerProperty(0);
private final SimpleBooleanProperty selected = new SimpleBooleanProperty(false);
Set<UserProfile> users = new HashSet<>();
private final Set<UserProfile> users = new HashSet<>();
private UserProfile selectedUserProfile;

ReactionItem(Reaction reaction) {
ReactionItem(Reaction reaction, UserProfile selectedUserProfile) {
this.reaction = reaction;
this.selectedUserProfile = selectedUserProfile;
this.iconId = reaction.toString().replace("_", "").toLowerCase();
}

void addUser(ChatMessageReaction chatMessageReaction, UserProfile userProfile, boolean isMyUser) {
public boolean hasActiveReactions() {
return !users.isEmpty();
}

public String getCountAsString() {
long count = users.size();
if (count < 2) {
return "";
}
return count < 100 ? String.valueOf(count) : "+99";
}

public static Comparator<ReactionItem> firstAddedComparator() {
return Comparator.comparingLong(ReactionItem::getFirstAdded);
}

void addUser(ChatMessageReaction chatMessageReaction, UserProfile userProfile) {
if (hasReactionBeenRemoved(chatMessageReaction)) {
return;
}
Expand All @@ -54,35 +72,28 @@ void addUser(ChatMessageReaction chatMessageReaction, UserProfile userProfile, b
firstAdded = chatMessageReaction.getDate();
}

selected.set(isMyUser);
users.add(userProfile);
count.set(users.size());
updateSelected();
}

void removeUser(UserProfile userProfile, boolean isMyUser) {
selected.set(!isMyUser);
void removeUser(UserProfile userProfile) {
users.remove(userProfile);
count.set(users.size());
updateSelected();
}

public boolean hasActiveReactions() {
return !users.isEmpty();
}

public String getCountAsString() {
long count = users.size();
if (count == 1) {
return "";
}
return count < 100 ? String.valueOf(count) : "+99";
void setSelectedUserProfile(UserProfile selectedUserProfile) {
this.selectedUserProfile = selectedUserProfile;
updateSelected();
}

private boolean hasReactionBeenRemoved(ChatMessageReaction chatMessageReaction) {
return (chatMessageReaction instanceof PrivateChatMessageReaction
&& ((PrivateChatMessageReaction) chatMessageReaction).isRemoved());
}

public static Comparator<ReactionItem> firstAddedComparator() {
return Comparator.comparingLong(ReactionItem::getFirstAdded);
private void updateSelected() {
selected.set(getUsers().contains(selectedUserProfile));
}
}
Loading
Loading