From 24ba9215cd12f65605b23c2f5d53037258afd019 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sun, 25 Oct 2020 10:41:19 -0300 Subject: [PATCH] Refactor PendingTradesViewModel methods -> TradeUtil & OfferUtil --- .../main/java/bisq/core/offer/OfferUtil.java | 4 + .../main/java/bisq/core/trade/TradeUtil.java | 96 +++++++++++++++ .../pendingtrades/PendingTradesViewModel.java | 112 ++++++------------ 3 files changed, 137 insertions(+), 75 deletions(-) diff --git a/core/src/main/java/bisq/core/offer/OfferUtil.java b/core/src/main/java/bisq/core/offer/OfferUtil.java index 373df679073..84e783f5b14 100644 --- a/core/src/main/java/bisq/core/offer/OfferUtil.java +++ b/core/src/main/java/bisq/core/offer/OfferUtil.java @@ -261,6 +261,10 @@ public boolean isBsqForTakerFeeAvailable(@Nullable Coin amount) { return !availableBalance.subtract(takerFee).isNegative(); } + public boolean isBlockChainPaymentMethod(Offer offer) { + return offer != null && offer.getPaymentMethod().isAsset(); + } + public Optional getFeeInUserFiatCurrency(Coin makerFee, boolean isCurrencyForMakerFeeBtc, CoinFormatter bsqFormatter) { diff --git a/core/src/main/java/bisq/core/trade/TradeUtil.java b/core/src/main/java/bisq/core/trade/TradeUtil.java index c93e5e2be3b..a89dec32459 100644 --- a/core/src/main/java/bisq/core/trade/TradeUtil.java +++ b/core/src/main/java/bisq/core/trade/TradeUtil.java @@ -18,6 +18,8 @@ package bisq.core.trade; import bisq.core.btc.wallet.BtcWalletService; +import bisq.core.locale.Res; +import bisq.core.offer.Offer; import bisq.common.crypto.KeyRing; import bisq.common.util.Tuple2; @@ -26,10 +28,18 @@ import javax.inject.Inject; import javax.inject.Singleton; +import java.util.Date; import java.util.Objects; import lombok.extern.slf4j.Slf4j; +import javax.annotation.Nullable; + +import static bisq.core.locale.CurrencyUtil.getCurrencyPair; +import static bisq.core.locale.CurrencyUtil.isFiatCurrency; +import static bisq.core.util.FormattingUtils.formatDurationAsWords; +import static com.google.common.base.Preconditions.checkNotNull; + /** * This class contains trade utility methods. */ @@ -108,4 +118,90 @@ public Tuple2 getTradeAddresses(Trade trade) { return new Tuple2<>(multiSigAddress.getAddressString(), payoutAddress); } + + public long getRemainingTradeDuration(Trade trade) { + return trade.getMaxTradePeriodDate() != null + ? trade.getMaxTradePeriodDate().getTime() - new Date().getTime() + : getMaxTradePeriod(trade); + } + + public long getMaxTradePeriod(Trade trade) { + return trade.getOffer() != null + ? trade.getOffer().getPaymentMethod().getMaxTradePeriod() + : 0; + } + + public double getRemainingTradeDurationAsPercentage(Trade trade) { + long maxPeriod = getMaxTradePeriod(trade); + long remaining = getRemainingTradeDuration(trade); + if (maxPeriod != 0) { + return 1 - (double) remaining / (double) maxPeriod; + } else + return 0; + } + + public String getRemainingTradeDurationAsWords(Trade trade) { + return formatDurationAsWords(Math.max(0, getRemainingTradeDuration(trade))); + } + + @Nullable + public Date getHalfTradePeriodDate(Trade trade) { + return trade != null ? trade.getHalfTradePeriodDate() : null; + } + + public Date getDateForOpenDispute(Trade trade) { + return new Date(new Date().getTime() + getRemainingTradeDuration(trade)); + } + + public String getMarketDescription(Trade trade) { + if (trade == null) + return ""; + + checkNotNull(trade.getOffer()); + checkNotNull(trade.getOffer().getCurrencyCode()); + return getCurrencyPair(trade.getOffer().getCurrencyCode()); + } + + public String getPaymentMethodNameWithCountryCode(Trade trade) { + String paymentMethodDescription = ""; + if (trade != null) { + Offer offer = trade.getOffer(); + checkNotNull(offer); + checkNotNull(offer.getPaymentMethod()); + paymentMethodDescription = offer.getPaymentMethodNameWithCountryCode(); + } + return paymentMethodDescription; + } + + /** + * Returns a string describing a trader's role. + * + * @param isBuyerMakerAndSellerTaker boolean + * @param isMaker boolean + * @param currencyCode String + * @return String describing a trader's role + */ + public String getRole(boolean isBuyerMakerAndSellerTaker, boolean isMaker, String currencyCode) { + if (isFiatCurrency(currencyCode)) { + String baseCurrencyCode = Res.getBaseCurrencyCode(); + if (isBuyerMakerAndSellerTaker) + return isMaker + ? Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.buyer")) + : Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.seller")); + else + return isMaker + ? Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.seller")) + : Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.buyer")); + } else { + if (isBuyerMakerAndSellerTaker) + return isMaker + ? Res.get("formatter.asMaker", currencyCode, Res.get("shared.seller")) + : Res.get("formatter.asTaker", currencyCode, Res.get("shared.buyer")); + else + return isMaker + ? Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer")) + : Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller")); + } + + } } diff --git a/desktop/src/main/java/bisq/desktop/main/portfolio/pendingtrades/PendingTradesViewModel.java b/desktop/src/main/java/bisq/desktop/main/portfolio/pendingtrades/PendingTradesViewModel.java index 35934419fdf..53369839bd0 100644 --- a/desktop/src/main/java/bisq/desktop/main/portfolio/pendingtrades/PendingTradesViewModel.java +++ b/desktop/src/main/java/bisq/desktop/main/portfolio/pendingtrades/PendingTradesViewModel.java @@ -24,13 +24,13 @@ import bisq.core.account.witness.AccountAgeWitnessService; import bisq.core.btc.wallet.Restrictions; -import bisq.core.locale.CurrencyUtil; -import bisq.core.locale.Res; import bisq.core.network.MessageState; import bisq.core.offer.Offer; +import bisq.core.offer.OfferUtil; import bisq.core.provider.fee.FeeService; import bisq.core.trade.Contract; import bisq.core.trade.Trade; +import bisq.core.trade.TradeUtil; import bisq.core.trade.closed.ClosedTradableManager; import bisq.core.user.User; import bisq.core.util.FormattingUtils; @@ -97,6 +97,8 @@ enum SellerState implements State { final AccountAgeWitnessService accountAgeWitnessService; public final P2PService p2PService; private final ClosedTradableManager closedTradableManager; + private final OfferUtil offerUtil; + private final TradeUtil tradeUtil; public final ClockWatcher clockWatcher; @Getter private final User user; @@ -120,6 +122,8 @@ public PendingTradesViewModel(PendingTradesDataModel dataModel, BtcAddressValidator btcAddressValidator, P2PService p2PService, ClosedTradableManager closedTradableManager, + OfferUtil offerUtil, + TradeUtil tradeUtil, AccountAgeWitnessService accountAgeWitnessService, ClockWatcher clockWatcher, User user) { @@ -130,6 +134,8 @@ public PendingTradesViewModel(PendingTradesDataModel dataModel, this.btcAddressValidator = btcAddressValidator; this.p2PService = p2PService; this.closedTradableManager = closedTradableManager; + this.offerUtil = offerUtil; + this.tradeUtil = tradeUtil; this.accountAgeWitnessService = accountAgeWitnessService; this.clockWatcher = clockWatcher; this.user = user; @@ -199,55 +205,30 @@ ReadOnlyObjectProperty getSellerState() { } public String getPayoutAmount() { - return dataModel.getTrade() != null ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getPayoutAmount()) : ""; + return dataModel.getTrade() != null + ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getPayoutAmount()) + : ""; } String getMarketLabel(PendingTradesListItem item) { - if ((item == null)) - return ""; - - checkNotNull(item.getTrade().getOffer()); - checkNotNull(item.getTrade().getOffer().getCurrencyCode()); - return CurrencyUtil.getCurrencyPair(item.getTrade().getOffer().getCurrencyCode()); - } - - private long getMaxTradePeriod() { - return dataModel.getOffer() != null ? dataModel.getOffer().getPaymentMethod().getMaxTradePeriod() : 0; - } - - @Nullable - private Date getMaxTradePeriodDate() { - return dataModel.getTrade() != null ? dataModel.getTrade().getMaxTradePeriodDate() : null; - } - - @Nullable - private Date getHalfTradePeriodDate() { - return dataModel.getTrade() != null ? dataModel.getTrade().getHalfTradePeriodDate() : null; - } - - private long getRemainingTradeDuration() { - return getMaxTradePeriodDate() != null ? getMaxTradePeriodDate().getTime() - new Date().getTime() : getMaxTradePeriod(); + return item == null ? "" : tradeUtil.getMarketDescription(item.getTrade()); } public String getRemainingTradeDurationAsWords() { - return FormattingUtils.formatDurationAsWords(Math.max(0, getRemainingTradeDuration())); + return tradeUtil.getRemainingTradeDurationAsWords(dataModel.getTrade()); } public double getRemainingTradeDurationAsPercentage() { - long maxPeriod = getMaxTradePeriod(); - long remaining = getRemainingTradeDuration(); - if (maxPeriod != 0) { - return 1 - (double) remaining / (double) maxPeriod; - } else - return 0; + return tradeUtil.getRemainingTradeDurationAsPercentage(dataModel.getTrade()); } public String getDateForOpenDispute() { - return DisplayUtils.formatDateTime(new Date(new Date().getTime() + getRemainingTradeDuration())); + return DisplayUtils.formatDateTime(tradeUtil.getDateForOpenDispute(dataModel.getTrade())); } public boolean showWarning() { - return getHalfTradePeriodDate() != null && new Date().after(getHalfTradePeriodDate()); + Date halfTradePeriodDate = tradeUtil.getHalfTradePeriodDate(dataModel.getTrade()); + return halfTradePeriodDate != null && new Date().after(halfTradePeriodDate); } public boolean showDispute() { @@ -263,36 +244,36 @@ String getMyRole(PendingTradesListItem item) { Offer offer = trade.getOffer(); checkNotNull(offer); checkNotNull(offer.getCurrencyCode()); - return getRole(contract.isBuyerMakerAndSellerTaker(), dataModel.isMaker(offer), offer.getCurrencyCode()); + return tradeUtil.getRole(contract.isBuyerMakerAndSellerTaker(), + dataModel.isMaker(offer), + offer.getCurrencyCode()); } else { return ""; } } String getPaymentMethod(PendingTradesListItem item) { - String result = ""; - if (item != null) { - Offer offer = item.getTrade().getOffer(); - checkNotNull(offer); - checkNotNull(offer.getPaymentMethod()); - result = offer.getPaymentMethodNameWithCountryCode(); - } - return result; + return item == null ? "" : tradeUtil.getPaymentMethodNameWithCountryCode(item.getTrade()); } // summary public String getTradeVolume() { - return dataModel.getTrade() != null ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getTradeAmount()) : ""; + return dataModel.getTrade() != null + ? btcFormatter.formatCoinWithCode(dataModel.getTrade().getTradeAmount()) + : ""; } public String getFiatVolume() { - return dataModel.getTrade() != null ? DisplayUtils.formatVolumeWithCode(dataModel.getTrade().getTradeVolume()) : ""; + return dataModel.getTrade() != null + ? DisplayUtils.formatVolumeWithCode(dataModel.getTrade().getTradeVolume()) + : ""; } public String getTxFee() { if (trade != null && trade.getTradeAmount() != null) { Coin txFee = dataModel.getTxFee(); - String percentage = GUIUtil.getPercentageOfTradeAmount(txFee, trade.getTradeAmount(), + String percentage = GUIUtil.getPercentageOfTradeAmount(txFee, + trade.getTradeAmount(), Coin.ZERO); return btcFormatter.formatCoinWithCode(txFee) + percentage; } else { @@ -344,7 +325,7 @@ public String getSecurityDeposit() { } public boolean isBlockChainMethod() { - return dataModel.getOffer() != null && dataModel.getOffer().getPaymentMethod().isAsset(); + return offerUtil.isBlockChainPaymentMethod(dataModel.getOffer()); } public int getNumPastTrades(Trade trade) { @@ -362,7 +343,13 @@ public int getNumPastTrades(Trade trade) { .collect(Collectors.toSet()) .size(); } - + + @Nullable + private Date getMaxTradePeriodDate() { + return dataModel.getTrade() != null + ? dataModel.getTrade().getMaxTradePeriodDate() + : null; + } /////////////////////////////////////////////////////////////////////////////////////////// // States @@ -479,29 +466,4 @@ private void onTradeStateChanged(Trade.State tradeState) { break; } } - - private static String getRole(boolean isBuyerMakerAndSellerTaker, boolean isMaker, String currencyCode) { - if (CurrencyUtil.isFiatCurrency(currencyCode)) { - String baseCurrencyCode = Res.getBaseCurrencyCode(); - if (isBuyerMakerAndSellerTaker) - return isMaker ? - Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.buyer")) : - Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.seller")); - else - return isMaker ? - Res.get("formatter.asMaker", baseCurrencyCode, Res.get("shared.seller")) : - Res.get("formatter.asTaker", baseCurrencyCode, Res.get("shared.buyer")); - } else { - if (isBuyerMakerAndSellerTaker) - return isMaker ? - Res.get("formatter.asMaker", currencyCode, Res.get("shared.seller")) : - Res.get("formatter.asTaker", currencyCode, Res.get("shared.buyer")); - else - return isMaker ? - Res.get("formatter.asMaker", currencyCode, Res.get("shared.buyer")) : - Res.get("formatter.asTaker", currencyCode, Res.get("shared.seller")); - } - - } - }