Skip to content

Commit

Permalink
Merge pull request #6348 from Android-X13/update-peer-tags
Browse files Browse the repository at this point in the history
Refresh all avatars upon setting a peer's tag
  • Loading branch information
ripcurlx authored Sep 21, 2022
2 parents e2948e2 + 1bbe90a commit 5d06b51
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 53 deletions.
52 changes: 26 additions & 26 deletions desktop/src/main/java/bisq/desktop/components/PeerInfoIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,21 @@

import javafx.geometry.Point2D;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import java.util.Map;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Nullable;

@Slf4j
public class PeerInfoIcon extends Group {
public interface notify {
void avatarTagUpdated();
}

@Setter
private notify callback;
protected Preferences preferences;
protected final String fullAddress;
protected String tooltipText;
Expand All @@ -68,10 +65,12 @@ public interface notify {
protected Pane tagPane;
protected Pane numTradesPane;
protected int numTrades = 0;
private final StringProperty tag;

public PeerInfoIcon(NodeAddress nodeAddress, Preferences preferences) {
this.preferences = preferences;
this.fullAddress = nodeAddress != null ? nodeAddress.getFullAddress() : "";
this.tag = new SimpleStringProperty("");
}

protected void createAvatar(Color ringColor) {
Expand Down Expand Up @@ -184,10 +183,7 @@ protected void addMouseListener(int numTrades,
.position(localToScene(new Point2D(0, 0)))
.onSave(newTag -> {
preferences.setTagForPeer(fullAddress, newTag);
updatePeerInfoIcon();
if (callback != null) {
callback.avatarTagUpdated();
}
tag.set(newTag);
})
.show();
}
Expand All @@ -205,20 +201,6 @@ protected String getAccountAgeTooltip(Long accountAge) {
}

protected void updatePeerInfoIcon() {
String tag;
Map<String, String> peerTagMap = preferences.getPeerTagMap();
if (peerTagMap.containsKey(fullAddress)) {
tag = peerTagMap.get(fullAddress);
final String text = !tag.isEmpty() ? Res.get("peerInfoIcon.tooltip", tooltipText, tag) : tooltipText;
Tooltip.install(this, new Tooltip(text));
} else {
tag = "";
Tooltip.install(this, new Tooltip(tooltipText));
}

if (!tag.isEmpty())
tagLabel.setText(tag.substring(0, 1));

if (numTrades > 0) {
numTradesLabel.setText(numTrades > 99 ? "*" : String.valueOf(numTrades));

Expand All @@ -229,9 +211,27 @@ protected void updatePeerInfoIcon() {
numTradesLabel.relocate(scaleFactor * 5, scaleFactor * 1);
}
}

numTradesPane.setVisible(numTrades > 0);

tagPane.setVisible(!tag.isEmpty());
refreshTag();
}

protected void refreshTag() {
Map<String, String> peerTagMap = preferences.getPeerTagMap();
if (peerTagMap.containsKey(fullAddress)) {
tag.set(peerTagMap.get(fullAddress));
}

Tooltip.install(this, new Tooltip(!tag.get().isEmpty() ?
Res.get("peerInfoIcon.tooltip", tooltipText, tag.get()) : tooltipText));

if (!tag.get().isEmpty()) {
tagLabel.setText(tag.get().substring(0, 1));
}
tagPane.setVisible(!tag.get().isEmpty());
}

protected StringProperty tagProperty() {
return tag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,4 @@ public PeerInfoIconDispute(NodeAddress nodeAddress,
addMouseListener(numTrades, null, null, null, preferences, false,
false, accountAge, 0L, null, null, null);
}

public void refreshTag() {
updatePeerInfoIcon();
}
}
48 changes: 48 additions & 0 deletions desktop/src/main/java/bisq/desktop/components/PeerInfoIconMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.components;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

import java.util.HashMap;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class PeerInfoIconMap extends HashMap<String, PeerInfoIcon> implements ChangeListener<String> {

@Override
public PeerInfoIcon put(String key, PeerInfoIcon icon) {
icon.tagProperty().addListener(this);
return super.put(key, icon);
}

@Override
public void changed(ObservableValue<? extends String> o, String oldVal, String newVal) {
log.info("Updating avatar tags, the avatar map size is {}", size());
forEach((key, icon) -> {
// We update all avatars, as some could be sharing the same tag.
// We also temporarily remove listeners to prevent firing of
// events while each icon's tagProperty is being reset.
icon.tagProperty().removeListener(this);
icon.refreshTag();
icon.tagProperty().addListener(this);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import bisq.desktop.components.ColoredDecimalPlacesWithZerosText;
import bisq.desktop.components.HyperlinkWithIcon;
import bisq.desktop.components.InfoAutoTooltipLabel;
import bisq.desktop.components.PeerInfoIconMap;
import bisq.desktop.components.PeerInfoIconTrading;
import bisq.desktop.components.TitledGroupBg;
import bisq.desktop.main.MainView;
Expand Down Expand Up @@ -152,6 +153,8 @@ abstract public class OfferBookView<R extends GridPane, M extends OfferBookViewM
private static final int SHOW_ALL = 0;
private Label disabledCreateOfferButtonTooltip;
protected VBox currencyComboBoxContainer;
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final PeerInfoIconMap avatarMap = new PeerInfoIconMap();

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
Expand Down Expand Up @@ -485,6 +488,8 @@ protected void deactivate() {
model.priceFeedService.updateCounterProperty().removeListener(priceFeedUpdateCounterListener);

currencySelectionSubscriber.unsubscribe();

avatarMap.clear();
}

static class CurrencyStringConverter extends StringConverter<TradeCurrency> {
Expand Down Expand Up @@ -1278,6 +1283,8 @@ public void updateItem(final OfferBookListItem newItem, boolean empty) {
model.preferences,
model.accountAgeWitnessService,
useDevPrivilegeKeys);
String key = offer.getId();
avatarMap.put(key, peerInfoIcon);
setGraphic(peerInfoIcon);
} else {
setGraphic(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.desktop.components.AutoTooltipButton;
import bisq.desktop.components.AutoTooltipLabel;
import bisq.desktop.components.HyperlinkWithIcon;
import bisq.desktop.components.PeerInfoIconMap;
import bisq.desktop.components.PeerInfoIconTrading;
import bisq.desktop.components.list.FilterBox;
import bisq.desktop.main.overlays.windows.BsqTradeDetailsWindow;
Expand All @@ -46,7 +47,6 @@

import javafx.stage.Stage;

import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
Expand Down Expand Up @@ -129,6 +129,8 @@ public String toString() {
private SortedList<UnconfirmedBsqSwapsListItem> sortedList;
private FilteredList<UnconfirmedBsqSwapsListItem> filteredList;
private ChangeListener<Number> widthListener;
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final PeerInfoIconMap avatarMap = new PeerInfoIconMap();

@Inject
public UnconfirmedBsqSwapsView(UnconfirmedBsqSwapsViewModel model,
Expand Down Expand Up @@ -259,6 +261,8 @@ protected void deactivate() {

filterBox.deactivate();
root.widthProperty().removeListener(widthListener);

avatarMap.clear();
}

private static <T extends Comparable<T>> Comparator<UnconfirmedBsqSwapsListItem> nullsFirstComparing(
Expand Down Expand Up @@ -400,14 +404,16 @@ public void updateItem(final UnconfirmedBsqSwapsListItem newItem, boolean empty)
int numPastTrades = newItem.getNumPastTrades();
final NodeAddress tradingPeerNodeAddress = bsqSwapTrade.getTradingPeerNodeAddress();
String role = Res.get("peerInfoIcon.tooltip.tradePeer");
Node peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
PeerInfoIconTrading peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
role,
numPastTrades,
privateNotificationManager,
bsqSwapTrade.getOffer(),
preferences,
accountAgeWitnessService,
useDevPrivilegeKeys);
String key = bsqSwapTrade.getId();
avatarMap.put(key, peerInfoIcon);
setPadding(new Insets(1, 15, 0, 0));
setGraphic(peerInfoIcon);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.desktop.components.AutoTooltipLabel;
import bisq.desktop.components.AutoTooltipTableColumn;
import bisq.desktop.components.HyperlinkWithIcon;
import bisq.desktop.components.PeerInfoIconMap;
import bisq.desktop.components.PeerInfoIconTrading;
import bisq.desktop.components.list.FilterBox;
import bisq.desktop.main.overlays.popups.Popup;
Expand Down Expand Up @@ -63,7 +64,6 @@

import javafx.stage.Stage;

import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
Expand Down Expand Up @@ -158,6 +158,8 @@ public String toString() {
private SortedList<ClosedTradesListItem> sortedList;
private FilteredList<ClosedTradesListItem> filteredList;
private ChangeListener<Number> widthListener;
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final PeerInfoIconMap avatarMap = new PeerInfoIconMap();

@Inject
public ClosedTradesView(ClosedTradesViewModel model,
Expand Down Expand Up @@ -344,6 +346,8 @@ protected void deactivate() {

filterBox.deactivate();
root.widthProperty().removeListener(widthListener);

avatarMap.clear();
}

private static <T extends Comparable<T>> Comparator<ClosedTradesListItem> nullsFirstComparing(Function<ClosedTradesListItem, T> keyExtractor) {
Expand Down Expand Up @@ -528,14 +532,16 @@ public void updateItem(final ClosedTradesListItem item, boolean empty) {
int numPastTrades = item.getNumPastTrades();
NodeAddress tradingPeerNodeAddress = tradeModel.getTradingPeerNodeAddress();
String role = Res.get("peerInfoIcon.tooltip.tradePeer");
Node peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
PeerInfoIconTrading peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
role,
numPastTrades,
privateNotificationManager,
tradeModel,
preferences,
model.dataModel.accountAgeWitnessService,
useDevPrivilegeKeys);
String key = tradeModel.getId();
avatarMap.put(key, peerInfoIcon);
setPadding(new Insets(1, 15, 0, 0));
setGraphic(peerInfoIcon);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.desktop.common.view.FxmlView;
import bisq.desktop.components.AutoTooltipLabel;
import bisq.desktop.components.HyperlinkWithIcon;
import bisq.desktop.components.PeerInfoIconMap;
import bisq.desktop.components.PeerInfoIconTrading;
import bisq.desktop.components.list.FilterBox;
import bisq.desktop.main.MainView;
Expand Down Expand Up @@ -68,7 +69,6 @@
import javafx.stage.StageStyle;
import javafx.stage.Window;

import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
Expand Down Expand Up @@ -152,6 +152,8 @@ public interface ChatCallback {
private ChangeListener<Trade.DisputeState> disputeStateListener;
private ChangeListener<MediationResultState> mediationResultStateListener;
private ChangeListener<Number> getMempoolStatusListener;
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final PeerInfoIconMap avatarMap = new PeerInfoIconMap();


///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -357,6 +359,8 @@ protected void deactivate() {

if (scene != null)
scene.removeEventHandler(KeyEvent.KEY_RELEASED, keyEventEventHandler);

avatarMap.clear();
}

private void removeSelectedSubView() {
Expand Down Expand Up @@ -829,14 +833,16 @@ public void updateItem(PendingTradesListItem newItem, boolean empty) {
final NodeAddress tradingPeerNodeAddress = trade.getTradingPeerNodeAddress();
int numPastTrades = model.getNumPastTrades(trade);
String role = Res.get("peerInfoIcon.tooltip.tradePeer");
Node peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
PeerInfoIconTrading peerInfoIcon = new PeerInfoIconTrading(tradingPeerNodeAddress,
role,
numPastTrades,
privateNotificationManager,
trade,
preferences,
model.accountAgeWitnessService,
useDevPrivilegeKeys);
String key = trade.getId();
avatarMap.put(key, peerInfoIcon);
setPadding(new Insets(1, 0, 0, 0));
setGraphic(peerInfoIcon);
} else {
Expand Down
Loading

0 comments on commit 5d06b51

Please sign in to comment.