Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split EmptyWalletWindow into BsqEmptyWalletWindow and BtcEmptyWalletWindow #3660

Merged
merged 1 commit into from
Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions desktop/src/main/java/bisq/desktop/app/BisqApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@
import bisq.desktop.main.MainView;
import bisq.desktop.main.debug.DebugView;
import bisq.desktop.main.overlays.popups.Popup;
import bisq.desktop.main.overlays.windows.EmptyWalletWindow;
import bisq.desktop.main.overlays.windows.BsqEmptyWalletWindow;
import bisq.desktop.main.overlays.windows.BtcEmptyWalletWindow;
import bisq.desktop.main.overlays.windows.FilterWindow;
import bisq.desktop.main.overlays.windows.ManualPayoutTxWindow;
import bisq.desktop.main.overlays.windows.SendAlertMessageWindow;
import bisq.desktop.main.overlays.windows.ShowWalletDataWindow;
import bisq.desktop.util.CssTheme;
import bisq.desktop.util.ImageUtil;

import bisq.core.alert.AlertManager;
import bisq.core.app.AppOptionKeys;
import bisq.core.app.AvoidStandbyModeService;
import bisq.core.app.BisqEnvironment;
import bisq.core.app.OSXStandbyModeDisabler;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.WalletsManager;
import bisq.core.dao.governance.voteresult.MissingDataRequestService;
import bisq.core.filter.FilterManager;
import bisq.core.locale.Res;
import bisq.core.offer.OpenOffer;
import bisq.core.offer.OpenOfferManager;
Expand Down Expand Up @@ -269,9 +268,9 @@ private void addSceneKeyEventHandler(Scene scene, Injector injector) {
shutDownByUser();
} else {
if (Utilities.isAltOrCtrlPressed(KeyCode.E, keyEvent)) {
showBtcEmergencyWalletPopup(injector);
injector.getInstance(BtcEmptyWalletWindow.class).show();
} else if (Utilities.isAltOrCtrlPressed(KeyCode.B, keyEvent)) {
showBsqEmergencyWalletPopup(injector);
injector.getInstance(BsqEmptyWalletWindow.class).show();
} else if (Utilities.isAltOrCtrlPressed(KeyCode.M, keyEvent)) {
injector.getInstance(SendAlertMessageWindow.class).show();
} else if (Utilities.isAltOrCtrlPressed(KeyCode.F, keyEvent)) {
Expand Down Expand Up @@ -336,18 +335,6 @@ private void shutDownByUser() {
}
}

private void showBtcEmergencyWalletPopup(Injector injector) {
EmptyWalletWindow emptyWalletWindow = injector.getInstance(EmptyWalletWindow.class);
emptyWalletWindow.setIsBtc(true);
emptyWalletWindow.show();
}

private void showBsqEmergencyWalletPopup(Injector injector) {
EmptyWalletWindow emptyWalletWindow = injector.getInstance(EmptyWalletWindow.class);
emptyWalletWindow.setIsBtc(false);
emptyWalletWindow.show();
}

// Used for debugging trade process
private void showDebugWindow(Scene scene, Injector injector) {
ViewLoader viewLoader = injector.getInstance(ViewLoader.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package bisq.desktop.main.overlays.windows;

import bisq.desktop.components.AutoTooltipButton;
import bisq.desktop.main.overlays.Overlay;

import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.locale.Res;
import bisq.core.util.coin.BsqFormatter;

import com.google.inject.Inject;

import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;

import javafx.geometry.Insets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static bisq.desktop.util.FormBuilder.addTopLabelTextField;

public final class BsqEmptyWalletWindow extends Overlay<BsqEmptyWalletWindow> {
protected static final Logger log = LoggerFactory.getLogger(BtcEmptyWalletWindow.class);

private final BsqWalletService bsqWalletService;
private final BsqFormatter bsqFormatter;

@Inject
public BsqEmptyWalletWindow(BsqWalletService bsqWalletService, BsqFormatter bsqFormatter) {
headLine(Res.get("emptyWalletWindow.headline", "BSQ"));
width = 768;
type = Type.Instruction;
this.bsqWalletService = bsqWalletService;
this.bsqFormatter = bsqFormatter;
}

public void show() {
createGridPane();
addHeadLine();
addContent();
applyStyles();
display();
}

@Override
protected void setupKeyHandler(Scene scene) {
if (!hideCloseButton) {
scene.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.ESCAPE) {
e.consume();
doClose();
}
});
}
}

private void addContent() {
gridPane.getColumnConstraints().remove(1);

addTopLabelTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.balance"),
bsqFormatter.formatCoinWithCode(bsqWalletService.getAvailableConfirmedBalance()), 10);

addTopLabelTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.bsq.btcBalance"),
bsqFormatter.formatBTCWithCode(bsqWalletService.getAvailableNonBsqBalance().value), 10);

