X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Fnew_transaction%2FNewTransactionModel.java;h=ac0cf4b39a703ac1f57669705f37b6db3a4b80df;hb=b4d93e72e7b2643d59e41b516dc07092bfc0033d;hp=c84c26d20bf21b532ce6a4e982e688f8841b5a18;hpb=346b3c8e74a12b1822239481f807479fa81fc706;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 c84c26d2..ac0cf4b3 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 @@ -57,12 +57,13 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.MatchResult; -enum ItemType {generalData, transactionRow, bottomFiller} +enum ItemType {generalData, transactionRow} enum FocusedElement {Account, Comment, Amount, Description, TransactionComment} public class NewTransactionModel extends ViewModel { + private static final int MIN_ITEMS = 3; private final MutableLiveData showCurrency = new MutableLiveData<>(false); private final MutableLiveData isSubmittable = new InertMutableLiveData<>(false); private final MutableLiveData showComments = new MutableLiveData<>(true); @@ -155,7 +156,6 @@ public class NewTransactionModel extends ViewModel { list.add(new TransactionHead("")); list.add(new TransactionAccount("")); list.add(new TransactionAccount("")); - list.add(new BottomFiller()); items.setValue(list); } boolean accountsInInitialState() { @@ -225,7 +225,7 @@ public class NewTransactionModel extends ViewModel { newItems.add(head); - for (int i = 1; i < present.size() - 1; i++) { + for (int i = 1; i < present.size(); i++) { final TransactionAccount row = present.get(i) .toTransactionAccount(); if (!row.isEmpty()) @@ -260,8 +260,6 @@ public class NewTransactionModel extends ViewModel { newItems.add(accRow); } - newItems.add(new BottomFiller()); - items.postValue(newItems); }); } @@ -373,7 +371,7 @@ public class NewTransactionModel extends ViewModel { tr.setComment(head.getComment()); LedgerTransactionAccount emptyAmountAccount = null; float emptyAmountAccountBalance = 0; - for (int i = 1; i < list.size() - 1; i++) { + for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) .toTransactionAccount(); LedgerTransactionAccount acc = new LedgerTransactionAccount(item.getAccountName() @@ -453,6 +451,8 @@ public class NewTransactionModel extends ViewModel { else item.resetAmount(); } + if (BuildConfig.DEBUG) + dumpItemList("Loaded previous transaction", newList); if (singleNegativeIndex != -1) { firstNegative.resetAmount(); @@ -463,11 +463,9 @@ public class NewTransactionModel extends ViewModel { moveItemLast(newList, singlePositiveIndex); } - noteFocusChanged(1, FocusedElement.Description); - - newList.add(new BottomFiller()); - setItems(newList); + + noteFocusChanged(1, FocusedElement.Amount); } /** * A transaction is submittable if: @@ -522,7 +520,7 @@ public class NewTransactionModel extends ViewModel { submittable = false; } - for (int i = 1; i < list.size() - 1; i++) { + for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) .toTransactionAccount(); @@ -589,7 +587,7 @@ public class NewTransactionModel extends ViewModel { float currencyBalance = balance.get(balCurrency); if (Misc.isZero(currencyBalance)) { // remove hints from all amount inputs in that currency - for (int i = 1; i < list.size() - 1; i++) { + for (int i = 1; i < list.size(); i++) { TransactionAccount acc = list.get(i) .toTransactionAccount(); if (Misc.equalStrings(acc.getCurrency(), balCurrency)) { @@ -729,7 +727,7 @@ public class NewTransactionModel extends ViewModel { Logger.debug("submittable", String.format("Adding new item with %s for currency %s", newAcc.getAmountHint(), balCurrency)); - list.add(list.size() - 1, newAcc); + list.add(newAcc); listChanged = true; } } @@ -737,18 +735,19 @@ public class NewTransactionModel extends ViewModel { // drop extra empty rows, not needed for (String currName : emptyRowsForCurrency.currencies()) { List emptyItems = emptyRowsForCurrency.getList(currName); - while ((list.size() > 4) && (emptyItems.size() > 1)) { + while ((list.size() > MIN_ITEMS) && (emptyItems.size() > 1)) { if (workingWithLiveList && !liveListCopied) { list = copyList(list); liveListCopied = true; } - Item item = emptyItems.remove(1); - list.remove(item); + // the list is a copy, so the empty item is no longer present + Item itemToRemove = emptyItems.remove(1); + removeItemById(list, itemToRemove.id); listChanged = true; } // unused currency, remove last item (which is also an empty one) - if ((list.size() > 4) && (emptyItems.size() == 1)) { + if ((list.size() > MIN_ITEMS) && (emptyItems.size() == 1)) { List currItems = itemsForCurrency.getList(currName); if (currItems.size() == 1) { @@ -756,8 +755,8 @@ public class NewTransactionModel extends ViewModel { list = copyList(list); liveListCopied = true; } - Item item = emptyItems.get(0); - list.remove(item); + // the list is a copy, so the empty item is no longer present + removeItemById(list, emptyItems.get(0).id); listChanged = true; } } @@ -765,12 +764,12 @@ public class NewTransactionModel extends ViewModel { // 6) at least two rows need to be present in the ledger // (the list also contains header and trailer) - while (list.size() < 4) { + while (list.size() < MIN_ITEMS) { if (workingWithLiveList && !liveListCopied) { list = copyList(list); liveListCopied = true; } - list.add(list.size() - 1, new TransactionAccount("")); + list.add(new TransactionAccount("")); listChanged = true; } @@ -795,10 +794,23 @@ public class NewTransactionModel extends ViewModel { setItemsWithoutSubmittableChecks(list); } } + private void removeItemById(@NotNull List list, int id) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + list.removeIf(item -> item.id == id); + } + else { + for (Item item : list) { + if (item.id == id) { + list.remove(item); + break; + } + } + } + } @SuppressLint("DefaultLocale") private void dumpItemList(@NotNull String msg, @NotNull List list) { Logger.debug("submittable", "== Dump of all items " + msg); - for (int i = 1; i < list.size() - 1; i++) { + for (int i = 1; i < list.size(); i++) { TransactionAccount item = list.get(i) .toTransactionAccount(); Logger.debug("submittable", String.format("%d:%s", i, item.toString())); @@ -859,8 +871,6 @@ public class NewTransactionModel extends ViewModel { return new TransactionHead((TransactionHead) origin); if (origin instanceof TransactionAccount) return new TransactionAccount((TransactionAccount) origin); - if (origin instanceof BottomFiller) - return new BottomFiller((BottomFiller) origin); throw new RuntimeException("Don't know how to handle " + origin); } public int getId() { @@ -894,8 +904,6 @@ public class NewTransactionModel extends ViewModel { return ((TransactionHead) item).equalContents((TransactionHead) this); if (this instanceof TransactionAccount) return ((TransactionAccount) item).equalContents((TransactionAccount) this); - if (this instanceof BottomFiller) - return true; throw new RuntimeException("Don't know how to handle " + this); } @@ -1001,26 +1009,6 @@ public class NewTransactionModel extends ViewModel { } } - public static class BottomFiller extends Item { - public BottomFiller(BottomFiller origin) { - id = origin.id; - // nothing to do - } - public BottomFiller() { - super(); - } - @Override - public ItemType getType() { - return ItemType.bottomFiller; - } - @SuppressLint("DefaultLocale") - @NonNull - @Override - public String toString() { - return String.format("id:%d «bottom filler»", id); - } - } - public static class TransactionAccount extends Item { private String accountName; private String amountHint;