From ccead92388ba6cadee007326f5e6fa98a2dc67ee Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 9 Dec 2020 16:06:30 +0200 Subject: [PATCH] add view option to hide used empty addresses --- .../sparrowwallet/sparrow/AppController.java | 15 +++++++++++++++ .../sparrow/control/AddressTreeTable.java | 6 +++++- .../HideEmptyUsedAddressesStatusEvent.java | 13 +++++++++++++ .../event/WalletAddressesStatusEvent.java | 18 ++++++++++++++++++ .../com/sparrowwallet/sparrow/io/Config.java | 10 ++++++++++ .../com/sparrowwallet/sparrow/io/Storage.java | 4 ++-- .../sparrow/wallet/AddressesController.java | 8 ++++++++ .../sparrow/wallet/NodeEntry.java | 3 ++- .../sparrow/wallet/WalletForm.java | 6 ++++++ .../com/sparrowwallet/sparrow/app.fxml | 2 ++ 10 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/HideEmptyUsedAddressesStatusEvent.java create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesStatusEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index 37353a7c..2daa232c 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -92,6 +92,9 @@ public class AppController implements Initializable { @FXML private CheckMenuItem openWalletsInNewWindows; + @FXML + private CheckMenuItem hideEmptyUsedAddresses; + @FXML private CheckMenuItem showTxHex; @@ -214,6 +217,7 @@ public class AppController implements Initializable { setTheme(null); openWalletsInNewWindows.setSelected(Config.get().isOpenWalletsInNewWindows()); + hideEmptyUsedAddresses.setSelected(Config.get().isHideEmptyUsedAddresses()); showTxHex.setSelected(Config.get().isShowTransactionHex()); exportWallet.setDisable(true); @@ -491,6 +495,12 @@ public class AppController implements Initializable { EventManager.get().post(new OpenWalletsNewWindowsStatusEvent(item.isSelected())); } + public void hideEmptyUsedAddresses(ActionEvent event) { + CheckMenuItem item = (CheckMenuItem)event.getSource(); + Config.get().setHideEmptyUsedAddresses(item.isSelected()); + EventManager.get().post(new HideEmptyUsedAddressesStatusEvent(item.isSelected())); + } + public void showTxHex(ActionEvent event) { CheckMenuItem item = (CheckMenuItem)event.getSource(); Config.get().setShowTransactionHex(item.isSelected()); @@ -1260,6 +1270,11 @@ public class AppController implements Initializable { openWalletsInNewWindows.setSelected(event.isOpenWalletsInNewWindows()); } + @Subscribe + public void hideEmptyUsedAddressesStatusChanged(HideEmptyUsedAddressesStatusEvent event) { + hideEmptyUsedAddresses.setSelected(event.isHideEmptyUsedAddresses()); + } + @Subscribe public void requestOpenWallets(RequestOpenWalletsEvent event) { EventManager.get().post(new OpenWalletsEvent(tabs.getScene().getWindow(), getOpenWallets())); diff --git a/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java b/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java index 1f80ca21..10070f57 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/AddressTreeTable.java @@ -14,6 +14,7 @@ import javafx.scene.input.MouseButton; import java.util.List; import java.util.Optional; +import java.util.OptionalInt; public class AddressTreeTable extends CoinTreeTable { public void initialize(NodeEntry rootEntry) { @@ -60,7 +61,10 @@ public class AddressTreeTable extends CoinTreeTable { Integer highestUsedIndex = rootEntry.getNode().getHighestUsedIndex(); if(highestUsedIndex != null) { - scrollTo(highestUsedIndex); + OptionalInt tableIndex = rootEntry.getChildren().stream().filter(childEntry -> ((NodeEntry)childEntry).getNode().getIndex() == highestUsedIndex + 1).mapToInt(childEntry -> rootEntry.getChildren().indexOf(childEntry)).findFirst(); + if(tableIndex.isPresent() && tableIndex.getAsInt() > 5) { + scrollTo(tableIndex.getAsInt()); + } } setOnMouseClicked(mouseEvent -> { diff --git a/src/main/java/com/sparrowwallet/sparrow/event/HideEmptyUsedAddressesStatusEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/HideEmptyUsedAddressesStatusEvent.java new file mode 100644 index 00000000..df5341ac --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/HideEmptyUsedAddressesStatusEvent.java @@ -0,0 +1,13 @@ +package com.sparrowwallet.sparrow.event; + +public class HideEmptyUsedAddressesStatusEvent { + private final boolean hideEmptyUsedAddresses; + + public HideEmptyUsedAddressesStatusEvent(boolean hideEmptyUsedAddresses) { + this.hideEmptyUsedAddresses = hideEmptyUsedAddresses; + } + + public boolean isHideEmptyUsedAddresses() { + return hideEmptyUsedAddresses; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesStatusEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesStatusEvent.java new file mode 100644 index 00000000..44fc05c2 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletAddressesStatusEvent.java @@ -0,0 +1,18 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.wallet.Wallet; + +/** + * Used to indicate that the display configuration of wallet addresses has been updated + */ +public class WalletAddressesStatusEvent { + private final Wallet wallet; + + public WalletAddressesStatusEvent(Wallet wallet) { + this.wallet = wallet; + } + + public Wallet getWallet() { + return wallet; + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Config.java b/src/main/java/com/sparrowwallet/sparrow/io/Config.java index 316e09cb..d5eee569 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Config.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Config.java @@ -32,6 +32,7 @@ public class Config { private boolean checkNewVersions = true; private Theme theme; private boolean openWalletsInNewWindows = false; + private boolean hideEmptyUsedAddresses = false; private boolean showTransactionHex = true; private List recentWalletFiles; private Integer keyDerivationPeriod; @@ -195,6 +196,15 @@ public class Config { flush(); } + public boolean isHideEmptyUsedAddresses() { + return hideEmptyUsedAddresses; + } + + public void setHideEmptyUsedAddresses(boolean hideEmptyUsedAddresses) { + this.hideEmptyUsedAddresses = hideEmptyUsedAddresses; + flush(); + } + public boolean isShowTransactionHex() { return showTransactionHex; } diff --git a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java index 3908b60c..9c1e7b69 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/Storage.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/Storage.java @@ -248,8 +248,8 @@ public class Storage { } public static boolean walletExists(String walletName) { - File encrypted = new File(getWalletsDir(), walletName); - File unencrypted = new File(getWalletsDir(), walletName + ".json"); + File encrypted = new File(getWalletsDir(), walletName.trim()); + File unencrypted = new File(getWalletsDir(), walletName.trim() + ".json"); return (encrypted.exists() || unencrypted.exists()); } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java index 92d9cfbb..2fd91901 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/AddressesController.java @@ -75,4 +75,12 @@ public class AddressesController extends WalletFormController implements Initial changeTable.refresh(); } } + + @Subscribe + public void walletAddressesStatusChanged(WalletAddressesStatusEvent event) { + if(event.getWallet().equals(walletForm.getWallet())) { + receiveTable.updateAll(getWalletForm().getNodeEntry(KeyPurpose.RECEIVE)); + changeTable.updateAll(getWalletForm().getNodeEntry(KeyPurpose.CHANGE)); + } + } } diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java index 363f1cd0..e64e6ca0 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/NodeEntry.java @@ -6,6 +6,7 @@ import com.sparrowwallet.drongo.wallet.Wallet; import com.sparrowwallet.drongo.wallet.WalletNode; import com.sparrowwallet.sparrow.EventManager; import com.sparrowwallet.sparrow.event.WalletEntryLabelChangedEvent; +import com.sparrowwallet.sparrow.io.Config; import java.util.stream.Collectors; @@ -15,7 +16,7 @@ public class NodeEntry extends Entry implements Comparable { public NodeEntry(Wallet wallet, WalletNode node) { super(wallet, node.getLabel(), !node.getChildren().isEmpty() ? - node.getChildren().stream().map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList()) : + node.getChildren().stream().filter(childNode -> !Config.get().isHideEmptyUsedAddresses() || childNode.getTransactionOutputs().isEmpty() || !childNode.getUnspentTransactionOutputs().isEmpty()).map(childNode -> new NodeEntry(wallet, childNode)).collect(Collectors.toList()) : node.getTransactionOutputs().stream().map(txo -> new HashIndexEntry(wallet, txo, HashIndexEntry.Type.OUTPUT, node.getKeyPurpose())).collect(Collectors.toList())); this.node = node; diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index a2167dc4..1f0b58ac 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -262,4 +262,10 @@ public class WalletForm { } } } + + @Subscribe + public void hideEmptyUsedAddressesStatusChanged(HideEmptyUsedAddressesStatusEvent event) { + accountEntries.clear(); + EventManager.get().post(new WalletAddressesStatusEvent(wallet)); + } } diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.fxml b/src/main/resources/com/sparrowwallet/sparrow/app.fxml index 3c5e5c23..1ba0eac3 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/app.fxml @@ -78,7 +78,9 @@ + +