closeButton = new AutoTooltipButton(Res.get("shared.cancel"));
closeButton.setOnAction(e -> {
hide();
closeHandlerOptional.ifPresent(Runnable::run);
});


closeButton.setDefaultButton(true);
closeButton.updateText(Res.get("shared.close"));

HBox hBox = new HBox();
hBox.setSpacing(10);
GridPane.setRowIndex(hBox, ++rowIndex);

hBox.getChildren().addAll(closeButton);

gridPane.getChildren().add(hBox);
GridPane.setMargin(hBox, new Insets(10, 0, 0, 0));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
/*
* This file is part of Bisq.
*
* Bisq 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.
*
* Bisq 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 Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.desktop.main.overlays.windows;

import bisq.desktop.components.AutoTooltipButton;
Expand All @@ -25,14 +8,11 @@
import bisq.desktop.util.Transitions;

import bisq.core.btc.setup.WalletsSetup;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.btc.wallet.Restrictions;
import bisq.core.btc.wallet.WalletService;
import bisq.core.locale.Res;
import bisq.core.offer.OpenOfferManager;
import bisq.core.util.FormattingUtils;
import bisq.core.util.coin.BsqFormatter;
import bisq.core.util.coin.CoinFormatter;

import bisq.network.p2p.P2PService;
Expand All @@ -43,7 +23,8 @@
import org.bitcoinj.core.Coin;
import org.bitcoinj.core.InsufficientMoneyException;

import javax.inject.Inject;
import com.google.inject.Inject;

import javax.inject.Named;

import javafx.scene.Scene;
Expand All @@ -66,69 +47,47 @@
import static bisq.desktop.util.FormBuilder.addMultilineLabel;
import static bisq.desktop.util.FormBuilder.addTopLabelTextField;

public class EmptyWalletWindow extends Overlay<EmptyWalletWindow> {
private static final Logger log = LoggerFactory.getLogger(EmptyWalletWindow.class);
public final class BtcEmptyWalletWindow extends Overlay<BtcEmptyWalletWindow> {
protected static final Logger log = LoggerFactory.getLogger(BtcEmptyWalletWindow.class);

private final WalletPasswordWindow walletPasswordWindow;
private final OpenOfferManager openOfferManager;
private final P2PService p2PService;
private final WalletsSetup walletsSetup;
private final BtcWalletService btcWalletService;
private final BsqWalletService bsqWalletService;
private final CoinFormatter btcFormatter;
private final BsqFormatter bsqFormatter;
private final OpenOfferManager openOfferManager;

private Button emptyWalletButton;
private InputTextField addressInputTextField;
private TextField balanceTextField;
private boolean isBtc;


///////////////////////////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////////////////////////

@Inject
public EmptyWalletWindow(WalletPasswordWindow walletPasswordWindow,
OpenOfferManager openOfferManager,
P2PService p2PService,
WalletsSetup walletsSetup,
BtcWalletService btcWalletService,
BsqWalletService bsqWalletService,
@Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter,
BsqFormatter bsqFormatter) {
this.walletPasswordWindow = walletPasswordWindow;
this.openOfferManager = openOfferManager;
public BtcEmptyWalletWindow(WalletPasswordWindow walletPasswordWindow,
OpenOfferManager openOfferManager,
P2PService p2PService,
WalletsSetup walletsSetup,
BtcWalletService btcWalletService,
@Named(FormattingUtils.BTC_FORMATTER_KEY) CoinFormatter btcFormatter) {
headLine(Res.get("emptyWalletWindow.headline", "BTC"));
width = 768;
type = Type.Instruction;

this.p2PService = p2PService;
this.walletsSetup = walletsSetup;
this.btcWalletService = btcWalletService;
this.bsqWalletService = bsqWalletService;
this.btcFormatter = btcFormatter;
this.bsqFormatter = bsqFormatter;

type = Type.Instruction;
this.walletPasswordWindow = walletPasswordWindow;
this.openOfferManager = openOfferManager;
}

public void show() {
if (headLine == null)
headLine = Res.get("emptyWalletWindow.headline", getCurrency());

width = 768;
createGridPane();
addHeadLine();
addContent();
applyStyles();
display();
}

private String getCurrency() {
return isBtc ? "BTC" : "BSQ";
}


///////////////////////////////////////////////////////////////////////////////////////////
// Protected
///////////////////////////////////////////////////////////////////////////////////////////

@Override
protected void setupKeyHandler(Scene scene) {
if (!hideCloseButton) {
Expand All @@ -141,67 +100,44 @@ protected void setupKeyHandler(Scene scene) {
}
}

public void setIsBtc(boolean isBtc) {
this.isBtc = isBtc;
}

private void addContent() {
addMultilineLabel(gridPane, ++rowIndex, Res.get("emptyWalletWindow.info"), 0);

if (!isBtc)
gridPane.getColumnConstraints().remove(1);

if (isBtc)
addMultilineLabel(gridPane, ++rowIndex, Res.get("emptyWalletWindow.info"), 0);

Coin totalBalance = getWalletService().getAvailableConfirmedBalance();
Coin totalBalance = btcWalletService.getAvailableConfirmedBalance();
balanceTextField = addTopLabelTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.balance"),
getFormatter().formatCoinWithCode(totalBalance), 10).second;
btcFormatter.formatCoinWithCode(totalBalance), 10).second;

addressInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.address"));

if (isBtc) {
addressInputTextField = addInputTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.address"));
} else {
addTopLabelTextField(gridPane, ++rowIndex, Res.get("emptyWalletWindow.bsq.btcBalance"),
bsqFormatter.formatBTCWithCode(bsqWalletService.getAvailableNonBsqBalance().value), 10);
}
closeButton = new AutoTooltipButton(Res.get("shared.cancel"));
closeButton.setOnAction(e -> {
hide();
closeHandlerOptional.ifPresent(Runnable::run);
});

if (isBtc) {
emptyWalletButton = new AutoTooltipButton(Res.get("emptyWalletWindow.button"));
boolean isBalanceSufficient = Restrictions.isAboveDust(totalBalance);
emptyWalletButton.setDefaultButton(isBalanceSufficient);
emptyWalletButton.setDisable(!isBalanceSufficient && addressInputTextField.getText().length() > 0);
emptyWalletButton.setOnAction(e -> {
if (addressInputTextField.getText().length() > 0 && isBalanceSufficient) {
if (getWalletService().isEncrypted()) {
walletPasswordWindow
.onAesKey(this::doEmptyWallet)
.onClose(this::blurAgain)
.show();
} else {
doEmptyWallet(null);
}
emptyWalletButton = new AutoTooltipButton(Res.get("emptyWalletWindow.button"));
boolean isBalanceSufficient = Restrictions.isAboveDust(totalBalance);
emptyWalletButton.setDefaultButton(isBalanceSufficient);
emptyWalletButton.setDisable(!isBalanceSufficient && addressInputTextField.getText().length() > 0);
emptyWalletButton.setOnAction(e -> {
if (addressInputTextField.getText().length() > 0 && isBalanceSufficient) {
if (btcWalletService.isEncrypted()) {
walletPasswordWindow
.onAesKey(this::doEmptyWallet)
.onClose(this::blurAgain)
.show();
} else {
doEmptyWallet(null);
}
});
}
});

closeButton.setDefaultButton(!isBalanceSufficient);
} else {
closeButton.setDefaultButton(true);
closeButton.updateText(Res.get("shared.close"));
}
closeButton.setDefaultButton(!isBalanceSufficient);

HBox hBox = new HBox();
hBox.setSpacing(10);
GridPane.setRowIndex(hBox, ++rowIndex);

if (isBtc)
hBox.getChildren().addAll(emptyWalletButton, closeButton);
else
hBox.getChildren().addAll(closeButton);

hBox.getChildren().addAll(emptyWalletButton, closeButton);
gridPane.getChildren().add(hBox);
GridPane.setMargin(hBox, new Insets(10, 0, 0, 0));
}
Expand All @@ -224,11 +160,11 @@ private void doEmptyWallet2(KeyParameter aesKey) {
emptyWalletButton.setDisable(true);
openOfferManager.removeAllOpenOffers(() -> {
try {
getWalletService().emptyWallet(addressInputTextField.getText(),
btcWalletService.emptyWallet(addressInputTextField.getText(),
aesKey,
() -> {
closeButton.updateText(Res.get("shared.close"));
balanceTextField.setText(getFormatter().formatCoinWithCode(getWalletService().getAvailableConfirmedBalance()));
balanceTextField.setText(btcFormatter.formatCoinWithCode(btcWalletService.getAvailableConfirmedBalance()));
emptyWalletButton.setDisable(true);
log.debug("wallet empty successful");
onClose(() -> UserThread.runAfter(() -> new Popup()
Expand All @@ -247,12 +183,4 @@ private void doEmptyWallet2(KeyParameter aesKey) {
}
});
}

private WalletService getWalletService() {
return isBtc ? btcWalletService : bsqWalletService;
}

private CoinFormatter getFormatter() {
return isBtc ? btcFormatter : bsqFormatter;
}
}