X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionItemHolder.java;h=d755782fb1f7864353c5855b12d4847142a9cfd6;hb=82cf0169d61ccf2feef4dc9d37f2afb054ee03f4;hp=4274a99b2620f4d825616a4c4604061a8330fef9;hpb=7c71fa95c5015c7cb3ba7d7c2ee3e41441e3d78a;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 4274a99b..d755782f 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 @@ -26,6 +26,7 @@ import android.view.Gravity; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.EditText; +import android.widget.SimpleCursorAdapter; import android.widget.TextView; import androidx.annotation.ColorInt; @@ -55,7 +56,7 @@ import java.util.Objects; class NewTransactionItemHolder extends RecyclerView.ViewHolder implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback { - private final String decimalDot; + private final String decimalDot = "."; private final MobileLedgerProfile mProfile; private final NewTransactionRowBinding b; private final NewTransactionItemsAdapter mAdapter; @@ -89,7 +90,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder mProfile = Data.getProfile(); - View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { + @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { final int id = v.getId(); if (hasFocus) { boolean wasSyncing = syncingData; @@ -118,6 +119,19 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder syncingData = wasSyncing; } } + else { // lost focus + if (id == R.id.account_row_acc_amounts) { + try { + String input = String.valueOf(b.accountRowAccAmounts.getText()); + input = input.replace(decimalSeparator, decimalDot); + b.accountRowAccAmounts.setText( + String.format("%4.2f", Float.parseFloat(input))); + } + catch (NumberFormatException ex) { + // ignored + } + } + } if (id == R.id.comment) { commentFocusChanged(b.comment, hasFocus); @@ -146,8 +160,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder DecimalFormatSymbols.getInstance(locale) .getMonetaryDecimalSeparator())); - decimalDot = "."; - final TextWatcher tw = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -164,10 +176,11 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder return; Logger.debug("textWatcher", "calling syncData()"); - syncData(); - Logger.debug("textWatcher", - "syncData() returned, checking if transaction is submittable"); - adapter.model.checkTransactionSubmittable(null); + if (syncData()) { + Logger.debug("textWatcher", + "syncData() returned, checking if transaction is submittable"); + adapter.model.checkTransactionSubmittable(null); + } Logger.debug("textWatcher", "done"); } }; @@ -263,16 +276,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder ignoreFocusChanges = false; } }); - adapter.model.getAccountCount() - .observe(activity, count -> { - final int adapterPosition = getAdapterPosition(); - final int layoutPosition = getLayoutPosition(); - - if (adapterPosition == count) - b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_DONE); - else - b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_NEXT); - }); Data.currencyGap.observe(activity, hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), @@ -485,16 +488,30 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder syncingData = true; + boolean significantChange = false; + try { if (item instanceof NewTransactionModel.TransactionHead) { NewTransactionModel.TransactionHead head = item.toTransactionHead(); head.setDate(String.valueOf(b.newTransactionDate.getText())); + + // transaction description is required + if (!significantChange && TextUtils.isEmpty(head.getDescription()) != + TextUtils.isEmpty(b.newTransactionDescription.getText())) + significantChange = true; + head.setDescription(String.valueOf(b.newTransactionDescription.getText())); head.setComment(String.valueOf(b.transactionComment.getText())); } else if (item instanceof NewTransactionModel.TransactionAccount) { NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); + + // having account name is important + if (!significantChange && TextUtils.isEmpty(acc.getAccountName()) != + TextUtils.isEmpty(b.accountRowAccName.getText())) + significantChange = true; + acc.setAccountName(String.valueOf(b.accountRowAccName.getText())); acc.setComment(String.valueOf(b.comment.getText())); @@ -503,36 +520,48 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder amount = amount.trim(); if (amount.isEmpty()) { + if (acc.isAmountSet()) + significantChange = true; acc.resetAmount(); acc.setAmountValid(true); } else { try { amount = amount.replace(decimalSeparator, decimalDot); - acc.setAmount(Float.parseFloat(amount)); + final float parsedAmount = Float.parseFloat(amount); + if (!acc.isAmountSet() || !Misc.equalFloats(parsedAmount, acc.getAmount())) + significantChange = true; + acc.setAmount(parsedAmount); acc.setAmountValid(true); } catch (NumberFormatException e) { Logger.debug("new-trans", String.format( "assuming amount is not set due to number format exception. " + "input was '%s'", amount)); + if (acc.isAmountValid()) + significantChange = true; acc.setAmountValid(false); } final String curr = String.valueOf(b.currency.getText()); + final String currValue; if (curr.equals(b.currency.getContext() .getResources() .getString(R.string.currency_symbol)) || curr.isEmpty()) - acc.setCurrency(null); + currValue = null; else - acc.setCurrency(curr); + currValue = curr; + + if (!significantChange && !TextUtils.equals(acc.getCurrency(), currValue)) + significantChange = true; + acc.setCurrency(currValue); } } else { throw new RuntimeException("Should not happen"); } - return true; + return significantChange; } catch (ParseException e) { throw new RuntimeException("Should not happen", e); @@ -563,7 +592,17 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder if (item instanceof NewTransactionModel.TransactionHead) { NewTransactionModel.TransactionHead head = item.toTransactionHead(); b.newTransactionDate.setText(head.getFormattedDate()); - b.newTransactionDescription.setText(head.getDescription()); + + // avoid triggering completion pop-up + SimpleCursorAdapter a = + (SimpleCursorAdapter) b.newTransactionDescription.getAdapter(); + try { + b.newTransactionDescription.setAdapter(null); + b.newTransactionDescription.setText(head.getDescription()); + } + finally { + b.newTransactionDescription.setAdapter(a); + } b.transactionComment.setText(head.getComment()); //styleComment(b.transactionComment, head.getComment()); @@ -576,7 +615,16 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder else if (item instanceof NewTransactionModel.TransactionAccount) { NewTransactionModel.TransactionAccount acc = item.toTransactionAccount(); - b.accountRowAccName.setText(acc.getAccountName()); + // 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 amountHint = acc.getAmountHint(); if (amountHint == null) { @@ -586,6 +634,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder b.accountRowAccAmounts.setHint(amountHint); } + b.accountRowAccAmounts.setImeOptions( + acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT); + setCurrencyString(acc.getCurrency()); b.accountRowAccAmounts.setText( acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null); @@ -596,6 +647,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder b.ntrData.setVisibility(View.GONE); b.ntrAccount.setVisibility(View.VISIBLE); b.ntrPadding.setVisibility(View.GONE); + setEditable(true); } else {