Move Notification out of DataModel to View

This commit is contained in:
Manfred Karrer 2018-03-01 19:06:19 -05:00
parent a1befee0bd
commit a30f9f0a52
No known key found for this signature in database
GPG Key ID: 401250966A6B2C46
5 changed files with 79 additions and 55 deletions

View File

@ -17,14 +17,9 @@
package io.bisq.gui.main.offer;
import io.bisq.common.app.DevEnv;
import io.bisq.common.locale.Res;
import io.bisq.core.btc.AddressEntry;
import io.bisq.core.btc.wallet.BtcWalletService;
import io.bisq.core.user.Preferences;
import io.bisq.gui.common.model.ActivatableDataModel;
import io.bisq.gui.main.overlays.notifications.Notification;
import io.bisq.gui.util.BSFormatter;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
@ -39,8 +34,6 @@ import org.bitcoinj.core.Coin;
*/
public abstract class OfferDataModel extends ActivatableDataModel {
protected final BtcWalletService btcWalletService;
protected final Preferences preferences;
protected final BSFormatter formatter;
@Getter
protected final BooleanProperty isBtcWalletFunded = new SimpleBooleanProperty();
@ -51,16 +44,14 @@ public abstract class OfferDataModel extends ActivatableDataModel {
@Getter
protected final ObjectProperty<Coin> missingCoin = new SimpleObjectProperty<>(Coin.ZERO);
@Getter
protected final BooleanProperty showWalletFundedNotification = new SimpleBooleanProperty();
@Getter
protected Coin totalAvailableBalance;
protected Notification walletFundedNotification;
protected AddressEntry addressEntry;
protected boolean useSavingsWallet;
public OfferDataModel(BtcWalletService btcWalletService, Preferences preferences, BSFormatter formatter) {
public OfferDataModel(BtcWalletService btcWalletService) {
this.btcWalletService = btcWalletService;
this.preferences = preferences;
this.formatter = formatter;
}
protected void updateBalance() {
@ -82,19 +73,10 @@ public abstract class OfferDataModel extends ActivatableDataModel {
if (missingCoin.get().isNegative())
missingCoin.set(Coin.ZERO);
}
log.debug("missingCoin " + missingCoin.get().toFriendlyString());
final boolean balanceSufficient = isBalanceSufficient(balance.get());
log.error("balanceSufficient " + balanceSufficient);
isBtcWalletFunded.set(balanceSufficient);
//noinspection ConstantConditions,ConstantConditions
if (totalToPayAsCoin.get() != null && isBtcWalletFunded.get() && walletFundedNotification == null && !DevEnv.DEV_MODE) {
walletFundedNotification = new Notification()
.headLine(Res.get("notification.walletUpdate.headline"))
.notification(Res.get("notification.walletUpdate.msg", formatter.formatCoinWithCode(totalToPayAsCoin.get())))
.autoClose();
walletFundedNotification.show();
isBtcWalletFunded.set(isBalanceSufficient(balance.get()));
if (totalToPayAsCoin.get() != null && isBtcWalletFunded.get() && !showWalletFundedNotification.get()) {
showWalletFundedNotification.set(true);
}
}

View File

@ -72,6 +72,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
class CreateOfferDataModel extends OfferDataModel {
private final OpenOfferManager openOfferManager;
private final BsqWalletService bsqWalletService;
private final Preferences preferences;
private final User user;
private final KeyRing keyRing;
private final P2PService p2PService;
@ -81,6 +82,7 @@ class CreateOfferDataModel extends OfferDataModel {
private final AccountAgeWitnessService accountAgeWitnessService;
private final TradeWalletService tradeWalletService;
private final FeeService feeService;
private final BSFormatter formatter;
private final String offerId;
private final BalanceListener btcBalanceListener;
private final BsqBalanceListener bsqBalanceListener;
@ -122,10 +124,11 @@ class CreateOfferDataModel extends OfferDataModel {
PriceFeedService priceFeedService, FilterManager filterManager,
AccountAgeWitnessService accountAgeWitnessService, TradeWalletService tradeWalletService,
FeeService feeService, BSFormatter formatter) {
super(btcWalletService, preferences, formatter);
super(btcWalletService);
this.openOfferManager = openOfferManager;
this.bsqWalletService = bsqWalletService;
this.preferences = preferences;
this.user = user;
this.keyRing = keyRing;
this.p2PService = p2PService;
@ -134,6 +137,7 @@ class CreateOfferDataModel extends OfferDataModel {
this.accountAgeWitnessService = accountAgeWitnessService;
this.tradeWalletService = tradeWalletService;
this.feeService = feeService;
this.formatter = formatter;
offerId = Utilities.getRandomPrefix(5, 8) + "-" +
UUID.randomUUID().toString() + "-" +

View File

@ -45,6 +45,7 @@ import io.bisq.gui.main.dao.wallet.receive.BsqReceiveView;
import io.bisq.gui.main.funds.FundsView;
import io.bisq.gui.main.funds.withdrawal.WithdrawalView;
import io.bisq.gui.main.offer.OfferView;
import io.bisq.gui.main.overlays.notifications.Notification;
import io.bisq.gui.main.overlays.popups.Popup;
import io.bisq.gui.main.overlays.windows.FeeOptionWindow;
import io.bisq.gui.main.overlays.windows.OfferDetailsWindow;
@ -126,6 +127,7 @@ public class CreateOfferView extends ActivatableViewAndModel<AnchorPane, CreateO
private int gridRow = 0;
private final List<Node> editOfferElements = new ArrayList<>();
private boolean clearXchangeWarningDisplayed, isActivated;
private ChangeListener<Boolean> getShowWalletFundedNotificationListener;
///////////////////////////////////////////////////////////////////////////////////////////
@ -658,8 +660,18 @@ public class CreateOfferView extends ActivatableViewAndModel<AnchorPane, CreateO
}
};
marketPriceAvailableListener = (observable, oldValue, newValue) -> updateMarketPriceAvailable();
getShowWalletFundedNotificationListener = (observable, oldValue, newValue) -> {
if (newValue) {
Notification walletFundedNotification = new Notification()
.headLine(Res.get("notification.walletUpdate.headline"))
.notification(Res.get("notification.walletUpdate.msg", btcFormatter.formatCoinWithCode(model.dataModel.getTotalToPayAsCoin().get())))
.autoClose();
walletFundedNotification.show();
}
};
}
private void updateMarketPriceAvailable() {
@ -687,6 +699,9 @@ public class CreateOfferView extends ActivatableViewAndModel<AnchorPane, CreateO
volumeTextField.focusedProperty().addListener(volumeFocusedListener);
buyerSecurityDepositInputTextField.focusedProperty().addListener(buyerSecurityDepositFocusedListener);
// notifications
model.dataModel.getShowWalletFundedNotification().addListener(getShowWalletFundedNotificationListener);
// warnings
model.errorMessage.addListener(errorMessageListener);
// model.dataModel.feeFromFundingTxProperty.addListener(feeFromFundingTxListener);
@ -710,6 +725,9 @@ public class CreateOfferView extends ActivatableViewAndModel<AnchorPane, CreateO
volumeTextField.focusedProperty().removeListener(volumeFocusedListener);
buyerSecurityDepositInputTextField.focusedProperty().removeListener(buyerSecurityDepositFocusedListener);
// notifications
model.dataModel.getShowWalletFundedNotification().removeListener(getShowWalletFundedNotificationListener);
// warnings
model.errorMessage.removeListener(errorMessageListener);
// model.dataModel.feeFromFundingTxProperty.removeListener(feeFromFundingTxListener);

View File

@ -46,7 +46,6 @@ import io.bisq.core.user.User;
import io.bisq.core.util.CoinUtil;
import io.bisq.gui.main.offer.OfferDataModel;
import io.bisq.gui.main.overlays.popups.Popup;
import io.bisq.gui.util.BSFormatter;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
@ -74,6 +73,7 @@ class TakeOfferDataModel extends OfferDataModel {
private final User user;
private final FeeService feeService;
private final FilterManager filterManager;
private final Preferences preferences;
private final PriceFeedService priceFeedService;
private final TradeWalletService tradeWalletService;
private final AccountAgeWitnessService accountAgeWitnessService;
@ -110,14 +110,15 @@ class TakeOfferDataModel extends OfferDataModel {
BtcWalletService btcWalletService, BsqWalletService bsqWalletService,
User user, FeeService feeService, FilterManager filterManager,
Preferences preferences, PriceFeedService priceFeedService, TradeWalletService tradeWalletService,
AccountAgeWitnessService accountAgeWitnessService, BSFormatter formatter) {
super(btcWalletService, preferences, formatter);
AccountAgeWitnessService accountAgeWitnessService) {
super(btcWalletService);
this.tradeManager = tradeManager;
this.bsqWalletService = bsqWalletService;
this.user = user;
this.feeService = feeService;
this.filterManager = filterManager;
this.preferences = preferences;
this.priceFeedService = priceFeedService;
this.tradeWalletService = tradeWalletService;
this.accountAgeWitnessService = accountAgeWitnessService;

View File

@ -111,6 +111,8 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
private int gridRow = 0;
private boolean offerDetailsWindowDisplayed, clearXchangeWarningDisplayed;
private SimpleBooleanProperty errorPopupDisplayed;
private ChangeListener<Boolean> getShowWalletFundedNotificationListener;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor, lifecycle
@ -148,6 +150,17 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
amountTextField.setText(model.amount.get());
};
getShowWalletFundedNotificationListener = (observable, oldValue, newValue) -> {
if (newValue) {
Notification walletFundedNotification = new Notification()
.headLine(Res.get("notification.walletUpdate.headline"))
.notification(Res.get("notification.walletUpdate.msg", formatter.formatCoinWithCode(model.dataModel.getTotalToPayAsCoin().get())))
.autoClose();
walletFundedNotification.show();
}
};
GUIUtil.focusWhenAddedToScene(amountTextField);
}
@ -196,33 +209,9 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
if (!model.isRange()) {
showNextStepAfterAmountIsSet();
}
}
private void showInsufficientBsqFundsForBtcFeePaymentPopup() {
Coin takerFee = model.dataModel.getTakerFee(false);
String message = null;
if (takerFee != null)
message = Res.get("popup.warning.insufficientBsqFundsForBtcFeePayment",
bsqFormatter.formatCoinWithCode(takerFee.subtract(model.dataModel.getBsqBalance())));
else if (model.dataModel.getBsqBalance().isZero())
message = Res.get("popup.warning.noBsqFundsForBtcFeePayment");
if (message != null)
//noinspection unchecked
new Popup<>().warning(message)
.actionButtonTextWithGoTo("navigation.dao.wallet.receive")
.onAction(() -> navigation.navigateTo(MainView.class, DaoView.class, BsqWalletView.class, BsqReceiveView.class))
.show();
}
private void maybeShowClearXchangeWarning() {
if (model.getPaymentMethod().getId().equals(PaymentMethod.CLEAR_X_CHANGE_ID) &&
!clearXchangeWarningDisplayed) {
clearXchangeWarningDisplayed = true;
UserThread.runAfter(GUIUtil::showClearXchangeWarning,
500, TimeUnit.MILLISECONDS);
}
// notifications
model.dataModel.getShowWalletFundedNotification().addListener(getShowWalletFundedNotificationListener);
}
@Override
@ -236,6 +225,8 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
if (waitingForFundsBusyAnimation != null)
waitingForFundsBusyAnimation.stop();
model.dataModel.getShowWalletFundedNotification().removeListener(getShowWalletFundedNotificationListener);
}
@ -981,6 +972,34 @@ public class TakeOfferView extends ActivatableViewAndModel<AnchorPane, TakeOffer
// Utils
///////////////////////////////////////////////////////////////////////////////////////////
private void showInsufficientBsqFundsForBtcFeePaymentPopup() {
Coin takerFee = model.dataModel.getTakerFee(false);
String message = null;
if (takerFee != null)
message = Res.get("popup.warning.insufficientBsqFundsForBtcFeePayment",
bsqFormatter.formatCoinWithCode(takerFee.subtract(model.dataModel.getBsqBalance())));
else if (model.dataModel.getBsqBalance().isZero())
message = Res.get("popup.warning.noBsqFundsForBtcFeePayment");
if (message != null)
//noinspection unchecked
new Popup<>().warning(message)
.actionButtonTextWithGoTo("navigation.dao.wallet.receive")
.onAction(() -> navigation.navigateTo(MainView.class, DaoView.class, BsqWalletView.class, BsqReceiveView.class))
.show();
}
private void maybeShowClearXchangeWarning() {
if (model.getPaymentMethod().getId().equals(PaymentMethod.CLEAR_X_CHANGE_ID) &&
!clearXchangeWarningDisplayed) {
clearXchangeWarningDisplayed = true;
UserThread.runAfter(GUIUtil::showClearXchangeWarning,
500, TimeUnit.MILLISECONDS);
}
}
private Tuple2<Label, VBox> getTradeInputBox(HBox amountValueBox, String promptText) {
Label descriptionLabel = new AutoTooltipLabel(promptText);
descriptionLabel.setId("input-description-label");