]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fix a rare crash when submitting new transaction
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 18 Jun 2022 16:09:43 +0000 (19:09 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 18 Jun 2022 16:09:43 +0000 (19:09 +0300)
occurring when multiple accounts have no amount and the remaining
balance is zero

app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java

index d8f387b72696983f1fe9f87d0ae12e0a5b4d1be8..51bcd306b2da3e6fb50223ecc5f61e7f5dd357dc 100644 (file)
@@ -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<LedgerTransactionAccount> 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);
+                    }
                 }
             }
         }