]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
save a setText() call that would not change anyting
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionItemHolder.java
index 5478da8acc9e286ffcf8e3906d6b25bfde7f89d2..01c3bfdf7d07667025666542ba746a17cc5afa5f 100644 (file)
@@ -124,8 +124,9 @@ 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))
+                            b.accountRowAccAmounts.setText(newText);
                     }
                     catch (NumberFormatException ex) {
                         // ignored
@@ -230,12 +231,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 +437,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;
@@ -492,8 +493,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 head.setDate(String.valueOf(b.newTransactionDate.getText()));
 
                 // transaction description is required
-                if (!significantChange && TextUtils.isEmpty(head.getDescription()) !=
-                                          TextUtils.isEmpty(b.newTransactionDescription.getText()))
+                if (TextUtils.isEmpty(head.getDescription()) !=
+                    TextUtils.isEmpty(b.newTransactionDescription.getText()))
                     significantChange = true;
 
                 head.setDescription(String.valueOf(b.newTransactionDescription.getText()));
@@ -503,11 +504,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
 
                 // having account name is important
-                if (!significantChange && TextUtils.isEmpty(acc.getAccountName()) !=
-                                          TextUtils.isEmpty(b.accountRowAccName.getText()))
+                final Editable incomingAccountName = b.accountRowAccName.getText();
+                if (TextUtils.isEmpty(acc.getAccountName()) !=
+                    TextUtils.isEmpty(incomingAccountName))
                     significantChange = true;
 
-                acc.setAccountName(String.valueOf(b.accountRowAccName.getText()));
+                acc.setAccountName(String.valueOf(incomingAccountName));
+                final int accNameSelEnd = b.accountRowAccName.getSelectionEnd();
+                final int accNameSelStart = b.accountRowAccName.getSelectionStart();
+                acc.setAccountNameCursorPosition(accNameSelEnd);
 
                 acc.setComment(String.valueOf(b.comment.getText()));
 
@@ -604,21 +609,29 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
                     b.ntrData.setVisibility(View.VISIBLE);
                     b.ntrAccount.setVisibility(View.GONE);
-                    b.ntrPadding.setVisibility(View.GONE);
                     setEditable(true);
                 }
                 else if (item instanceof NewTransactionModel.TransactionAccount) {
                     NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
 
-                    // avoid triggering completion pop-up
-                    AccountAutocompleteAdapter a =
-                            (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
-                    try {
-                        b.accountRowAccName.setAdapter(null);
-                        b.accountRowAccName.setText(acc.getAccountName());
-                    }
-                    finally {
-                        b.accountRowAccName.setAdapter(a);
+                    final String incomingAccountName = acc.getAccountName();
+                    final String presentAccountName = String.valueOf(b.accountRowAccName.getText());
+                    if (!TextUtils.equals(incomingAccountName, presentAccountName)) {
+                        Logger.debug("bind",
+                                String.format("Setting account name from '%s' to '%s' (| @ %d)",
+                                        presentAccountName, incomingAccountName,
+                                        acc.getAccountNameCursorPosition()));
+                        // avoid triggering completion pop-up
+                        AccountAutocompleteAdapter a =
+                                (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
+                        try {
+                            b.accountRowAccName.setAdapter(null);
+                            b.accountRowAccName.setText(incomingAccountName);
+                            b.accountRowAccName.setSelection(acc.getAccountNameCursorPosition());
+                        }
+                        finally {
+                            b.accountRowAccName.setAdapter(a);
+                        }
                     }
 
                     final String amountHint = acc.getAmountHint();
@@ -641,7 +654,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
                     b.ntrData.setVisibility(View.GONE);
                     b.ntrAccount.setVisibility(View.VISIBLE);
-                    b.ntrPadding.setVisibility(View.GONE);
 
                     setEditable(true);
                 }