]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
avoid dataSync when updating entered amount format on losing focus
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionItemHolder.java
index 247f40eb748006c83477c59433661037e31919e6..6df1b5406cac95c88d9f25565f4374b4764cf2b6 100644 (file)
@@ -124,8 +124,17 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     try {
                         String input = String.valueOf(b.accountRowAccAmounts.getText());
                         input = input.replace(decimalSeparator, decimalDot);
-                        b.accountRowAccAmounts.setText(
-                                String.format("%4.2f", Float.parseFloat(input)));
+                        final String newText = String.format("%4.2f", Float.parseFloat(input));
+                        if (!newText.equals(input)) {
+                            boolean wasSyncingData = syncingData;
+                            syncingData = true;
+                            try {
+                                b.accountRowAccAmounts.setText(newText);
+                            }
+                            finally {
+                                syncingData = wasSyncingData;
+                            }
+                        }
                     }
                     catch (NumberFormatException ex) {
                         // ignored
@@ -206,8 +215,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         b.currencyButton.setOnClickListener(v -> {
             CurrencySelectorFragment cpf = new CurrencySelectorFragment();
             cpf.showPositionAndPadding();
-            cpf.setOnCurrencySelectedListener(
-                    c -> adapter.setItemCurrency(getAdapterPosition(), c.getName()));
+            cpf.setOnCurrencySelectedListener(c -> adapter.setItemCurrency(getAdapterPosition(),
+                    (c == null) ? null : c.getName()));
             cpf.show(activity.getSupportFragmentManager(), "currency-selector");
         });
 
@@ -230,12 +239,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                          if (showCurrency) {
                              b.currency.setVisibility(View.VISIBLE);
                              b.currencyButton.setVisibility(View.VISIBLE);
-                             b.currency.setText(mProfile.getDefaultCommodity());
+                             setCurrencyString(mProfile.getDefaultCommodity());
                          }
                          else {
                              b.currency.setVisibility(View.GONE);
                              b.currencyButton.setVisibility(View.GONE);
-                             b.currency.setText(null);
+                             setCurrencyString(null);
                          }
                      });
 
@@ -436,7 +445,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private void setCurrencyString(String currency) {
         @ColorInt int textColor = b.dummyText.getTextColors()
                                              .getDefaultColor();
-        if ((currency == null) || currency.isEmpty()) {
+        if (TextUtils.isEmpty(currency)) {
             b.currency.setText(R.string.currency_symbol);
             int alpha = (textColor >> 24) & 0xff;
             alpha = alpha * 3 / 4;
@@ -479,6 +488,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             return false;
         }
 
+        if (getAdapterPosition() < 0) {
+            // probably the row was swiped out
+            Logger.debug("new-trans", "Ignoring request to suncData(): adapter position negative");
+            return false;
+        }
+
         NewTransactionModel.Item item = getItem();
 
         syncingData = true;