From a167f6aedb76be5c21bf0155ccd472523bcd0777 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Tue, 16 Apr 2024 12:31:56 +0200 Subject: [PATCH] add show all wallets summary --- .../sparrowwallet/sparrow/AppController.java | 21 ++++++- .../sparrow/control/WalletSummaryDialog.java | 62 ++++++++++++++++--- .../com/sparrowwallet/sparrow/app.fxml | 2 + 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index e5b66109..8cd268fd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1615,6 +1615,25 @@ public class AppController implements Initializable { } } + public void showAllWalletsSummary(ActionEvent event) { + List> allWalletForms = new ArrayList<>(); + for(Tab tab : tabs.getTabs()) { + if(tab.getUserData() instanceof WalletTabData) { + TabPane subTabs = (TabPane)tab.getContent(); + allWalletForms.add(subTabs.getTabs().stream().map(subTab -> ((WalletTabData)subTab.getUserData()).getWalletForm()) + .filter(walletForm -> walletForm.getWallet().isValid() && !walletForm.isLocked()).collect(Collectors.toList())); + } + } + + if(allWalletForms.isEmpty() || allWalletForms.stream().allMatch(List::isEmpty)) { + showErrorDialog("No wallets", "There are no open and unlocked wallets to summarize."); + } else { + WalletSummaryDialog walletSummaryDialog = new WalletSummaryDialog(allWalletForms); + walletSummaryDialog.initOwner(rootStack.getScene().getWindow()); + walletSummaryDialog.showAndWait(); + } + } + public void showWalletSummary(ActionEvent event) { Tab selectedTab = tabs.getSelectionModel().getSelectedItem(); if(selectedTab != null) { @@ -1623,7 +1642,7 @@ public class AppController implements Initializable { TabPane subTabs = (TabPane) selectedTab.getContent(); List walletForms = subTabs.getTabs().stream().map(subTab -> ((WalletTabData)subTab.getUserData()).getWalletForm()).collect(Collectors.toList()); if(!walletForms.isEmpty()) { - WalletSummaryDialog walletSummaryDialog = new WalletSummaryDialog(walletForms); + WalletSummaryDialog walletSummaryDialog = new WalletSummaryDialog(List.of(walletForms)); walletSummaryDialog.initOwner(rootStack.getScene().getWindow()); walletSummaryDialog.showAndWait(); } diff --git a/src/main/java/com/sparrowwallet/sparrow/control/WalletSummaryDialog.java b/src/main/java/com/sparrowwallet/sparrow/control/WalletSummaryDialog.java index 2fb67422..ea69bb9b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/control/WalletSummaryDialog.java +++ b/src/main/java/com/sparrowwallet/sparrow/control/WalletSummaryDialog.java @@ -18,17 +18,21 @@ import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; public class WalletSummaryDialog extends Dialog { - public WalletSummaryDialog(List walletForms) { - if(walletForms.isEmpty()) { + public WalletSummaryDialog(List> summaryWalletFormsList) { + List> walletFormsList = new ArrayList<>(summaryWalletFormsList); + walletFormsList.removeIf(List::isEmpty); + if(walletFormsList.isEmpty()) { throw new IllegalArgumentException("No wallets selected to summarize"); } - Wallet masterWallet = walletForms.get(0).getMasterWallet(); + boolean allOpenWallets = walletFormsList.size() > 1; + List masterWallets = walletFormsList.stream().map(walletForms -> walletForms.get(0).getMasterWallet()).toList(); final DialogPane dialogPane = getDialogPane(); dialogPane.getStylesheets().add(AppServices.class.getResource("general.css").toExternalForm()); @@ -37,7 +41,7 @@ public class WalletSummaryDialog extends Dialog { dialogPane.getStylesheets().add(AppServices.class.getResource("wallet/transactions.css").toExternalForm()); AppServices.setStageIcon(dialogPane.getScene().getWindow()); - dialogPane.setHeaderText("Wallet Summary for " + masterWallet.getName()); + dialogPane.setHeaderText("Wallet Summary for " + (allOpenWallets ? "All Open Wallets" : masterWallets.get(0).getName())); Image image = new Image("image/sparrow-small.png", 50, 50, false, false); if(!image.isError()) { @@ -64,7 +68,7 @@ public class WalletSummaryDialog extends Dialog { }); balanceColumn.setCellFactory(p -> new CoinCell()); table.getColumns().add(balanceColumn); - table.setUnitFormat(masterWallet); + table.setUnitFormat(masterWallets.get(0)); CurrencyRate currencyRate = AppServices.getFiatCurrencyExchangeRate(); if(currencyRate != null && currencyRate.isAvailable() && Config.get().getExchangeSource() != ExchangeSource.NONE) { @@ -77,12 +81,20 @@ public class WalletSummaryDialog extends Dialog { table.setCurrencyRate(currencyRate); } - SummaryEntry rootEntry = new SummaryEntry(walletForms); + Entry rootEntry = allOpenWallets ? new AllSummaryEntry(walletFormsList) : new SummaryEntry(walletFormsList.get(0)); TreeItem rootItem = new TreeItem<>(rootEntry); for(Entry childEntry : rootEntry.getChildren()) { TreeItem childItem = new TreeItem<>(childEntry); rootItem.getChildren().add(childItem); - childItem.getChildren().add(new TreeItem<>(new UnconfirmedEntry((WalletTransactionsEntry)childEntry))); + if(allOpenWallets) { + for(Entry walletEntry : childEntry.getChildren()) { + TreeItem walletItem = new TreeItem<>(walletEntry); + childItem.getChildren().add(walletItem); + walletItem.getChildren().add(new TreeItem<>(new UnconfirmedEntry((WalletTransactionsEntry)walletEntry))); + } + } else { + childItem.getChildren().add(new TreeItem<>(new UnconfirmedEntry((WalletTransactionsEntry)childEntry))); + } } table.setShowRoot(true); @@ -97,6 +109,14 @@ public class WalletSummaryDialog extends Dialog { hBox.getChildren().add(vBox); + Wallet balanceWallet; + if(allOpenWallets) { + balanceWallet = new Wallet(); + balanceWallet.getChildWallets().addAll(masterWallets.stream().flatMap(mws -> mws.getAllWallets().stream()).toList()); + } else { + balanceWallet = masterWallets.get(0); + } + NumberAxis xAxis = new NumberAxis(); xAxis.setSide(Side.BOTTOM); xAxis.setForceZeroInRange(false); @@ -104,7 +124,7 @@ public class WalletSummaryDialog extends Dialog { NumberAxis yAxis = new NumberAxis(); yAxis.setSide(Side.LEFT); BalanceChart balanceChart = new BalanceChart(xAxis, yAxis); - balanceChart.initialize(new WalletTransactionsEntry(masterWallet, true)); + balanceChart.initialize(new WalletTransactionsEntry(balanceWallet, true)); balanceChart.setAnimated(false); balanceChart.setLegendVisible(false); balanceChart.setVerticalGridLinesVisible(false); @@ -120,6 +140,32 @@ public class WalletSummaryDialog extends Dialog { AppServices.moveToActiveWindowScreen(this); } + public static class AllSummaryEntry extends Entry { + private AllSummaryEntry(List> walletFormsList) { + super(null, "All Wallets", walletFormsList.stream().map(SummaryEntry::new).collect(Collectors.toList())); + } + + @Override + public Long getValue() { + long value = 0; + for(Entry entry : getChildren()) { + value += entry.getValue(); + } + + return value; + } + + @Override + public String getEntryType() { + return null; + } + + @Override + public Function getWalletFunction() { + return Function.TRANSACTIONS; + } + } + public static class SummaryEntry extends Entry { private SummaryEntry(List walletForms) { super(walletForms.get(0).getWallet(), walletForms.get(0).getWallet().getName(), walletForms.stream().map(WalletForm::getWalletTransactionsEntry).collect(Collectors.toList())); diff --git a/src/main/resources/com/sparrowwallet/sparrow/app.fxml b/src/main/resources/com/sparrowwallet/sparrow/app.fxml index 56fb044f..42847896 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/app.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/app.fxml @@ -128,6 +128,8 @@ + +