X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionItemHolder.java;h=5757c72a249073360e5a2a20a4c3d8eb7fb7b67a;hb=a3e134a982b377a6b27ac409048d84bac6195814;hp=d755782fb1f7864353c5855b12d4847142a9cfd6;hpb=82cf0169d61ccf2feef4dc9d37f2afb054ee03f4;p=mobile-ledger.git 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..5757c72a 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 @@ -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"); }); @@ -215,67 +224,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(), @@ -290,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); } }); @@ -331,6 +280,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 { @@ -441,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; @@ -484,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; @@ -497,8 +507,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())); @@ -508,11 +518,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())); @@ -552,7 +566,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder else currValue = curr; - if (!significantChange && !TextUtils.equals(acc.getCurrency(), currValue)) + if (!significantChange && !Misc.equalStrings(acc.getCurrency(), currValue)) significantChange = true; acc.setCurrency(currValue); } @@ -609,21 +623,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 (!Misc.equalStrings(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(); @@ -646,13 +668,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder b.ntrData.setVisibility(View.GONE); b.ntrAccount.setVisibility(View.VISIBLE); - b.ntrPadding.setVisibility(View.GONE); setEditable(true); } else { throw new RuntimeException("Don't know how to handle " + item); } + + applyFocus(mAdapter.model.getFocusInfo() + .getValue()); } finally { syncingData = false;