]> git.ktnx.net Git - mobile-ledger.git/commitdiff
machinery for tracking currency format (e.g. locale) changes
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 9 Jan 2020 21:10:04 +0000 (23:10 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 9 Jan 2020 21:10:04 +0000 (23:10 +0200)
app/src/main/java/net/ktnx/mobileledger/App.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java

index bec320e00d3e8db688ede6bf2bc0d559ae690986..fb69138b8cdcdf273fe29f410daa49007d428dbf 100644 (file)
@@ -33,6 +33,7 @@ import java.net.Authenticator;
 import java.net.MalformedURLException;
 import java.net.PasswordAuthentication;
 import java.net.URL;
+import java.util.Locale;
 
 public class App extends Application {
     public static App instance;
@@ -48,6 +49,7 @@ public class App extends Application {
         instance = this;
         super.onCreate();
         updateMonthNames();
+        Data.refreshCurrencyData(Locale.getDefault());
         Authenticator.setDefault(new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
index 4a79095a1e95b3d28f915f507b435854daf5c71c..2cc401980815d250341e3a1da65378c9c196c998 100644 (file)
@@ -33,6 +33,7 @@ import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.ObservableList;
 
 import java.lang.ref.WeakReference;
+import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
@@ -51,6 +52,8 @@ public final class Data {
     public static MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
             new MutableLiveData<>(null);
     public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
+    public static MutableLiveData<Currency.Position> currencySymbolPosition =
+            new MutableLiveData<>();
     private static AtomicInteger backgroundTaskCount = new AtomicInteger(0);
     private static Locker profilesLocker = new Locker();
     private static RetrieveTransactionsTask retrieveTransactionsTask;
@@ -149,4 +152,21 @@ public final class Data {
     public static void transactionRetrievalDone() {
         retrieveTransactionsTask = null;
     }
-}
+    public static void refreshCurrencyData(Locale locale) {
+        NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
+        java.util.Currency currency = formatter.getCurrency();
+        Logger.debug("locale",
+                String.format("Discovering currency symbol position for locale %s (currency is %s)",
+                        locale.toString(), currency.toString()));
+        String formatted = formatter.format(1234.56f);
+        Logger.debug("locale", String.format("1234.56 formats as '%s'", formatted));
+        String symbol = currency.getSymbol();
+        if (formatted.startsWith(symbol))
+            currencySymbolPosition.setValue(Currency.Position.before);
+        else if (formatted.endsWith(symbol))
+            currencySymbolPosition.setValue(Currency.Position.after);
+        else
+            currencySymbolPosition.setValue(Currency.Position.none);
+    }
+
+}
\ No newline at end of file