make controller and tab data share data sources

This commit is contained in:
Craig Raw 2020-08-19 15:44:40 +02:00
parent 30e2ab5e2a
commit d5aba35184
6 changed files with 45 additions and 74 deletions

View file

@ -21,6 +21,7 @@ import com.sparrowwallet.sparrow.io.*;
import com.sparrowwallet.sparrow.net.ElectrumServer;
import com.sparrowwallet.sparrow.preferences.PreferencesDialog;
import com.sparrowwallet.sparrow.transaction.TransactionController;
import com.sparrowwallet.sparrow.transaction.TransactionData;
import com.sparrowwallet.sparrow.transaction.TransactionView;
import com.sparrowwallet.sparrow.wallet.WalletController;
import com.sparrowwallet.sparrow.wallet.WalletForm;
@ -468,13 +469,9 @@ public class AppController implements Initializable {
TransactionTabData transactionTabData = (TransactionTabData)tabData;
Transaction transaction = transactionTabData.getTransaction();
//Note the transactionTabData's transaction does not change even once the final tx is extracted, so extract it here if possible
if(transactionTabData.getPsbt() != null && transactionTabData.getPsbt().isFinalized()) {
transaction = transactionTabData.getPsbt().extractTransaction();
}
//Save a transaction if the PSBT is null or finalized (a finalized PSBT is less useful than a broadcastable tx)
boolean saveTx = (transactionTabData.getPsbt() == null || transactionTabData.getPsbt().isFinalized());
//Save a transaction if the PSBT is null or transaction has already been extracted, otherwise save PSBT
//The PSBT's transaction is not altered with transaction extraction, but the extracted transaction is stored in TransactionData
boolean saveTx = (transactionTabData.getPsbt() == null || transactionTabData.getPsbt().getTransaction() != transaction);
Stage window = new Stage();
FileChooser fileChooser = new FileChooser();
@ -754,8 +751,6 @@ public class AppController implements Initializable {
name = name.substring(0, name.lastIndexOf('.'));
}
Tab tab = new Tab(name);
TabData tabData = new WalletTabData(TabData.TabType.WALLET, wallet, storage);
tab.setUserData(tabData);
tab.setContextMenu(getTabContextMenu(tab));
tab.setClosable(true);
FXMLLoader walletLoader = new FXMLLoader(getClass().getResource("wallet/wallet.fxml"));
@ -767,6 +762,9 @@ public class AppController implements Initializable {
EventManager.get().register(walletForm);
controller.setWalletForm(walletForm);
TabData tabData = new WalletTabData(TabData.TabType.WALLET, walletForm);
tab.setUserData(tabData);
tabs.getTabs().add(tab);
return tab;
} catch(IOException e) {
@ -863,29 +861,30 @@ public class AppController implements Initializable {
}
Tab tab = new Tab(tabName);
TabData tabData = new TransactionTabData(TabData.TabType.TRANSACTION, transaction, psbt);
tab.setUserData(tabData);
tab.setContextMenu(getTabContextMenu(tab));
tab.setClosable(true);
FXMLLoader transactionLoader = new FXMLLoader(getClass().getResource("transaction/transaction.fxml"));
tab.setContent(transactionLoader.load());
TransactionController controller = transactionLoader.getController();
TransactionData transactionData;
if(psbt != null) {
controller.setPSBT(psbt);
transactionData = new TransactionData(name, psbt);
} else if(blockTransaction != null) {
controller.setBlockTransaction(blockTransaction);
transactionData = new TransactionData(name, blockTransaction);
} else {
controller.setTransaction(transaction);
transactionData = new TransactionData(name, transaction);
}
controller.setName(name);
controller.setTransactionData(transactionData);
if(initialView != null) {
controller.setInitialView(initialView, initialIndex);
}
controller.initializeView();
TabData tabData = new TransactionTabData(TabData.TabType.TRANSACTION, transactionData);
tab.setUserData(tabData);
tabs.getTabs().add(tab);
return tab;
@ -930,7 +929,7 @@ public class AppController implements Initializable {
TransactionTabSelectedEvent txTabEvent = (TransactionTabSelectedEvent)event;
TransactionTabData transactionTabData = txTabEvent.getTransactionTabData();
saveTransaction.setDisable(false);
saveTransaction.setText("Save " + (transactionTabData.getPsbt() == null || transactionTabData.getPsbt().isFinalized() ? "Transaction..." : "PSBT..."));
saveTransaction.setText("Save " + (transactionTabData.getPsbt() == null || transactionTabData.getPsbt().getTransaction() != transactionTabData.getTransaction() ? "Transaction..." : "PSBT..."));
exportWallet.setDisable(true);
showTxHex.setDisable(false);
} else if(event instanceof WalletTabSelectedEvent) {
@ -944,12 +943,12 @@ public class AppController implements Initializable {
}
@Subscribe
public void psbtFinalizedEvent(PSBTFinalizedEvent event) {
public void transactionExtractedEvent(TransactionExtractedEvent event) {
for(Tab tab : tabs.getTabs()) {
TabData tabData = (TabData) tab.getUserData();
if(tabData instanceof TransactionTabData) {
TransactionTabData transactionTabData = (TransactionTabData)tabData;
if(Arrays.equals(transactionTabData.getTransaction().bitcoinSerialize(), event.getPsbt().getTransaction().bitcoinSerialize())) {
if(transactionTabData.getTransaction() == event.getFinalTransaction()) {
saveTransaction.setText("Save Transaction...");
}
}

View file

@ -2,26 +2,25 @@ package com.sparrowwallet.sparrow;
import com.sparrowwallet.drongo.protocol.Transaction;
import com.sparrowwallet.drongo.psbt.PSBT;
import com.sparrowwallet.sparrow.transaction.TransactionData;
public class TransactionTabData extends TabData {
private final Transaction transaction;
private final PSBT psbt;
private final TransactionData transactionData;
public TransactionTabData(TabType type, Transaction transaction) {
this(type, transaction, null);
public TransactionTabData(TabType type, TransactionData transactionData) {
super(type);
this.transactionData = transactionData;
}
public TransactionTabData(TabType type, Transaction transaction, PSBT psbt) {
super(type);
this.transaction = transaction;
this.psbt = psbt;
public TransactionData getTransactionData() {
return transactionData;
}
public Transaction getTransaction() {
return transaction;
return transactionData.getTransaction();
}
public PSBT getPsbt() {
return psbt;
return transactionData.getPsbt();
}
}

View file

@ -1,34 +1,22 @@
package com.sparrowwallet.sparrow;
import com.google.common.eventbus.Subscribe;
import com.sparrowwallet.drongo.wallet.Wallet;
import com.sparrowwallet.sparrow.event.WalletSettingsChangedEvent;
import com.sparrowwallet.sparrow.io.Storage;
import com.sparrowwallet.sparrow.wallet.WalletForm;
public class WalletTabData extends TabData {
private Wallet wallet;
private final Storage storage;
private final WalletForm walletForm;
public WalletTabData(TabType type, Wallet wallet, Storage storage) {
public WalletTabData(TabType type, WalletForm walletForm) {
super(type);
this.wallet = wallet;
this.storage = storage;
EventManager.get().register(this);
this.walletForm = walletForm;
}
public Wallet getWallet() {
return wallet;
return walletForm.getWallet();
}
public Storage getStorage() {
return storage;
}
@Subscribe
public void walletSettingsChanged(WalletSettingsChangedEvent event) {
if(event.getWalletFile().equals(storage.getWalletFile())) {
wallet = event.getWallet();
}
return walletForm.getStorage();
}
}

View file

@ -13,7 +13,7 @@ public class PageForm extends IndexedTransactionForm {
private final int pageEnd;
public PageForm(TransactionView view, int pageStart, int pageEnd) {
super(new TransactionData(ElectrumServer.UNFETCHABLE_BLOCK_TRANSACTION), pageStart);
super(new TransactionData(pageStart + "-" + pageEnd, ElectrumServer.UNFETCHABLE_BLOCK_TRANSACTION), pageStart);
this.view = view;
this.pageStart = pageStart;
this.pageEnd = pageEnd;

View file

@ -363,34 +363,22 @@ public class TransactionController implements Initializable {
}
}
public void setTransactionData(TransactionData transactionData) {
this.txdata = transactionData;
}
public Transaction getTransaction() {
return txdata.getTransaction();
}
public void setTransaction(Transaction transaction) {
this.txdata = new TransactionData(transaction);
}
public void setName(String name) {
this.txdata.setName(name);
}
public PSBT getPSBT() {
return txdata.getPsbt();
}
public void setPSBT(PSBT psbt) {
this.txdata = new TransactionData(psbt);
}
public BlockTransaction getBlockTransaction() {
return txdata.getBlockTransaction();
}
public void setBlockTransaction(BlockTransaction blockTransaction) {
this.txdata = new TransactionData(blockTransaction);
}
public void setInitialView(TransactionView initialView, Integer initialIndex) {
this.initialView = initialView;
this.initialIndex = initialIndex;

View file

@ -32,17 +32,18 @@ public class TransactionData {
private final SimpleObjectProperty<Wallet> signingWallet = new SimpleObjectProperty<>(this, "signingWallet", null);
private final ObservableList<Keystore> signedKeystores = FXCollections.observableArrayList();
public TransactionData(PSBT psbt) {
this.transaction = psbt.getTransaction();
public TransactionData(String name, PSBT psbt) {
this(name, psbt.getTransaction());
this.psbt = psbt;
}
public TransactionData(BlockTransaction blockTransaction) {
this.transaction = blockTransaction.getTransaction();
public TransactionData(String name, BlockTransaction blockTransaction) {
this(name, blockTransaction.getTransaction());
this.blockTransaction = blockTransaction;
}
public TransactionData(Transaction transaction) {
public TransactionData(String name, Transaction transaction) {
this.name = name;
this.transaction = transaction;
}
@ -58,10 +59,6 @@ public class TransactionData {
return name;
}
public void setName(String name) {
this.name = name;
}
public PSBT getPsbt() {
return psbt;
}