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=1725f91a64ae0c58ffc7ca5652db67007a7d1e3f;hpb=80d43ee6b1f34f923dd78ea33847d4addf990fde;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 1725f91a..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 @@ -56,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; @@ -90,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; @@ -119,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); @@ -147,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) { @@ -165,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"); } }; @@ -264,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(), @@ -486,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())); @@ -504,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); @@ -606,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); @@ -616,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 {