@NonNull NewTransactionModel.Item newItem) {
// Logger.debug("new-trans",
-// String.format("comparing contents of {%s} and {%s}", oldItem.toString(),
+// String.format("comparing contents of {%s} and {%s}", oldItem
+// .toString(),
// newItem.toString()));
return oldItem.equalContents(newItem);
}
@NonNull RecyclerView.ViewHolder target) {
final int adapterPosition = target.getAdapterPosition();
- // first and last items are immovable
+ // first item is immovable
if (adapterPosition == 0)
return false;
- if (adapterPosition == adapter.getItemCount() - 1)
- return false;
return super.canDropOver(recyclerView, current, target);
}
int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
// the top (date and description) and the bottom (padding) items are always there
final int adapterPosition = viewHolder.getAdapterPosition();
- if ((adapterPosition > 0) && (adapterPosition < adapter.getItemCount() - 1)) {
+ if (adapterPosition > 0) {
flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
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<Boolean> showCurrency = new MutableLiveData<>(false);
private final MutableLiveData<Boolean> isSubmittable = new InertMutableLiveData<>(false);
private final MutableLiveData<Boolean> showComments = new MutableLiveData<>(true);
list.add(new TransactionHead(""));
list.add(new TransactionAccount(""));
list.add(new TransactionAccount(""));
- list.add(new BottomFiller());
items.setValue(list);
}
boolean accountsInInitialState() {
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())
newItems.add(accRow);
}
- newItems.add(new BottomFiller());
-
items.postValue(newItems);
});
}
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()
noteFocusChanged(1, FocusedElement.Description);
- newList.add(new BottomFiller());
-
setItems(newList);
}
/**
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();
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)) {
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;
}
}
// drop extra empty rows, not needed
for (String currName : emptyRowsForCurrency.currencies()) {
List<Item> 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;
}
// 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<Item> currItems = itemsForCurrency.getList(currName);
if (currItems.size() == 1) {
// 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;
}
@SuppressLint("DefaultLocale")
private void dumpItemList(@NotNull String msg, @NotNull List<Item> 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()));
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() {
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);
}
}
}
- 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;