X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionModel.java;h=51bcd306b2da3e6fb50223ecc5f61e7f5dd357dc;hb=307f2f25fe92a33665958bdf8b051ca846d7922f;hp=a87cb22fade82ed0d6c6d5edb5e6983425460879;hpb=bc1c46625d447c784d5be7cd86644a897d299e8f;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java index a87cb22f..51bcd306 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java @@ -482,25 +482,28 @@ public class NewTransactionModel extends ViewModel { if (emptyAmountAccounts.size() > 0) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { emptyAmountAccounts.forEach((currency, accounts) -> { - if (accounts.size() != 1) + final Float balance = emptyAmountAccountBalance.get(currency); + + if (balance != null && !Misc.isZero(balance) && accounts.size() != 1) { throw new RuntimeException(String.format(Locale.US, - "Should not happen: approved transaction has %d accounts for " + - "currency %s", accounts.size(), currency)); - accounts.get(0) - .setAmount(-emptyAmountAccountBalance.get(currency)); + "Should not happen: approved transaction has %d accounts " + + "without amounts for currency '%s'", accounts.size(), currency)); + } + accounts.forEach(acc -> acc.setAmount(balance == null ? 0 : -balance)); }); } else { for (String currency : emptyAmountAccounts.keySet()) { List accounts = Objects.requireNonNull(emptyAmountAccounts.get(currency)); - - if (accounts.size() != 1) + final Float balance = emptyAmountAccountBalance.get(currency); + if (balance != null && !Misc.isZero(balance) && accounts.size() != 1) throw new RuntimeException(String.format(Locale.US, "Should not happen: approved transaction has %d accounts for " + "currency %s", accounts.size(), currency)); - accounts.get(0) - .setAmount(-emptyAmountAccountBalance.get(currency)); + for (LedgerTransactionAccount acc : accounts) { + acc.setAmount(balance == null ? 0 : -balance); + } } } } @@ -530,6 +533,7 @@ public class NewTransactionModel extends ViewModel { int singleNegativeIndex = -1; int singlePositiveIndex = -1; int negativeCount = 0; + boolean hasCurrency = false; for (int i = 0; i < accounts.size(); i++) { LedgerTransactionAccount acc = accounts.get(i); TransactionAccount item = new TransactionAccount(acc.getAccountName(), @@ -559,6 +563,10 @@ public class NewTransactionModel extends ViewModel { } else item.resetAmount(); + + if (item.getCurrency() + .length() > 0) + hasCurrency = true; } if (BuildConfig.DEBUG) dumpItemList("Loaded previous transaction", newList); @@ -572,9 +580,12 @@ public class NewTransactionModel extends ViewModel { moveItemLast(newList, singlePositiveIndex); } + final boolean foundTransactionHasCurrency = hasCurrency; Misc.onMainThread(() -> { setItems(newList); noteFocusChanged(1, FocusedElement.Amount); + if (foundTransactionHasCurrency) + showCurrency.setValue(true); }); } /** @@ -764,8 +775,8 @@ public class NewTransactionModel extends ViewModel { !Misc.equalStrings(acc.getAmountHint(), hint)) { Logger.debug("submittable", - String.format("Setting amount hint of {%s} to %s [%s]", - acc.toString(), hint, balCurrency)); + String.format("Setting amount hint of {%s} to %s [%s]", acc, + hint, balCurrency)); acc.setAmountHint(hint); listChanged = true; } @@ -1060,7 +1071,7 @@ public class NewTransactionModel extends ViewModel { b.append(String.format(" '%s'", description)); if (date != null) - b.append(String.format("@%s", date.toString())); + b.append(String.format("@%s", date)); if (!TextUtils.isEmpty(comment)) b.append(String.format(" /%s/", comment)); @@ -1298,8 +1309,7 @@ public class NewTransactionModel extends ViewModel { equal = equal && Misc.equalStrings(currency, other.currency) && isLast == other.isLast; Logger.debug("new-trans", - String.format("Comparing {%s} and {%s}: %s", this.toString(), other.toString(), - equal)); + String.format("Comparing {%s} and {%s}: %s", this, other, equal)); return equal; } public int getAccountNameCursorPosition() {