fix export from settings tabs for new account by ensuring bidirectional links are restored on save

This commit is contained in:
Craig Raw 2022-11-08 15:51:59 +02:00
parent 6990b398c2
commit 2972f1a4d7
2 changed files with 11 additions and 13 deletions

View file

@ -458,18 +458,10 @@ public class SettingsController extends WalletFormController implements Initiali
} }
Optional<Wallet> optWallet = AppServices.get().getOpenWallets().entrySet().stream() Optional<Wallet> optWallet = AppServices.get().getOpenWallets().entrySet().stream()
.filter(entry -> walletForm.getWalletFile().equals(entry.getValue().getWalletFile()) && entry.getKey().isMasterWallet()).map(Map.Entry::getKey).findFirst(); .filter(entry -> walletForm.getWalletFile().equals(entry.getValue().getWalletFile())
&& entry.getKey().getName().equals(walletForm.getWallet().getName())).map(Map.Entry::getKey).findFirst();
if(optWallet.isPresent()) { if(optWallet.isPresent()) {
Wallet wallet = optWallet.get(); WalletExportDialog dlg = new WalletExportDialog(optWallet.get());
if(!walletForm.getWallet().getName().equals(wallet.getName())) {
wallet = wallet.getChildWallet(walletForm.getWallet().getName());
}
if(wallet == null) {
throw new IllegalStateException("Cannot find child wallet " + walletForm.getWallet().getFullDisplayName() + " to export");
}
WalletExportDialog dlg = new WalletExportDialog(wallet);
dlg.showAndWait(); dlg.showAndWait();
} else { } else {
AppServices.showErrorDialog("Cannot export wallet", "Wallet cannot be exported, please save it first."); AppServices.showErrorDialog("Cannot export wallet", "Wallet cannot be exported, please save it first.");

View file

@ -60,15 +60,21 @@ public class SettingsWalletForm extends WalletForm {
//Clear node tree, detaching and saving any labels from the existing wallet //Clear node tree, detaching and saving any labels from the existing wallet
walletCopy.clearNodes(wallet); walletCopy.clearNodes(wallet);
//Retrieve master or child wallets from current active wallet before overwriting
Wallet masterWallet = wallet.isMasterWallet() ? null : wallet.getMasterWallet();
Integer childIndex = wallet.isMasterWallet() ? null : wallet.getMasterWallet().getChildWallets().indexOf(wallet); Integer childIndex = wallet.isMasterWallet() ? null : wallet.getMasterWallet().getChildWallets().indexOf(wallet);
List<Wallet> childWallets = wallet.getChildWallets();
//Replace the SettingsWalletForm wallet reference - note that this reference is only shared with the WalletForm wallet with WalletAddressesChangedEvent below //Replace the SettingsWalletForm wallet reference - note that this reference is only shared with the WalletForm wallet with WalletAddressesChangedEvent below
wallet = walletCopy.copy(); wallet = walletCopy.copy();
//Restore bidirectional links between original master or child wallets
if(wallet.isMasterWallet()) { if(wallet.isMasterWallet()) {
wallet.setChildWallets(childWallets);
wallet.getChildWallets().forEach(childWallet -> childWallet.setMasterWallet(wallet)); wallet.getChildWallets().forEach(childWallet -> childWallet.setMasterWallet(wallet));
} else if(childIndex != null) { } else if(masterWallet != null && childIndex != null) {
wallet.getMasterWallet().getChildWallets().set(childIndex, wallet); wallet.setMasterWallet(masterWallet);
masterWallet.getChildWallets().set(childIndex, wallet);
} }
save(); save();