From e625a4e54295bbaabc325590b681f3689c8f5ce6 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 15 Dec 2021 09:11:38 +0200 Subject: [PATCH] remove account using tab context menu --- .../sparrowwallet/sparrow/AppController.java | 23 +++++++++++++++---- .../sparrow/event/WalletDeletedEvent.java | 9 ++++++++ .../sparrow/io/CaravanMultisig.java | 2 +- .../sparrow/io/db/DbPersistence.java | 14 +++++++++++ .../sparrow/wallet/WalletForm.java | 8 +++++++ 5 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/event/WalletDeletedEvent.java diff --git a/src/main/java/com/sparrowwallet/sparrow/AppController.java b/src/main/java/com/sparrowwallet/sparrow/AppController.java index a1899b3f..a068c7f5 100644 --- a/src/main/java/com/sparrowwallet/sparrow/AppController.java +++ b/src/main/java/com/sparrowwallet/sparrow/AppController.java @@ -1472,7 +1472,7 @@ public class AppController implements Initializable { TabData tabData = new WalletTabData(TabData.TabType.WALLET, walletForm); subTab.setUserData(tabData); if(!wallet.isWhirlpoolChildWallet()) { - subTab.setContextMenu(getSubTabContextMenu(subTab)); + subTab.setContextMenu(getSubTabContextMenu(wallet, subTabs, subTab)); } subTabs.getTabs().add(subTab); @@ -1702,7 +1702,7 @@ public class AppController implements Initializable { return contextMenu; } - private ContextMenu getSubTabContextMenu(Tab subTab) { + private ContextMenu getSubTabContextMenu(Wallet wallet, TabPane subTabs, Tab subTab) { ContextMenu contextMenu = new ContextMenu(); MenuItem rename = new MenuItem("Rename Account"); rename.setOnAction(event -> { @@ -1713,14 +1713,27 @@ public class AppController implements Initializable { String label = optLabel.get(); subTabLabel.setText(label); - WalletTabData walletTabData = (WalletTabData)subTab.getUserData(); - Wallet wallet = walletTabData.getWallet(); wallet.setLabel(label); EventManager.get().post(new WalletLabelChangedEvent(wallet)); } }); - contextMenu.getItems().add(rename); + + if(!wallet.isMasterWallet() && !wallet.isWhirlpoolChildWallet()) { + MenuItem delete = new MenuItem("Delete Account"); + delete.setOnAction(event -> { + Optional optButtonType = AppServices.showWarningDialog("Delete Wallet Account?", "Labels applied in this wallet account will be lost. Are you sure?", ButtonType.CANCEL, ButtonType.OK); + if(optButtonType.isPresent() && optButtonType.get() == ButtonType.OK) { + subTabs.getTabs().remove(subTab); + if(subTabs.getTabs().size() == 1) { + setSubTabsVisible(subTabs, false); + } + EventManager.get().post(new WalletDeletedEvent(wallet)); + } + }); + contextMenu.getItems().add(delete); + } + return contextMenu; } diff --git a/src/main/java/com/sparrowwallet/sparrow/event/WalletDeletedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/WalletDeletedEvent.java new file mode 100644 index 00000000..a77afffd --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/event/WalletDeletedEvent.java @@ -0,0 +1,9 @@ +package com.sparrowwallet.sparrow.event; + +import com.sparrowwallet.drongo.wallet.Wallet; + +public class WalletDeletedEvent extends WalletChangedEvent { + public WalletDeletedEvent(Wallet wallet) { + super(wallet); + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/io/CaravanMultisig.java b/src/main/java/com/sparrowwallet/sparrow/io/CaravanMultisig.java index 041dd2ad..729595bd 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/CaravanMultisig.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/CaravanMultisig.java @@ -50,7 +50,7 @@ public class CaravanMultisig implements WalletImport, WalletExport { Wallet wallet = new Wallet(); wallet.setName(cf.name); wallet.setPolicyType(PolicyType.MULTI); - ScriptType scriptType = ScriptType.valueOf(cf.addressType); + ScriptType scriptType = ScriptType.valueOf(cf.addressType.replace('-', '_')); for(ExtPublicKey extKey : cf.extendedPublicKeys) { Keystore keystore = new Keystore(extKey.name); diff --git a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java index bd47b823..1bb4e211 100644 --- a/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java +++ b/src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java @@ -196,6 +196,11 @@ public class DbPersistence implements Persistence { jdbi.useHandle(handle -> { WalletDao walletDao = handle.attach(WalletDao.class); try { + if(dirtyPersistables.deleteAccount && !wallet.isMasterWallet()) { + handle.execute("drop schema `" + getSchema(wallet) + "` cascade"); + return; + } + walletDao.setSchema(getSchema(wallet)); if(dirtyPersistables.clearHistory) { @@ -631,6 +636,13 @@ public class DbPersistence implements Persistence { return false; } + @Subscribe + public void walletDeleted(WalletDeletedEvent event) { + if(persistsFor(event.getWallet())) { + dirtyPersistablesMap.computeIfAbsent(event.getWallet(), key -> new DirtyPersistables()).deleteAccount = true; + } + } + @Subscribe public void walletHistoryCleared(WalletHistoryClearedEvent event) { if(persistsFor(event.getWallet())) { @@ -710,6 +722,7 @@ public class DbPersistence implements Persistence { } private static class DirtyPersistables { + public boolean deleteAccount; public boolean clearHistory; public final List historyNodes = new ArrayList<>(); public String label; @@ -725,6 +738,7 @@ public class DbPersistence implements Persistence { public String toString() { return "Dirty Persistables" + + "\nDelete account:" + deleteAccount + "\nClear history:" + clearHistory + "\nNodes:" + historyNodes + "\nLabel:" + label + diff --git a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java index 90bce949..f8688a41 100644 --- a/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java +++ b/src/main/java/com/sparrowwallet/sparrow/wallet/WalletForm.java @@ -519,6 +519,14 @@ public class WalletForm { } } + @Subscribe + public void walletDeleted(WalletDeletedEvent event) { + if(event.getWallet() == wallet && !wallet.isMasterWallet()) { + wallet.getMasterWallet().getChildWallets().remove(wallet); + Platform.runLater(() -> EventManager.get().post(new WalletDataChangedEvent(wallet))); + } + } + @Subscribe public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) { if(event.getWallet() == wallet) {