]> git.ktnx.net Git - mobile-ledger.git/commitdiff
NT: keep cursor position while setting account name text
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 3 Mar 2021 17:33:20 +0000 (19:33 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 3 Mar 2021 17:33:20 +0000 (19:33 +0200)
some times the ViewHolder is replaced and the already entred text needs
to be restored. this resets the cursor position to 0, which is rather
uncomfortable while entering text

app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java
app/src/main/res/layout/new_transaction_row.xml

index 4ca2c495b2d49626e035bfa39a3dd7e840d93b49..c6232d167f7e303af4d93be2222354918727cc36 100644 (file)
@@ -503,11 +503,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
 
                 // having account name is important
+                final Editable incomingAccountName = b.accountRowAccName.getText();
                 if (TextUtils.isEmpty(acc.getAccountName()) !=
-                    TextUtils.isEmpty(b.accountRowAccName.getText()))
+                    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()));
 
@@ -609,15 +613,26 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 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);
+                            if (b.accountRowAccName.hasFocus())
+                                b.accountRowAccName.setSelection(
+                                        acc.getAccountNameCursorPosition());
+                        }
+                        finally {
+                            b.accountRowAccName.setAdapter(a);
+                        }
                     }
 
                     final String amountHint = acc.getAmountHint();
index 005abbb3e45d6b01ad07bb7891f03f799be1286f..b1844c8ec182eda8b1a07f8089d4fd71a5fed751 100644 (file)
@@ -132,11 +132,16 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 NewTransactionRowBinding.inflate(LayoutInflater.from(parent.getContext()), parent,
                         false);
 
-        return new NewTransactionItemHolder(b, this);
+        final NewTransactionItemHolder newHolder = new NewTransactionItemHolder(b, this);
+        Logger.debug("new-trans",
+                "Creating new ViewHolder " + Integer.toHexString(newHolder.hashCode()));
+        return newHolder;
     }
     @Override
     public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
-        Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
+        Logger.debug("bind",
+                String.format(Locale.US, "Binding item at position %d, holder %s", position,
+                        Integer.toHexString(holder.hashCode())));
         NewTransactionModel.Item item = Objects.requireNonNull(differ.getCurrentList()
                                                                      .get(position));
         holder.bind(item);
index d0b99f3d558c4106238b5747dd1e94fecc826995..d1db37b2c5f242ec8bb424537f94e75e21ee9276 100644 (file)
@@ -170,6 +170,7 @@ public class NewTransactionModel extends ViewModel {
         return this.isSubmittable;
     }
     void reset() {
+        Logger.debug("new-trans", "Resetting model");
         List<Item> list = new ArrayList<>();
         list.add(new TransactionHead(""));
         list.add(new TransactionAccount(""));
@@ -1039,6 +1040,7 @@ public class NewTransactionModel extends ViewModel {
         private FocusedElement focusedElement = FocusedElement.Account;
         private boolean amountHintIsSet = true;
         private boolean isLast = false;
+        private int accountNameCursorPosition;
         public TransactionAccount(TransactionAccount origin) {
             id = origin.id;
             accountName = origin.accountName;
@@ -1051,6 +1053,7 @@ public class NewTransactionModel extends ViewModel {
             amountValid = origin.amountValid;
             focusedElement = origin.focusedElement;
             isLast = origin.isLast;
+            accountNameCursorPosition = origin.accountNameCursorPosition;
         }
         public TransactionAccount(LedgerTransactionAccount account) {
             super();
@@ -1177,6 +1180,12 @@ public class NewTransactionModel extends ViewModel {
                             equal));
             return equal;
         }
+        public int getAccountNameCursorPosition() {
+            return accountNameCursorPosition;
+        }
+        public void setAccountNameCursorPosition(int position) {
+            this.accountNameCursorPosition = position;
+        }
     }
 
     private static class BalanceForCurrency {
index 795e38f9a14712731c65b16445877355164ea4e5..d9c31083350874c23ad58a81d3eedee286e8297b 100644 (file)
@@ -87,6 +87,7 @@
             android:id="@+id/transaction_comment_layout"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
+            android:animateLayoutChanges="true"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toBottomOf="@id/new_transaction_description"
             android:id="@+id/comment_layout"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
+            android:animateLayoutChanges="true"
             app:layout_constraintEnd_toStartOf="@id/amount_layout"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toBottomOf="@id/account_row_acc_name"
             android:id="@+id/amount_layout"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
+            android:animateLayoutChanges="true"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintTop_toBottomOf="@id/account_row_acc_name"
             >