Skip to content

Commit

Permalink
Reduce bs formatter interface (bisq-network#3253)
Browse files Browse the repository at this point in the history
* Extract FormattingUtils from BSFormatter

All formatting related functions that are depended on by core and
desktop jars are extracted to the new class FormattingUtils.

* Remove dead Code from BSFormatter

* Move Currency related helpers out of BSFormatter to CurrencyUtils

* Move functions that only have 1 call-site out of BSFormatter

Make them private instance functions at call site to minimize
dependencies.
  • Loading branch information
ripcurlx authored Nov 19, 2019
2 parents e157ac3 + cfaa23e commit 30b1f02
Show file tree
Hide file tree
Showing 48 changed files with 595 additions and 555 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/bisq/core/app/WalletAppSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import bisq.core.locale.Res;
import bisq.core.user.Preferences;
import bisq.core.util.BSFormatter;
import bisq.core.util.FormattingUtils;

import org.bitcoinj.core.VersionMessage;
import org.bitcoinj.store.BlockStoreException;
Expand Down Expand Up @@ -123,7 +124,7 @@ void init(@Nullable Consumer<String> chainFileLockedExceptionHandler,
result = Res.get("mainView.footer.btcInfo",
peers,
Res.get("mainView.footer.btcInfo.synchronizingWith"),
getBtcNetworkAsString() + ": " + BSFormatter.formatToPercentWithSymbol(percentage));
getBtcNetworkAsString() + ": " + FormattingUtils.formatToPercentWithSymbol(percentage));
} else {
result = Res.get("mainView.footer.btcInfo",
peers,
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/bisq/core/dao/presentation/DaoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import bisq.core.dao.state.model.governance.DaoPhase;
import bisq.core.locale.Res;
import bisq.core.util.BSFormatter;
import bisq.core.util.FormattingUtils;

import java.text.SimpleDateFormat;

Expand All @@ -40,8 +41,8 @@ public static String getNextPhaseDuration(int height, DaoPhase.Phase phase, DaoF
long now = new Date().getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM", Locale.getDefault());
SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
String startDateTime = BSFormatter.formatDateTime(new Date(now + (start - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String endDateTime = BSFormatter.formatDateTime(new Date(now + (end - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String startDateTime = FormattingUtils.formatDateTime(new Date(now + (start - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String endDateTime = FormattingUtils.formatDateTime(new Date(now + (end - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);

return Res.get("dao.cycle.phaseDurationWithoutBlocks", start, end, startDateTime, endDateTime);
}
Expand All @@ -53,9 +54,9 @@ public static String getPhaseDuration(int height, DaoPhase.Phase phase, DaoFacad
long now = new Date().getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd MMM", Locale.getDefault());
SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.getDefault());
String startDateTime = BSFormatter.formatDateTime(new Date(now + (start - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String endDateTime = BSFormatter.formatDateTime(new Date(now + (end - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String durationTime = BSFormatter.formatDurationAsWords(duration * 10 * 60 * 1000, false, false);
String startDateTime = FormattingUtils.formatDateTime(new Date(now + (start - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String endDateTime = FormattingUtils.formatDateTime(new Date(now + (end - height) * 10 * 60 * 1000L), dateFormatter, timeFormatter);
String durationTime = FormattingUtils.formatDurationAsWords(duration * 10 * 60 * 1000, false, false);
return Res.get("dao.cycle.phaseDuration", duration, durationTime, start, end, startDateTime, endDateTime);
}
}
25 changes: 25 additions & 0 deletions core/src/main/java/bisq/core/locale/CurrencyUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,29 @@ public static List<CryptoCurrency> getActiveSortedCryptoCurrencies(AssetService
.filter(e -> !filterManager.isCurrencyBanned(e.getCode()))
.collect(Collectors.toList());
}

public static String getCurrencyPair(String currencyCode) {
if (isFiatCurrency(currencyCode))
return Res.getBaseCurrencyCode() + "/" + currencyCode;
else
return currencyCode + "/" + Res.getBaseCurrencyCode();
}

public static String getCounterCurrency(String currencyCode) {
if (isFiatCurrency(currencyCode))
return currencyCode;
else
return Res.getBaseCurrencyCode();
}

public static String getPriceWithCurrencyCode(String currencyCode) {
return getPriceWithCurrencyCode(currencyCode, "shared.priceInCurForCur");
}

public static String getPriceWithCurrencyCode(String currencyCode, String translationKey) {
if (isCryptoCurrency(currencyCode))
return Res.get(translationKey, Res.getBaseCurrencyCode(), currencyCode);
else
return Res.get(translationKey, currencyCode, Res.getBaseCurrencyCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import bisq.core.provider.price.PriceFeedService;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
import bisq.core.util.FormattingUtils;

import bisq.common.crypto.KeyRing;
import bisq.common.util.MathUtils;
Expand Down Expand Up @@ -180,9 +181,9 @@ else if (!isFiatCurrency && !isSellOffer)
ratio = Math.abs(ratio);
String msg = Res.get("account.notifications.marketAlert.message.msg",
direction,
BSFormatter.getCurrencyPair(currencyCode),
BSFormatter.formatPrice(offerPrice),
BSFormatter.formatToPercentWithSymbol(ratio / 10000d),
CurrencyUtil.getCurrencyPair(currencyCode),
FormattingUtils.formatPrice(offerPrice),
FormattingUtils.formatToPercentWithSymbol(ratio / 10000d),
marketDir,
Res.get(offer.getPaymentMethod().getId()),
shortOfferId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import bisq.core.provider.price.PriceFeedService;
import bisq.core.user.User;
import bisq.core.util.BSFormatter;
import bisq.core.util.FormattingUtils;

import bisq.common.util.MathUtils;

Expand Down Expand Up @@ -70,8 +71,8 @@ private void update() {
if (priceAsLong > filter.getHigh() || priceAsLong < filter.getLow()) {
String msg = Res.get("account.notifications.priceAlert.message.msg",
currencyName,
BSFormatter.formatMarketPrice(priceAsDouble, currencyCode),
BSFormatter.getCurrencyPair(currencyCode));
FormattingUtils.formatMarketPrice(priceAsDouble, currencyCode),
CurrencyUtil.getCurrencyPair(currencyCode));
MobileMessage message = new MobileMessage(Res.get("account.notifications.priceAlert.message.title", currencyName),
msg,
MobileMessageType.PRICE);
Expand Down
Loading

0 comments on commit 30b1f02

Please sign in to comment.