Skip to content

Commit

Permalink
Add user preference combobox for BSQ block explorer with random default
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz committed Dec 15, 2019
1 parent 14bec2f commit 78a72a2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
44 changes: 34 additions & 10 deletions core/src/main/java/bisq/core/user/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

import lombok.Getter;
Expand Down Expand Up @@ -107,12 +108,18 @@ public final class Preferences implements PersistedDataHost, BridgeAddressProvid
new BlockChainExplorer("BTC DAO-testnet explorer", "https://bisq.network/explorer/btc/dao_testnet/tx/", "https://bisq.network/explorer/btc/dao_testnet/address/")
));

public static final BlockChainExplorer BSQ_MAIN_NET_EXPLORER = new BlockChainExplorer("BSQ", "https://explorer.bisq.network/tx.html?tx=",
"https://explorer.bisq.network/Address.html?addr=");
private static final BlockChainExplorer BSQ_BETA_NET_EXPLORER = new BlockChainExplorer("BSQ", "http://explorer.betanet.bisq.network/tx.html?tx=",
"http://explorer.betanet.bisq.network/Address.html?addr=");
private static final BlockChainExplorer BSQ_TEST_NET_EXPLORER = new BlockChainExplorer("BSQ", "http://explorer.testnet.bisq.network/tx.html?tx=",
"http://explorer.testnet.bisq.network/Address.html?addr=");
public static final ArrayList<BlockChainExplorer> BSQ_MAIN_NET_EXPLORERS = new ArrayList<>(Arrays.asList(
new BlockChainExplorer("bsq.wiz.biz (@wiz)", "https://bsq.wiz.biz/tx.html?tx=", "https://bsq.wiz.biz/Address.html?addr="),
new BlockChainExplorer("explorer.sqrrm.net (@sqrrm)", "https://explorer.sqrrm.net/tx.html?tx=", "https://explorer.sqrrm.net/Address.html?addr=")
));

private static final ArrayList<BlockChainExplorer> BSQ_BETA_NET_EXPLORERS = new ArrayList<>(Collections.singletonList(
new BlockChainExplorer("BSQ", "http://explorer.betanet.bisq.network/tx.html?tx=", "http://explorer.betanet.bisq.network/Address.html?addr=")
));

private static final ArrayList<BlockChainExplorer> BSQ_TEST_NET_EXPLORERS = new ArrayList<>(Collections.singletonList(
new BlockChainExplorer("BSQ", "http://explorer.testnet.bisq.network/tx.html?tx=", "http://explorer.testnet.bisq.network/Address.html?addr=")
));

