From: Damyan Ivanov Date: Mon, 1 Mar 2021 19:53:33 +0000 (+0200) Subject: fix storing/restoring focus on reconfiguration X-Git-Tag: v0.17.0~57 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=c03961a02b4d5dafb50b73ff49ded68b8734b0c4;p=mobile-ledger.git fix storing/restoring focus on reconfiguration --- 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 d755782f..5478da8a 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 @@ -215,67 +215,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder commentFocusChanged(b.comment, false); adapter.model.getFocusInfo() - .observe(activity, focusInfo -> { - if (ignoreFocusChanges) { - Logger.debug("new-trans", "Ignoring focus change"); - return; - } - ignoreFocusChanges = true; - try { - if (((focusInfo == null) || (focusInfo.element == null) || - focusInfo.position != getAdapterPosition()) || - itemView.hasFocus()) - return; - - NewTransactionModel.Item item = getItem(); - if (item instanceof NewTransactionModel.TransactionHead) { - NewTransactionModel.TransactionHead head = - item.toTransactionHead(); - // bad idea - double pop-up, and not really necessary. - // the user can tap the input to get the calendar - //if (!tvDate.hasFocus()) tvDate.requestFocus(); - switch (focusInfo.element) { - case TransactionComment: - b.transactionComment.setVisibility(View.VISIBLE); - b.transactionComment.requestFocus(); - break; - case Description: - boolean focused = - b.newTransactionDescription.requestFocus(); -// tvDescription.dismissDropDown(); - if (focused) - Misc.showSoftKeyboard( - (NewTransactionActivity) b.getRoot() - .getContext()); - break; - } - } - else if (item instanceof NewTransactionModel.TransactionAccount) { - NewTransactionModel.TransactionAccount acc = - item.toTransactionAccount(); - switch (focusInfo.element) { - case Amount: - b.accountRowAccAmounts.requestFocus(); - break; - case Comment: - b.comment.setVisibility(View.VISIBLE); - b.comment.requestFocus(); - break; - case Account: - boolean focused = b.accountRowAccName.requestFocus(); -// b.accountRowAccName.dismissDropDown(); - if (focused) - Misc.showSoftKeyboard( - (NewTransactionActivity) b.getRoot() - .getContext()); - break; - } - } - } - finally { - ignoreFocusChanges = false; - } - }); + .observe(activity, this::applyFocus); Data.currencyGap.observe(activity, hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), @@ -331,6 +271,61 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder b.transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE); }); } + private void applyFocus(NewTransactionModel.FocusInfo focusInfo) { + if (ignoreFocusChanges) { + Logger.debug("new-trans", "Ignoring focus change"); + return; + } + ignoreFocusChanges = true; + try { + if (((focusInfo == null) || (focusInfo.element == null) || + focusInfo.position != getAdapterPosition())) + return; + + NewTransactionModel.Item item = getItem(); + if (item instanceof NewTransactionModel.TransactionHead) { + NewTransactionModel.TransactionHead head = item.toTransactionHead(); + // bad idea - double pop-up, and not really necessary. + // the user can tap the input to get the calendar + //if (!tvDate.hasFocus()) tvDate.requestFocus(); + switch (focusInfo.element) { + case TransactionComment: + b.transactionComment.setVisibility(View.VISIBLE); + b.transactionComment.requestFocus(); + break; + case Description: + boolean focused = b.newTransactionDescription.requestFocus(); +// tvDescription.dismissDropDown(); + if (focused) + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); + break; + } + } + else if (item instanceof NewTransactionModel.TransactionAccount) { + NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); + switch (focusInfo.element) { + case Amount: + b.accountRowAccAmounts.requestFocus(); + break; + case Comment: + b.comment.setVisibility(View.VISIBLE); + b.comment.requestFocus(); + break; + case Account: + boolean focused = b.accountRowAccName.requestFocus(); +// b.accountRowAccName.dismissDropDown(); + if (focused) + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); + break; + } + } + } + finally { + ignoreFocusChanges = false; + } + } public void checkAmountValid(String s) { boolean valid = true; try { @@ -653,6 +648,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder else { throw new RuntimeException("Don't know how to handle " + item); } + + applyFocus(mAdapter.model.getFocusInfo() + .getValue()); } finally { syncingData = false;