checkTransactionSubmittable(newList);
setItemsWithoutSubmittableChecks(newList);
}
+ private void replaceItems(@NonNull List<Item> newList) {
+ renumberItems();
+
+ setItems(newList);
+ }
+ /**
+ * make old items replaceable in-place. makes the new values visually blend in
+ */
+ private void renumberItems() {
+ final List<Item> list = items.getValue();
+ if (list == null) {
+ return;
+ }
+
+ int id = 0;
+ for (Item item : list)
+ item.id = id++;
+ }
private void setItemsWithoutSubmittableChecks(@NonNull List<Item> list) {
final int cnt = list.size();
for (int i = 1; i < cnt - 1; i++) {
void reset() {
Logger.debug("new-trans", "Resetting model");
List<Item> list = new ArrayList<>();
+ Item.resetIdDispenser();
list.add(new TransactionHead(""));
list.add(new TransactionAccount(""));
list.add(new TransactionAccount(""));
noteFocusChanged(0, FocusedElement.Description);
+ renumberItems();
isSubmittable.setValue(false);
setItemsWithoutSubmittableChecks(list);
}
if (Misc.emptyIsNull(transactionComment) != null)
head.setComment(transactionComment);
+ Item.resetIdDispenser();
List<Item> newItems = new ArrayList<>();
newItems.add(head);
newItems.add(accRow);
}
- new Handler(Looper.getMainLooper()).post(() -> setItems(newItems));
+ new Handler(Looper.getMainLooper()).post(() -> replaceItems(newItems));
});
}
private int extractIntFromMatches(MatchResult m, Integer group, Integer literal) {
return tr;
}
- void loadTransactionIntoModel(String profileUUID, int transactionId) {
+ void loadTransactionIntoModel(long profileId, int transactionId) {
List<Item> newList = new ArrayList<>();
+ Item.resetIdDispenser();
LedgerTransaction tr;
- MobileLedgerProfile profile = Data.getProfile(profileUUID);
+ MobileLedgerProfile profile = Data.getProfile(profileId);
if (profile == null)
throw new RuntimeException(String.format(
"Unable to find profile %s, which is supposed to contain transaction %d",
- profileUUID, transactionId));
+ profileId, transactionId));
tr = profile.loadTransaction(transactionId);
TransactionHead head = new TransactionHead(tr.getDescription());
private static int idDispenser = 0;
protected int id;
private Item() {
- synchronized (Item.class) {
- id = ++idDispenser;
- }
+ if (this instanceof TransactionHead)
+ id = 0;
+ else
+ synchronized (Item.class) {
+ id = ++idDispenser;
+ }
+ }
+ public Item(int id) {
+ this.id = id;
}
public static Item from(Item origin) {
if (origin instanceof TransactionHead)
return new TransactionAccount((TransactionAccount) origin);
throw new RuntimeException("Don't know how to handle " + origin);
}
+ private static void resetIdDispenser() {
+ idDispenser = 0;
+ }
public int getId() {
return id;
}
this.description = description;
}
public TransactionHead(TransactionHead origin) {
- id = origin.id;
+ super(origin.id);
date = origin.date;
description = origin.description;
comment = origin.comment;
return ItemType.generalData;
}
public LedgerTransaction asLedgerTransaction() {
- return new LedgerTransaction(null, date, description, Data.getProfile());
+ return new LedgerTransaction(0, date, description, Data.getProfile());
}
public boolean equalContents(TransactionHead other) {
if (other == null)
private boolean isLast = false;
private int accountNameCursorPosition;
public TransactionAccount(TransactionAccount origin) {
- id = origin.id;
+ super(origin.id);
accountName = origin.accountName;
amount = origin.amount;
amountSet = origin.amountSet;