/*
- * 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
package net.ktnx.mobileledger.ui.new_transaction;
import android.annotation.SuppressLint;
+import android.os.Build;
import android.text.TextUtils;
import androidx.annotation.NonNull;
private final AtomicInteger busyCounter = new AtomicInteger(0);
private final MutableLiveData<Boolean> busyFlag = new InertMutableLiveData<>(false);
private final Observer<Profile> profileObserver = profile -> {
- showCurrency.postValue(profile.getShowCommodityByDefault());
- showComments.postValue(profile.getShowCommentsByDefault());
+ if (profile != null) {
+ showCurrency.postValue(profile.getShowCommodityByDefault());
+ showComments.postValue(profile.getShowCommentsByDefault());
+ }
};
private final MutableLiveData<FocusInfo> focusInfo = new MutableLiveData<>();
private boolean observingDataProfile;
public NewTransactionModel() {
- reset();
}
public LiveData<Boolean> getShowCurrency() {
return showCurrency;
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));
LedgerTransaction tr = head.asLedgerTransaction();
tr.setComment(head.getComment());
- LedgerTransactionAccount emptyAmountAccount = null;
- float emptyAmountAccountBalance = 0;
+ HashMap<String, List<LedgerTransactionAccount>> emptyAmountAccounts = new HashMap<>();
+ HashMap<String, Float> 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;
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<LedgerTransactionAccount> 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) -> {
+ 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 " +
+ "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));
+ 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));
+ for (LedgerTransactionAccount acc : accounts) {
+ acc.setAmount(balance == null ? 0 : -balance);
+ }
+ }
+ }
+ }
return tr;
}
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(),
}
else
item.resetAmount();
+
+ if (item.getCurrency()
+ .length() > 0)
+ hasCurrency = true;
}
if (BuildConfig.DEBUG)
dumpItemList("Loaded previous transaction", newList);
moveItemLast(newList, singlePositiveIndex);
}
+ final boolean foundTransactionHasCurrency = hasCurrency;
Misc.onMainThread(() -> {
setItems(newList);
noteFocusChanged(1, FocusedElement.Amount);
+ if (foundTransactionHasCurrency)
+ showCurrency.setValue(true);
});
}
/**
!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;
}
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;
}
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));
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() {