]> git.ktnx.net Git - mobile-ledger.git/commitdiff
format amounts when the input field loses focus
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 28 Feb 2021 21:26:26 +0000 (23:26 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 1 Mar 2021 06:00:42 +0000 (06:00 +0000)
previously this relied on an update via the model

app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java

index e4afc812aa0e35ae7dfd83d86f83ec64e76443b6..d93752e8da660ea53cef2fb9798b05c1b8de85fb 100644 (file)
@@ -56,7 +56,7 @@ import java.util.Objects;
 
 class NewTransactionItemHolder extends RecyclerView.ViewHolder
         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
-    private final String decimalDot;
+    private final String decimalDot = ".";
     private final MobileLedgerProfile mProfile;
     private final NewTransactionRowBinding b;
     private final NewTransactionItemsAdapter mAdapter;
@@ -90,7 +90,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         mProfile = Data.getProfile();
 
-        View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
+        @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
             final int id = v.getId();
             if (hasFocus) {
                 boolean wasSyncing = syncingData;
@@ -119,6 +119,19 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     syncingData = wasSyncing;
                 }
             }
+            else {  // lost focus
+                if (id == R.id.account_row_acc_amounts) {
+                    try {
+                        String input = String.valueOf(b.accountRowAccAmounts.getText());
+                        input = input.replace(decimalSeparator, decimalDot);
+                        b.accountRowAccAmounts.setText(
+                                String.format("%4.2f", Float.parseFloat(input)));
+                    }
+                    catch (NumberFormatException ex) {
+                        // ignored
+                    }
+                }
+            }
 
             if (id == R.id.comment) {
                 commentFocusChanged(b.comment, hasFocus);
@@ -147,8 +160,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 DecimalFormatSymbols.getInstance(locale)
                                     .getMonetaryDecimalSeparator()));
 
-        decimalDot = ".";
-
         final TextWatcher tw = new TextWatcher() {
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {