Skip to content

Commit

Permalink
Merge pull request #1786 from axpoems/add-notification-num-priv-chats
Browse files Browse the repository at this point in the history
Add number of notifications pending to be read in private chats
  • Loading branch information
alvasw authored Mar 13, 2024
2 parents ea07c85 + f8ce877 commit 3e1cac7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ TableCell<MarketChannelItem, MarketChannelItem>> getMarketLabelCellFactory() {
@Override
protected void updateItem(MarketChannelItem item, boolean empty) {
super.updateItem(item, empty);

if (item != null && !empty) {
marketName.setText(item.getMarket().getQuoteCurrencyName());
marketCode.setText(item.getMarket().getQuoteCurrencyCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public final class CommonChatTabController extends ContentTabController<CommonCh
private final CommonPublicChatChannelService commonPublicChatChannelService;
private final TwoPartyPrivateChatChannelService twoPartyPrivateChatChannelService;
private final CommonChannelSelectionService chatChannelSelectionService;
private Pin selectedChannelPin;
private Pin changedChatNotificationPin;
private Pin selectedChannelPin, changedChatNotificationPin;

public CommonChatTabController(ServiceProvider serviceProvider, ChatChannelDomain chatChannelDomain, NavigationTarget navigationTarget) {
super(new CommonChatTabModel(chatChannelDomain), navigationTarget, serviceProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class CommonChatTabView extends ContentTabView<CommonChatTabModel, CommonC
protected void onChildView(View<? extends Parent, ? extends Model, ? extends Controller> oldValue,
View<? extends Parent, ? extends Model, ? extends Controller> newValue) {
super.onChildView(oldValue, newValue);

controller.onSelected(model.getNavigationTarget());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import bisq.chat.ChatChannel;
import bisq.chat.ChatChannelDomain;
import bisq.chat.ChatMessage;
import bisq.chat.notifications.ChatNotification;
import bisq.chat.notifications.ChatNotificationService;
import bisq.chat.two_party.TwoPartyPrivateChatChannel;
import bisq.chat.two_party.TwoPartyPrivateChatChannelService;
import bisq.common.observable.Pin;
Expand All @@ -39,8 +41,9 @@
@Slf4j
public abstract class PrivateChatsController extends ChatController<PrivateChatsView, PrivateChatsModel> {
private final TwoPartyPrivateChatChannelService channelService;
private final ChatNotificationService chatNotificationService;
private final ReputationService reputationService;
private Pin channelItemPin, channelsPin;
private Pin channelItemPin, channelsPin, changedChatNotificationPin;
private Subscription openPrivateChatsPin;

public PrivateChatsController(ServiceProvider serviceProvider,
Expand All @@ -49,6 +52,7 @@ public PrivateChatsController(ServiceProvider serviceProvider,
super(serviceProvider, chatChannelDomain, navigationTarget);

channelService = chatService.getTwoPartyPrivateChatChannelServices().get(chatChannelDomain);
chatNotificationService = serviceProvider.getChatService().getChatNotificationService();
reputationService = serviceProvider.getUserService().getReputationService();
}

Expand All @@ -71,6 +75,9 @@ public void onActivate() {
openPrivateChatsPin = EasyBind.subscribe(model.getNoOpenChats(),
noOpenChats -> chatMessagesComponent.enableChatDialog(!noOpenChats));

chatNotificationService.getNotConsumedNotifications().forEach(this::handleNotification);
changedChatNotificationPin = chatNotificationService.getChangedNotification().addObserver(this::handleNotification);

maybeSelectFirst();
}

Expand All @@ -83,6 +90,7 @@ public void onDeactivate() {
model.getListItems().clear();
resetSelectedChildTarget();
openPrivateChatsPin.unsubscribe();
changedChatNotificationPin.unbind();
}

@Override
Expand Down Expand Up @@ -155,4 +163,24 @@ private void maybeSelectFirst() {
selectionService.selectChannel(model.getSortedList().get(0).getChannel());
}
}

private void handleNotification(ChatNotification notification) {
if (notification == null || notification.getChatChannelDomain() != model.getChatChannelDomain()) {
return;
}

handlePrivateNotification(notification.getChatChannelId());
}

private void handlePrivateNotification(String channelId) {
UIThread.run(() -> {
channelService.findChannel(channelId).ifPresent(channel -> {
long numNotifications = chatNotificationService.getNotConsumedNotifications(channel.getId()).count();
model.getFilteredList().stream()
.filter(listItem -> listItem.getChannel() == channel)
.findAny()
.ifPresent(listItem -> listItem.setNumNotifications(numNotifications));
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.desktop.common.Layout;
import bisq.desktop.common.utils.ImageUtil;
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.controls.Badge;
import bisq.desktop.components.controls.DropdownMenuItem;
import bisq.desktop.components.table.BisqTableColumn;
import bisq.desktop.components.table.BisqTableView;
Expand Down Expand Up @@ -202,6 +203,8 @@ private void configTableView() {

private Callback<TableColumn<ListItem, ListItem>, TableCell<ListItem, ListItem>> getTradePeerCellFactory() {
return column -> new TableCell<>() {
private final HBox hBox = new HBox(5);

@Override
public void updateItem(final ListItem item, boolean empty) {
super.updateItem(item, empty);
Expand All @@ -210,7 +213,9 @@ public void updateItem(final ListItem item, boolean empty) {
UserProfileDisplay userProfileDisplay = new UserProfileDisplay(item.getChannel().getPeer());
userProfileDisplay.setReputationScore(item.getReputationScore());
getStyleClass().add("user-profile-table-cell");
setGraphic(userProfileDisplay);
hBox.getChildren().setAll(userProfileDisplay, Spacer.fillHBox(), item.getNumMessagesBadge());

setGraphic(hBox);
} else {
setGraphic(null);
}
Expand Down Expand Up @@ -269,6 +274,8 @@ static class ListItem {
private final long totalReputationScore, profileAge;
private final String totalReputationScoreString, profileAgeString;
private final ReputationScore reputationScore;
@EqualsAndHashCode.Exclude
private final Badge numMessagesBadge;

public ListItem(TwoPartyPrivateChatChannel channel, ReputationService reputationService) {
this.channel = channel;
Expand All @@ -286,6 +293,12 @@ public ListItem(TwoPartyPrivateChatChannel channel, ReputationService reputation
profileAgeString = optionalProfileAge
.map(TimeFormatter::formatAgeInDays)
.orElse(Res.get("data.na"));

numMessagesBadge = new Badge(null, Pos.CENTER_RIGHT);
}

public void setNumNotifications(long numNotifications) {
numMessagesBadge.setText(numNotifications == 0 ? "" : String.valueOf(numNotifications));
}
}
}
3 changes: 3 additions & 0 deletions apps/desktop/desktop/src/main/resources/css/chat.css
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@
-fx-padding: 0 0 0 13;
}

.private-chats-selection-list .user-profile-table-cell .bisq-badge {
-fx-padding: 0 13 0 0;
}

/*******************************************************************************
* *
Expand Down

0 comments on commit 3e1cac7

Please sign in to comment.