X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionModel.java;h=d8f387b72696983f1fe9f87d0ae12e0a5b4d1be8;hp=426cb0417300b51e52116fae6d9dd9463ca0c474;hb=dd7e3b83b17546504605f93b3a462d1a1be3b382;hpb=dee1d58effc26bccf90c03d0f71428a436e6eb99 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 426cb041..d8f387b7 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 @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Damyan Ivanov. + * Copyright © 2022 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -18,6 +18,7 @@ package net.ktnx.mobileledger.ui.new_transaction; import android.annotation.SuppressLint; +import android.os.Build; import android.text.TextUtils; import androidx.annotation.NonNull; @@ -370,7 +371,7 @@ public class NewTransactionModel extends ViewModel { noteFocusChanged(fi.position - 1, fi.element); setItems(newList); } - void noteFocusChanged(int position, FocusedElement element) { + void noteFocusChanged(int position, @Nullable FocusedElement element) { FocusInfo present = focusInfo.getValue(); if (present == null || present.position != position || present.element != element) focusInfo.setValue(new FocusInfo(position, element)); @@ -443,14 +444,14 @@ public class NewTransactionModel extends ViewModel { LedgerTransaction tr = head.asLedgerTransaction(); tr.setComment(head.getComment()); - LedgerTransactionAccount emptyAmountAccount = null; - float emptyAmountAccountBalance = 0; + HashMap> emptyAmountAccounts = new HashMap<>(); + HashMap emptyAmountAccountBalance = new HashMap<>(); for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) .toTransactionAccount(); + String currency = item.getCurrency(); LedgerTransactionAccount acc = new LedgerTransactionAccount(item.getAccountName() - .trim(), - item.getCurrency()); + .trim(), currency); if (acc.getAccountName() .isEmpty()) continue; @@ -459,17 +460,52 @@ public class NewTransactionModel extends ViewModel { if (item.isAmountSet()) { acc.setAmount(item.getAmount()); - emptyAmountAccountBalance += item.getAmount(); + Float emptyCurrBalance = emptyAmountAccountBalance.get(currency); + if (emptyCurrBalance == null) { + emptyAmountAccountBalance.put(currency, item.getAmount()); + } + else { + emptyAmountAccountBalance.put(currency, emptyCurrBalance + item.getAmount()); + } } else { - emptyAmountAccount = acc; + List emptyCurrAccounts = + emptyAmountAccounts.get(currency); + if (emptyCurrAccounts == null) + emptyAmountAccounts.put(currency, emptyCurrAccounts = new ArrayList<>()); + emptyCurrAccounts.add(acc); } tr.addAccount(acc); } - if (emptyAmountAccount != null) - emptyAmountAccount.setAmount(-emptyAmountAccountBalance); + if (emptyAmountAccounts.size() > 0) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + emptyAmountAccounts.forEach((currency, accounts) -> { + if (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))); + }); + } + else { + for (String currency : emptyAmountAccounts.keySet()) { + List accounts = + Objects.requireNonNull(emptyAmountAccounts.get(currency)); + + if (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))); + } + } + } return tr; } @@ -496,6 +532,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(), @@ -525,6 +562,10 @@ public class NewTransactionModel extends ViewModel { } else item.resetAmount(); + + if (item.getCurrency() + .length() > 0) + hasCurrency = true; } if (BuildConfig.DEBUG) dumpItemList("Loaded previous transaction", newList); @@ -538,9 +579,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); }); } /** @@ -730,8 +774,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; } @@ -896,7 +940,7 @@ public class NewTransactionModel extends ViewModel { public static class FocusInfo { int position; FocusedElement element; - public FocusInfo(int position, FocusedElement element) { + public FocusInfo(int position, @Nullable FocusedElement element) { this.position = position; this.element = element; } @@ -1026,7 +1070,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)); @@ -1264,8 +1308,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() {