X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionModel.java;h=1fe215b291fd2d6e8a4cab24a5b86842633742b2;hb=f3a51462230bae2734179aa7eeef30901daaee30;hp=35bb3c82889c700efbc6e62e2bf91c7110157778;hpb=426a2bd8f495b5cf802e20248599dc5e08309aaf;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 35bb3c82..1fe215b2 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 @@ -74,13 +74,14 @@ public class NewTransactionModel extends ViewModel { private final AtomicInteger busyCounter = new AtomicInteger(0); private final MutableLiveData busyFlag = new InertMutableLiveData<>(false); private final Observer 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 = new MutableLiveData<>(); private boolean observingDataProfile; public NewTransactionModel() { - reset(); } public LiveData getShowCurrency() { return showCurrency; @@ -369,7 +370,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)); @@ -442,7 +443,7 @@ public class NewTransactionModel extends ViewModel { LedgerTransaction tr = head.asLedgerTransaction(); tr.setComment(head.getComment()); - LedgerTransactionAccount emptyAmountAccount = null; + List emptyAmountAccounts = new ArrayList<>(); float emptyAmountAccountBalance = 0; for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) @@ -461,14 +462,20 @@ public class NewTransactionModel extends ViewModel { emptyAmountAccountBalance += item.getAmount(); } else { - emptyAmountAccount = acc; + emptyAmountAccounts.add(acc); } tr.addAccount(acc); } - if (emptyAmountAccount != null) - emptyAmountAccount.setAmount(-emptyAmountAccountBalance); + if (emptyAmountAccounts.size() > 0) { + if (emptyAmountAccounts.size() > 1 && !Misc.isZero(emptyAmountAccountBalance)) + throw new RuntimeException(String.format(Locale.US, + "Should not happen. %d accounts with non zero amount to distribute (%5" + + ".3f)")); + for (LedgerTransactionAccount a : emptyAmountAccounts) + a.setAmount(-emptyAmountAccountBalance); + } return tr; } @@ -895,7 +902,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; }