// payload is initialized so the default values are available for Property initialization.
@Setter
Expand Down Expand Up @@ -213,6 +220,7 @@ public void readPersisted() {
setPreferredTradeCurrency(preferredTradeCurrency);
setFiatCurrencies(prefPayload.getFiatCurrencies());
setCryptoCurrencies(prefPayload.getCryptoCurrencies());
setBsqBlockChainExplorer(prefPayload.getBsqBlockChainExplorer());
} else {
prefPayload = new PreferencesPayload();
prefPayload.setUserLanguage(GlobalSettings.getLocale().getLanguage());
Expand All @@ -233,6 +241,10 @@ public void readPersisted() {
throw new RuntimeException("BaseCurrencyNetwork not defined. BaseCurrencyNetwork=" + baseCurrencyNetwork);
}

// if no preference is set, randomly select a BSQ block explorer for the user
ArrayList<BlockChainExplorer> bsqExplorers = getBsqBlockChainExplorers();
setBsqBlockChainExplorer(bsqExplorers.get((new Random()).nextInt(bsqExplorers.size())));

prefPayload.setDirectoryChooserPath(Utilities.getSystemHomeDirectory());

prefPayload.setOfferBookChartScreenCurrencyCode(preferredTradeCurrency.getCode());
Expand All @@ -241,10 +253,6 @@ public void readPersisted() {
prefPayload.setSellScreenCurrencyCode(preferredTradeCurrency.getCode());
}

prefPayload.setBsqBlockChainExplorer(baseCurrencyNetwork.isMainnet() ? BSQ_MAIN_NET_EXPLORER :
baseCurrencyNetwork.isDaoBetaNet() ? BSQ_BETA_NET_EXPLORER :
BSQ_TEST_NET_EXPLORER);

// We don't want to pass Preferences to all popups where the don't show again checkbox is used, so we use
// that static lookup class to avoid static access to the Preferences directly.
DontShowAgainLookup.setPreferences(this);
Expand Down Expand Up @@ -531,6 +539,11 @@ private void setCryptoCurrencies(List<CryptoCurrency> currencies) {
cryptoCurrenciesAsObservable.setAll(currencies.stream().distinct().collect(Collectors.toList()));
}

public void setBsqBlockChainExplorer(BlockChainExplorer bsqBlockChainExplorer) {
prefPayload.setBsqBlockChainExplorer(bsqBlockChainExplorer);
persist();
}

public void setBlockChainExplorerTestNet(BlockChainExplorer blockChainExplorerTestNet) {
prefPayload.setBlockChainExplorerTestNet(blockChainExplorerTestNet);
persist();
Expand Down Expand Up @@ -720,6 +733,17 @@ public ArrayList<BlockChainExplorer> getBlockChainExplorers() {
}
}

public ArrayList<BlockChainExplorer> getBsqBlockChainExplorers()
{
BaseCurrencyNetwork baseCurrencyNetwork = BisqEnvironment.getBaseCurrencyNetwork();
if (baseCurrencyNetwork.isMainnet())
return BSQ_MAIN_NET_EXPLORERS;
else if (baseCurrencyNetwork.isDaoBetaNet())
return BSQ_BETA_NET_EXPLORERS;
else // testnet
return BSQ_TEST_NET_EXPLORERS;
}

public boolean showAgain(String key) {
return !prefPayload.getDontShowAgainMap().containsKey(key) || !prefPayload.getDontShowAgainMap().get(key);
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/bisq/core/user/PreferencesPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class PreferencesPayload implements PersistableEnvelope {
private List<CryptoCurrency> cryptoCurrencies = new ArrayList<>();
private BlockChainExplorer blockChainExplorerMainNet;
private BlockChainExplorer blockChainExplorerTestNet;
private BlockChainExplorer bsqBlockChainExplorer = Preferences.BSQ_MAIN_NET_EXPLORER;
private BlockChainExplorer bsqBlockChainExplorer;
@Nullable
private String backupDirectory;
private boolean autoSelectArbitrators = true;
Expand Down
1 change: 1 addition & 0 deletions core/src/main/resources/i18n/displayStrings.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,7 @@ settings.tab.about=About

setting.preferences.general=General preferences
setting.preferences.explorer=Bitcoin block explorer
setting.preferences.explorer.bsq=BSQ block explorer
setting.preferences.deviation=Max. deviation from market price
setting.preferences.avoidStandbyMode=Avoid standby mode
setting.preferences.deviationToLarge=Values higher than {0}% are not allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public class PreferencesView extends ActivatableViewAndModel<GridPane, Preferenc
// not supported yet
//private ComboBox<String> btcDenominationComboBox;
private ComboBox<BlockChainExplorer> blockChainExplorerComboBox;
private ComboBox<BlockChainExplorer> bsqBlockChainExplorerComboBox;
private ComboBox<String> userLanguageComboBox;
private ComboBox<Country> userCountryComboBox;
private ComboBox<TradeCurrency> preferredTradeCurrencyComboBox;
Expand Down Expand Up @@ -132,6 +133,7 @@ public class PreferencesView extends ActivatableViewAndModel<GridPane, Preferenc
private Button resetDontShowAgainButton, resyncDaoButton;
// private ListChangeListener<TradeCurrency> displayCurrenciesListChangeListener;
private ObservableList<BlockChainExplorer> blockExplorers;
private ObservableList<BlockChainExplorer> bsqBlockChainExplorers;
private ObservableList<String> languageCodes;
private ObservableList<Country> countries;
private ObservableList<FiatCurrency> fiatCurrencies;
Expand Down Expand Up @@ -181,6 +183,7 @@ public PreferencesView(PreferencesViewModel model,
@Override
public void initialize() {
blockExplorers = FXCollections.observableArrayList(preferences.getBlockChainExplorers());
bsqBlockChainExplorers = FXCollections.observableArrayList(preferences.getBsqBlockChainExplorers());
languageCodes = FXCollections.observableArrayList(LanguageUtil.getUserLanguageCodes());
countries = FXCollections.observableArrayList(CountryUtil.getAllCountries());
fiatCurrencies = preferences.getFiatCurrenciesAsObservable();
Expand Down Expand Up @@ -237,11 +240,17 @@ private void initializeGeneralOptions() {
Res.get("shared.country"));
userCountryComboBox.setButtonCell(GUIUtil.getComboBoxButtonCell(Res.get("shared.country"), userCountryComboBox,
false));

blockChainExplorerComboBox = addComboBox(root, ++gridRow,
Res.get("setting.preferences.explorer"));
blockChainExplorerComboBox.setButtonCell(GUIUtil.getComboBoxButtonCell(Res.get("setting.preferences.explorer"),
blockChainExplorerComboBox, false));

bsqBlockChainExplorerComboBox = addComboBox(root, ++gridRow,
Res.get("setting.preferences.explorer.bsq"));
bsqBlockChainExplorerComboBox.setButtonCell(GUIUtil.getComboBoxButtonCell(Res.get("setting.preferences.explorer.bsq"),
bsqBlockChainExplorerComboBox, false));

Tuple3<Label, InputTextField, ToggleButton> tuple = addTopLabelInputTextFieldSlideToggleButton(root, ++gridRow,
Res.get("setting.preferences.txFee"), Res.get("setting.preferences.useCustomValue"));
transactionFeeInputTextField = tuple.second;
Expand Down Expand Up @@ -732,6 +741,21 @@ public BlockChainExplorer fromString(String string) {
});
blockChainExplorerComboBox.setOnAction(e -> preferences.setBlockChainExplorer(blockChainExplorerComboBox.getSelectionModel().getSelectedItem()));

bsqBlockChainExplorerComboBox.setItems(bsqBlockChainExplorers);
bsqBlockChainExplorerComboBox.getSelectionModel().select(preferences.getBsqBlockChainExplorer());
bsqBlockChainExplorerComboBox.setConverter(new StringConverter<>() {
@Override
public String toString(BlockChainExplorer bsqBlockChainExplorer) {
return bsqBlockChainExplorer.name;
}

@Override
public BlockChainExplorer fromString(String string) {
return null;
}
});
bsqBlockChainExplorerComboBox.setOnAction(e -> preferences.setBsqBlockChainExplorer(bsqBlockChainExplorerComboBox.getSelectionModel().getSelectedItem()));

deviationInputTextField.setText(FormattingUtils.formatPercentagePrice(preferences.getMaxPriceDistanceInPercent()));
deviationInputTextField.textProperty().addListener(deviationListener);
deviationInputTextField.focusedProperty().addListener(deviationFocusedListener);
Expand Down Expand Up @@ -918,6 +942,7 @@ private void deactivateGeneralOptions() {
userLanguageComboBox.setOnAction(null);
userCountryComboBox.setOnAction(null);
blockChainExplorerComboBox.setOnAction(null);
bsqBlockChainExplorerComboBox.setOnAction(null);
deviationInputTextField.textProperty().removeListener(deviationListener);
deviationInputTextField.focusedProperty().removeListener(deviationFocusedListener);
transactionFeeInputTextField.focusedProperty().removeListener(transactionFeeFocusedListener);
Expand Down

0 comments on commit 78a72a2

Please sign in to comment.