]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
format amounts when the input field loses focus
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionItemHolder.java
index 1725f91a64ae0c58ffc7ca5652db67007a7d1e3f..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) {
@@ -264,16 +275,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                              ignoreFocusChanges = false;
                          }
                      });
-        adapter.model.getAccountCount()
-                     .observe(activity, count -> {
-                         final int adapterPosition = getAdapterPosition();
-                         final int layoutPosition = getLayoutPosition();
-
-                         if (adapterPosition == count)
-                             b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_DONE);
-                         else
-                             b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_NEXT);
-                     });
 
         Data.currencyGap.observe(activity,
                 hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
@@ -606,6 +607,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                         b.accountRowAccAmounts.setHint(amountHint);
                     }
 
+                    b.accountRowAccAmounts.setImeOptions(
+                            acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT);
+
                     setCurrencyString(acc.getCurrency());
                     b.accountRowAccAmounts.setText(
                             acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null);
@@ -616,6 +620,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     b.ntrData.setVisibility(View.GONE);
                     b.ntrAccount.setVisibility(View.VISIBLE);
                     b.ntrPadding.setVisibility(View.GONE);
+
                     setEditable(true);
                 }
                 else {