mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-20 10:22:18 +01:00
Use balance util to handle balance update
This commit is contained in:
parent
4c488fd574
commit
08df6be671
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* This file is part of Bitsquare.
|
||||
*
|
||||
* Bitsquare is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Bitsquare is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
|
||||
* License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Bitsquare. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.bitsquare.gui.main.dao.wallet;
|
||||
|
||||
import io.bitsquare.btc.listeners.BalanceListener;
|
||||
import io.bitsquare.btc.wallet.SquWalletService;
|
||||
import io.bitsquare.gui.main.overlays.popups.Popup;
|
||||
import io.bitsquare.gui.util.SQUFormatter;
|
||||
import javafx.scene.control.TextField;
|
||||
import org.bitcoinj.core.Coin;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class BalanceUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(BalanceUtil.class);
|
||||
|
||||
private final SquWalletService squWalletService;
|
||||
private final SQUFormatter formatter;
|
||||
private TextField balanceTextField;
|
||||
private BalanceListener balanceListener;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor, lifecycle
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Inject
|
||||
private BalanceUtil(SquWalletService squWalletService, SQUFormatter formatter) {
|
||||
this.squWalletService = squWalletService;
|
||||
this.formatter = formatter;
|
||||
}
|
||||
|
||||
public void setBalanceTextField(TextField balanceTextField) {
|
||||
this.balanceTextField = balanceTextField;
|
||||
}
|
||||
|
||||
public void initialize() {
|
||||
balanceListener = new BalanceListener() {
|
||||
@Override
|
||||
public void onBalanceChanged(Coin balance, Transaction tx) {
|
||||
updateBalance(balance);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void activate() {
|
||||
squWalletService.requestSquUtxo(() -> {
|
||||
updateBalance(squWalletService.getAvailableBalance());
|
||||
}, errorMessage -> {
|
||||
new Popup<>().warning(errorMessage);
|
||||
});
|
||||
squWalletService.addBalanceListener(balanceListener);
|
||||
|
||||
updateBalance(squWalletService.getAvailableBalance());
|
||||
}
|
||||
|
||||
public void deactivate() {
|
||||
squWalletService.removeBalanceListener(balanceListener);
|
||||
}
|
||||
|
||||
private void updateBalance(Coin balance) {
|
||||
balanceTextField.setText(formatter.formatCoinWithCode(balance));
|
||||
}
|
||||
|
||||
}
|
@ -17,17 +17,14 @@
|
||||
|
||||
package io.bitsquare.gui.main.dao.wallet.dashboard;
|
||||
|
||||
import io.bitsquare.btc.listeners.BalanceListener;
|
||||
import io.bitsquare.btc.wallet.SquWalletService;
|
||||
import io.bitsquare.gui.common.view.ActivatableView;
|
||||
import io.bitsquare.gui.common.view.FxmlView;
|
||||
import io.bitsquare.gui.main.overlays.popups.Popup;
|
||||
import io.bitsquare.gui.main.dao.wallet.BalanceUtil;
|
||||
import io.bitsquare.gui.util.Layout;
|
||||
import io.bitsquare.gui.util.SQUFormatter;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import org.bitcoinj.core.Coin;
|
||||
import org.bitcoinj.core.Transaction;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ -37,56 +34,42 @@ import static io.bitsquare.gui.util.FormBuilder.addTitledGroupBg;
|
||||
@FxmlView
|
||||
public class TokenDashboardView extends ActivatableView<GridPane, Void> {
|
||||
|
||||
private TextField confirmedBalance;
|
||||
private TextField balanceTextField;
|
||||
|
||||
private final SquWalletService squWalletService;
|
||||
private final SQUFormatter formatter;
|
||||
private BalanceUtil balanceUtil;
|
||||
|
||||
private final int gridRow = 0;
|
||||
private BalanceListener balanceListener;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Constructor, lifecycle
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Inject
|
||||
private TokenDashboardView(SquWalletService squWalletService, SQUFormatter formatter) {
|
||||
private TokenDashboardView(SquWalletService squWalletService, SQUFormatter formatter, BalanceUtil balanceUtil) {
|
||||
this.squWalletService = squWalletService;
|
||||
this.formatter = formatter;
|
||||
|
||||
this.balanceUtil = balanceUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
addTitledGroupBg(root, gridRow, 1, "Balance");
|
||||
confirmedBalance = addLabelTextField(root, gridRow, "Confirmed SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
|
||||
balanceListener = new BalanceListener() {
|
||||
@Override
|
||||
public void onBalanceChanged(Coin balance, Transaction tx) {
|
||||
updateBalance(balance);
|
||||
}
|
||||
};
|
||||
balanceTextField = addLabelTextField(root, gridRow, "SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
balanceUtil.setBalanceTextField(balanceTextField);
|
||||
balanceUtil.initialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void activate() {
|
||||
squWalletService.requestSquUtxo(() -> {
|
||||
updateBalance(squWalletService.getAvailableBalance());
|
||||
}, errorMessage -> {
|
||||
new Popup<>().warning(errorMessage);
|
||||
});
|
||||
squWalletService.addBalanceListener(balanceListener);
|
||||
|
||||
updateBalance(squWalletService.getAvailableBalance());
|
||||
balanceUtil.activate();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deactivate() {
|
||||
squWalletService.removeBalanceListener(balanceListener);
|
||||
}
|
||||
|
||||
private void updateBalance(Coin balance) {
|
||||
confirmedBalance.setText(formatter.formatCoinWithCode(balance));
|
||||
balanceUtil.deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ import io.bitsquare.gui.common.view.ActivatableView;
|
||||
import io.bitsquare.gui.common.view.FxmlView;
|
||||
import io.bitsquare.gui.components.AddressTextField;
|
||||
import io.bitsquare.gui.components.InputTextField;
|
||||
import io.bitsquare.gui.main.dao.wallet.BalanceUtil;
|
||||
import io.bitsquare.gui.main.overlays.windows.QRCodeWindow;
|
||||
import io.bitsquare.gui.util.GUIUtil;
|
||||
import io.bitsquare.gui.util.Layout;
|
||||
@ -36,8 +37,8 @@ import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import net.glxn.qrgen.QRCode;
|
||||
import net.glxn.qrgen.image.ImageType;
|
||||
import org.bitcoinj.core.*;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.core.Coin;
|
||||
import org.bitcoinj.core.Wallet;
|
||||
import org.bitcoinj.uri.BitcoinURI;
|
||||
import org.fxmisc.easybind.EasyBind;
|
||||
import org.fxmisc.easybind.Subscription;
|
||||
@ -45,7 +46,6 @@ import org.fxmisc.easybind.Subscription;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.bitsquare.gui.util.FormBuilder.*;
|
||||
@ -56,15 +56,15 @@ public class TokenReceiveView extends ActivatableView<GridPane, Void> {
|
||||
private ImageView qrCodeImageView;
|
||||
private AddressTextField addressTextField;
|
||||
private InputTextField amountTextField;
|
||||
private TextField confirmedBalance;
|
||||
private TextField balanceTextField;
|
||||
|
||||
private final SquWalletService squWalletService;
|
||||
private final SQUFormatter formatter;
|
||||
private BalanceUtil balanceUtil;
|
||||
|
||||
@Nullable
|
||||
private Wallet squWallet;
|
||||
private int gridRow = 0;
|
||||
private WalletEventListener walletEventListener;
|
||||
private final String paymentLabelString;
|
||||
private Subscription amountTextFieldSubscription;
|
||||
|
||||
@ -74,16 +74,19 @@ public class TokenReceiveView extends ActivatableView<GridPane, Void> {
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Inject
|
||||
private TokenReceiveView(SquWalletService squWalletService, SQUFormatter formatter) {
|
||||
private TokenReceiveView(SquWalletService squWalletService, SQUFormatter formatter, BalanceUtil balanceUtil) {
|
||||
this.squWalletService = squWalletService;
|
||||
this.formatter = formatter;
|
||||
this.balanceUtil = balanceUtil;
|
||||
paymentLabelString = "Fund Bitsquare token wallet";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
addTitledGroupBg(root, gridRow, 1, "Balance");
|
||||
confirmedBalance = addLabelTextField(root, gridRow, "Confirmed SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
balanceTextField = addLabelTextField(root, gridRow, "SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
balanceUtil.setBalanceTextField(balanceTextField);
|
||||
balanceUtil.initialize();
|
||||
|
||||
addTitledGroupBg(root, ++gridRow, 3, "Fund your token wallet", Layout.GROUP_DISTANCE);
|
||||
|
||||
@ -101,51 +104,12 @@ public class TokenReceiveView extends ActivatableView<GridPane, Void> {
|
||||
amountTextField = addLabelInputTextField(root, ++gridRow, "Amount (optional):").second;
|
||||
if (DevFlags.DEV_MODE)
|
||||
amountTextField.setText("10");
|
||||
|
||||
|
||||
walletEventListener = new WalletEventListener() {
|
||||
@Override
|
||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReorganize(Wallet wallet) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWalletChanged(Wallet wallet) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKeysAdded(List<ECKey> keys) {
|
||||
updateBalance();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void activate() {
|
||||
balanceUtil.activate();
|
||||
squWallet = squWalletService.getWallet();
|
||||
squWallet.addEventListener(walletEventListener);
|
||||
updateBalance();
|
||||
|
||||
amountTextFieldSubscription = EasyBind.subscribe(amountTextField.textProperty(), t -> {
|
||||
addressTextField.setAmountAsCoin(formatter.parseToCoin(t));
|
||||
@ -162,8 +126,7 @@ public class TokenReceiveView extends ActivatableView<GridPane, Void> {
|
||||
|
||||
@Override
|
||||
protected void deactivate() {
|
||||
if (squWallet != null)
|
||||
squWallet.removeEventListener(walletEventListener);
|
||||
balanceUtil.deactivate();
|
||||
|
||||
qrCodeImageView.setOnMouseClicked(null);
|
||||
amountTextFieldSubscription.unsubscribe();
|
||||
@ -192,10 +155,5 @@ public class TokenReceiveView extends ActivatableView<GridPane, Void> {
|
||||
paymentLabelString,
|
||||
null);
|
||||
}
|
||||
|
||||
private void updateBalance() {
|
||||
if (squWallet != null)
|
||||
confirmedBalance.setText(formatter.formatCoinWithCode(squWallet.getBalance()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ import io.bitsquare.btc.wallet.SquWalletService;
|
||||
import io.bitsquare.gui.common.view.ActivatableView;
|
||||
import io.bitsquare.gui.common.view.FxmlView;
|
||||
import io.bitsquare.gui.components.InputTextField;
|
||||
import io.bitsquare.gui.main.dao.wallet.BalanceUtil;
|
||||
import io.bitsquare.gui.main.overlays.popups.Popup;
|
||||
import io.bitsquare.gui.util.BSFormatter;
|
||||
import io.bitsquare.gui.util.Layout;
|
||||
@ -36,12 +37,10 @@ import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import org.bitcoinj.core.*;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static io.bitsquare.gui.util.FormBuilder.*;
|
||||
@ -50,17 +49,17 @@ import static io.bitsquare.gui.util.FormBuilder.*;
|
||||
public class TokenSendView extends ActivatableView<GridPane, Void> {
|
||||
|
||||
|
||||
private TextField confirmedBalance;
|
||||
private TextField balanceTextField;
|
||||
|
||||
private final SquWalletService squWalletService;
|
||||
private final BtcWalletService btcWalletService;
|
||||
private final FeeService feeService;
|
||||
private final BSFormatter formatter;
|
||||
private BalanceUtil balanceUtil;
|
||||
|
||||
@Nullable
|
||||
private Wallet squWallet;
|
||||
private int gridRow = 0;
|
||||
private WalletEventListener walletEventListener;
|
||||
private InputTextField amountInputTextField;
|
||||
private Button sendButton;
|
||||
private InputTextField receiversAddressInputTextField;
|
||||
@ -70,18 +69,21 @@ public class TokenSendView extends ActivatableView<GridPane, Void> {
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@Inject
|
||||
private TokenSendView(SquWalletService squWalletService, BtcWalletService btcWalletService, FeeService feeService, SQUFormatter formatter) {
|
||||
private TokenSendView(SquWalletService squWalletService, BtcWalletService btcWalletService, FeeService feeService, SQUFormatter formatter, BalanceUtil balanceUtil) {
|
||||
this.squWalletService = squWalletService;
|
||||
this.btcWalletService = btcWalletService;
|
||||
this.feeService = feeService;
|
||||
|
||||
this.formatter = formatter;
|
||||
this.balanceUtil = balanceUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
addTitledGroupBg(root, gridRow, 1, "Balance");
|
||||
confirmedBalance = addLabelTextField(root, gridRow, "Confirmed SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
balanceTextField = addLabelTextField(root, gridRow, "SQU balance:", Layout.FIRST_ROW_DISTANCE).second;
|
||||
balanceUtil.setBalanceTextField(balanceTextField);
|
||||
balanceUtil.initialize();
|
||||
|
||||
addTitledGroupBg(root, ++gridRow, 3, "Send funds", Layout.GROUP_DISTANCE);
|
||||
amountInputTextField = addLabelInputTextField(root, gridRow, "Amount in SQU:", Layout.FIRST_ROW_AND_GROUP_DISTANCE).second;
|
||||
@ -127,62 +129,17 @@ public class TokenSendView extends ActivatableView<GridPane, Void> {
|
||||
new Popup<>().warning(e.toString());
|
||||
}
|
||||
});
|
||||
|
||||
walletEventListener = new WalletEventListener() {
|
||||
@Override
|
||||
public void onCoinsReceived(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCoinsSent(Wallet wallet, Transaction tx, Coin prevBalance, Coin newBalance) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReorganize(Wallet wallet) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransactionConfidenceChanged(Wallet wallet, Transaction tx) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWalletChanged(Wallet wallet) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScriptsChanged(Wallet wallet, List<Script> scripts, boolean isAddingScripts) {
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onKeysAdded(List<ECKey> keys) {
|
||||
updateBalance();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void activate() {
|
||||
balanceUtil.activate();
|
||||
this.squWallet = squWalletService.getWallet();
|
||||
squWallet.addEventListener(walletEventListener);
|
||||
|
||||
updateBalance();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deactivate() {
|
||||
if (squWallet != null)
|
||||
squWallet.removeEventListener(walletEventListener);
|
||||
}
|
||||
|
||||
private void updateBalance() {
|
||||
if (squWallet != null)
|
||||
confirmedBalance.setText(formatter.formatCoinWithCode(squWallet.getBalance()));
|
||||
balanceUtil.deactivate();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user