From a0508ea0def0c4c6400626f26517d8e69631ff15 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Wed, 3 Mar 2021 19:33:20 +0200 Subject: [PATCH] NT: keep cursor position while setting account name text 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 --- .../NewTransactionItemHolder.java | 37 +++++++++++++------ .../NewTransactionItemsAdapter.java | 9 ++++- .../new_transaction/NewTransactionModel.java | 9 +++++ .../main/res/layout/new_transaction_row.xml | 3 ++ 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java index 4ca2c495..c6232d16 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java @@ -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(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java index 005abbb3..b1844c8e 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java @@ -132,11 +132,16 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 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 { diff --git a/app/src/main/res/layout/new_transaction_row.xml b/app/src/main/res/layout/new_transaction_row.xml index 795e38f9..d9c31083 100644 --- a/app/src/main/res/layout/new_transaction_row.xml +++ b/app/src/main/res/layout/new_transaction_row.xml @@ -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" @@ -143,6 +144,7 @@ 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" @@ -175,6 +177,7 @@ 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" > -- 2.39.2