remove account using tab context menu

This commit is contained in:
Craig Raw 2021-12-15 09:11:38 +02:00
parent 72ff1df61e
commit e625a4e542
5 changed files with 50 additions and 6 deletions

View file

@ -1472,7 +1472,7 @@ public class AppController implements Initializable {
TabData tabData = new WalletTabData(TabData.TabType.WALLET, walletForm); TabData tabData = new WalletTabData(TabData.TabType.WALLET, walletForm);
subTab.setUserData(tabData); subTab.setUserData(tabData);
if(!wallet.isWhirlpoolChildWallet()) { if(!wallet.isWhirlpoolChildWallet()) {
subTab.setContextMenu(getSubTabContextMenu(subTab)); subTab.setContextMenu(getSubTabContextMenu(wallet, subTabs, subTab));
} }
subTabs.getTabs().add(subTab); subTabs.getTabs().add(subTab);
@ -1702,7 +1702,7 @@ public class AppController implements Initializable {
return contextMenu; return contextMenu;
} }
private ContextMenu getSubTabContextMenu(Tab subTab) { private ContextMenu getSubTabContextMenu(Wallet wallet, TabPane subTabs, Tab subTab) {
ContextMenu contextMenu = new ContextMenu(); ContextMenu contextMenu = new ContextMenu();
MenuItem rename = new MenuItem("Rename Account"); MenuItem rename = new MenuItem("Rename Account");
rename.setOnAction(event -> { rename.setOnAction(event -> {
@ -1713,14 +1713,27 @@ public class AppController implements Initializable {
String label = optLabel.get(); String label = optLabel.get();
subTabLabel.setText(label); subTabLabel.setText(label);
WalletTabData walletTabData = (WalletTabData)subTab.getUserData();
Wallet wallet = walletTabData.getWallet();
wallet.setLabel(label); wallet.setLabel(label);
EventManager.get().post(new WalletLabelChangedEvent(wallet)); EventManager.get().post(new WalletLabelChangedEvent(wallet));
} }
}); });
contextMenu.getItems().add(rename); contextMenu.getItems().add(rename);
if(!wallet.isMasterWallet() && !wallet.isWhirlpoolChildWallet()) {
MenuItem delete = new MenuItem("Delete Account");
delete.setOnAction(event -> {
Optional<ButtonType> 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; return contextMenu;
} }

View file

@ -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);
}
}

View file

@ -50,7 +50,7 @@ public class CaravanMultisig implements WalletImport, WalletExport {
Wallet wallet = new Wallet(); Wallet wallet = new Wallet();
wallet.setName(cf.name); wallet.setName(cf.name);
wallet.setPolicyType(PolicyType.MULTI); wallet.setPolicyType(PolicyType.MULTI);
ScriptType scriptType = ScriptType.valueOf(cf.addressType); ScriptType scriptType = ScriptType.valueOf(cf.addressType.replace('-', '_'));
for(ExtPublicKey extKey : cf.extendedPublicKeys) { for(ExtPublicKey extKey : cf.extendedPublicKeys) {
Keystore keystore = new Keystore(extKey.name); Keystore keystore = new Keystore(extKey.name);

View file

@ -196,6 +196,11 @@ public class DbPersistence implements Persistence {
jdbi.useHandle(handle -> { jdbi.useHandle(handle -> {
WalletDao walletDao = handle.attach(WalletDao.class); WalletDao walletDao = handle.attach(WalletDao.class);
try { try {
if(dirtyPersistables.deleteAccount && !wallet.isMasterWallet()) {
handle.execute("drop schema `" + getSchema(wallet) + "` cascade");
return;
}
walletDao.setSchema(getSchema(wallet)); walletDao.setSchema(getSchema(wallet));
if(dirtyPersistables.clearHistory) { if(dirtyPersistables.clearHistory) {
@ -631,6 +636,13 @@ public class DbPersistence implements Persistence {
return false; return false;
} }
@Subscribe
public void walletDeleted(WalletDeletedEvent event) {
if(persistsFor(event.getWallet())) {
dirtyPersistablesMap.computeIfAbsent(event.getWallet(), key -> new DirtyPersistables()).deleteAccount = true;
}
}
@Subscribe @Subscribe
public void walletHistoryCleared(WalletHistoryClearedEvent event) { public void walletHistoryCleared(WalletHistoryClearedEvent event) {
if(persistsFor(event.getWallet())) { if(persistsFor(event.getWallet())) {
@ -710,6 +722,7 @@ public class DbPersistence implements Persistence {
} }
private static class DirtyPersistables { private static class DirtyPersistables {
public boolean deleteAccount;
public boolean clearHistory; public boolean clearHistory;
public final List<WalletNode> historyNodes = new ArrayList<>(); public final List<WalletNode> historyNodes = new ArrayList<>();
public String label; public String label;
@ -725,6 +738,7 @@ public class DbPersistence implements Persistence {
public String toString() { public String toString() {
return "Dirty Persistables" + return "Dirty Persistables" +
"\nDelete account:" + deleteAccount +
"\nClear history:" + clearHistory + "\nClear history:" + clearHistory +
"\nNodes:" + historyNodes + "\nNodes:" + historyNodes +
"\nLabel:" + label + "\nLabel:" + label +

View file

@ -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 @Subscribe
public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) { public void walletUtxoStatusChanged(WalletUtxoStatusChangedEvent event) {
if(event.getWallet() == wallet) { if(event.getWallet() == wallet) {