Use correct currency position for all locales.
This commit is contained in:
parent
113393de8f
commit
4ae7312c7f
3 changed files with 110 additions and 6 deletions
|
@ -16,13 +16,21 @@ import org.whispersystems.libsignal.util.guava.Optional;
|
||||||
import org.whispersystems.signalservice.api.payments.Money;
|
import org.whispersystems.signalservice.api.payments.Money;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.DecimalFormatSymbols;
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
|
import java.util.Currency;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class FiatMoneyUtil {
|
public final class FiatMoneyUtil {
|
||||||
|
|
||||||
private static final String TAG = Log.tag(FiatMoneyUtil.class);
|
private static final String TAG = Log.tag(FiatMoneyUtil.class);
|
||||||
|
|
||||||
|
private static final char CURRENCY_SYMBOL_PLACE_HOLDER = '\u00A4';
|
||||||
|
private static final char NON_BREAKING_WHITESPACE = '\u00A0';
|
||||||
|
|
||||||
|
private FiatMoneyUtil() {}
|
||||||
|
|
||||||
public static @NonNull LiveData<Optional<FiatMoney>> getExchange(@NonNull LiveData<Money> amount) {
|
public static @NonNull LiveData<Optional<FiatMoney>> getExchange(@NonNull LiveData<Money> amount) {
|
||||||
return LiveDataUtil.mapAsync(amount, a -> {
|
return LiveDataUtil.mapAsync(amount, a -> {
|
||||||
try {
|
try {
|
||||||
|
@ -61,6 +69,38 @@ public class FiatMoneyUtil {
|
||||||
return formattedAmount;
|
return formattedAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prefixes or postfixes the currency symbol based on the formatter for the currency.
|
||||||
|
*
|
||||||
|
* @param value String so that you can force trailing zeros.
|
||||||
|
*/
|
||||||
|
public static String manualFormat(@NonNull Currency currency, @NonNull String value) {
|
||||||
|
NumberFormat format = NumberFormat.getCurrencyInstance();
|
||||||
|
format.setCurrency(currency);
|
||||||
|
|
||||||
|
DecimalFormat decimalFormat = (DecimalFormat) format;
|
||||||
|
DecimalFormatSymbols decimalFormatSymbols = decimalFormat.getDecimalFormatSymbols();
|
||||||
|
String symbol = decimalFormatSymbols.getCurrencySymbol();
|
||||||
|
String localizedPattern = decimalFormat.toLocalizedPattern();
|
||||||
|
int currencySymbolIndex = localizedPattern.indexOf(CURRENCY_SYMBOL_PLACE_HOLDER);
|
||||||
|
boolean prefixSymbol = currencySymbolIndex <= 0;
|
||||||
|
|
||||||
|
if (currencySymbolIndex == 0) {
|
||||||
|
char cAfterSymbol = localizedPattern.charAt(currencySymbolIndex + 1);
|
||||||
|
if (Character.isWhitespace(cAfterSymbol) || cAfterSymbol == NON_BREAKING_WHITESPACE) {
|
||||||
|
symbol = symbol + cAfterSymbol;
|
||||||
|
}
|
||||||
|
} else if (currencySymbolIndex > 0) {
|
||||||
|
char cBeforeSymbol = localizedPattern.charAt(currencySymbolIndex - 1);
|
||||||
|
if (Character.isWhitespace(cBeforeSymbol) || cBeforeSymbol == NON_BREAKING_WHITESPACE) {
|
||||||
|
symbol = cBeforeSymbol + symbol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return prefixSymbol ? symbol + value
|
||||||
|
: value + symbol;
|
||||||
|
}
|
||||||
|
|
||||||
public static FormatOptions formatOptions() {
|
public static FormatOptions formatOptions() {
|
||||||
return new FormatOptions();
|
return new FormatOptions();
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.thoughtcrime.securesms.payments.create;
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.graphics.drawable.LayerDrawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
@ -148,12 +147,12 @@ public class CreatePaymentFragment extends LoggingFragment {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeInfoIcon() {
|
private void initializeInfoIcon() {
|
||||||
spacer = Objects.requireNonNull(AppCompatResources.getDrawable(requireContext(), R.drawable.payment_info_pad));
|
spacer = Objects.requireNonNull(AppCompatResources.getDrawable(requireContext(), R.drawable.payment_info_pad));
|
||||||
infoIcon = Objects.requireNonNull(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_update_info_16));
|
infoIcon = Objects.requireNonNull(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_update_info_16));
|
||||||
|
|
||||||
DrawableCompat.setTint(infoIcon, exchange.getCurrentTextColor());
|
DrawableCompat.setTint(infoIcon, exchange.getCurrentTextColor());
|
||||||
|
|
||||||
spacer.setBounds(0, 0, ViewUtil.dpToPx(13), ViewUtil.dpToPx(16));
|
spacer.setBounds(0, 0, ViewUtil.dpToPx(8), ViewUtil.dpToPx(16));
|
||||||
infoIcon.setBounds(0, 0, ViewUtil.dpToPx(16), ViewUtil.dpToPx(16));
|
infoIcon.setBounds(0, 0, ViewUtil.dpToPx(16), ViewUtil.dpToPx(16));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,6 +181,7 @@ public class CreatePaymentFragment extends LoggingFragment {
|
||||||
break;
|
break;
|
||||||
case FIAT_MONEY:
|
case FIAT_MONEY:
|
||||||
amount.setMoney(inputState.getMoney(), false, inputState.getExchangeRate().get().getTimestamp());
|
amount.setMoney(inputState.getMoney(), false, inputState.getExchangeRate().get().getTimestamp());
|
||||||
|
amount.append(SpanUtil.buildImageSpan(spacer));
|
||||||
amount.append(SpanUtil.buildImageSpan(infoIcon));
|
amount.append(SpanUtil.buildImageSpan(infoIcon));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -205,8 +205,7 @@ public class CreatePaymentFragment extends LoggingFragment {
|
||||||
break;
|
break;
|
||||||
case FIAT_MONEY:
|
case FIAT_MONEY:
|
||||||
Currency currency = inputState.getFiatMoney().get().getCurrency();
|
Currency currency = inputState.getFiatMoney().get().getCurrency();
|
||||||
exchange.setText(new SpannableStringBuilder().append(currency.getSymbol())
|
exchange.setText(FiatMoneyUtil.manualFormat(currency, inputState.getFiatAmount()));
|
||||||
.append(inputState.getFiatAmount()));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package org.thoughtcrime.securesms.payments;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Currency;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public final class FiatMoneyUtil_manualFormat_Test {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void gbp_UK() {
|
||||||
|
Locale.setDefault(Locale.UK);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("GBP"), "1.20");
|
||||||
|
|
||||||
|
assertEquals("£1.20", format);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void eur_France() {
|
||||||
|
Locale.setDefault(Locale.FRANCE);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("EUR"), "2");
|
||||||
|
|
||||||
|
assertEquals("2 €", format);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void aud_France() {
|
||||||
|
Locale.setDefault(Locale.FRANCE);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("AUD"), "1");
|
||||||
|
|
||||||
|
assertEquals("1 AUD", format);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void usd_US() {
|
||||||
|
Locale.setDefault(Locale.US);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("USD"), "4.0");
|
||||||
|
|
||||||
|
assertEquals("$4.0", format);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void cad_US() {
|
||||||
|
Locale.setDefault(Locale.US);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("CAD"), "5.00");
|
||||||
|
|
||||||
|
assertEquals("CAD5.00", format);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void cad_Canada() {
|
||||||
|
Locale.setDefault(Locale.CANADA);
|
||||||
|
|
||||||
|
String format = FiatMoneyUtil.manualFormat(Currency.getInstance("CAD"), "5.12");
|
||||||
|
|
||||||
|
assertEquals("$5.12", format);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue