From 307f2f25fe92a33665958bdf8b051ca846d7922f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 18 Jun 2022 19:09:43 +0300 Subject: [PATCH] fix a rare crash when submitting new transaction occurring when multiple accounts have no amount and the remaining balance is zero --- .../new_transaction/NewTransactionModel.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) 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 d8f387b7..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,27 +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(-Objects.requireNonNull( - 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(-Objects.requireNonNull( - emptyAmountAccountBalance.get(currency))); + for (LedgerTransactionAccount acc : accounts) { + acc.setAmount(balance == null ? 0 : -balance); + } } } } -- 2.39.5