]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java
make transaction header row always have Id of 0
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionModel.java
index 23a4ef73cafcd6eddefcfa9ff59b1e832e5187ec..36b457ff4bf76bc1c0973a1db085196c2f3ee4e3 100644 (file)
@@ -170,6 +170,7 @@ public class NewTransactionModel extends ViewModel {
     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(""));
@@ -442,6 +443,7 @@ public class NewTransactionModel extends ViewModel {
     }
     void loadTransactionIntoModel(String profileUUID, int transactionId) {
         List<Item> newList = new ArrayList<>();
+        Item.resetIdDispenser();
         LedgerTransaction tr;
         MobileLedgerProfile profile = Data.getProfile(profileUUID);
         if (profile == null)
@@ -863,11 +865,17 @@ public class NewTransactionModel extends ViewModel {
 
     static abstract class Item {
         private static int idDispenser = 0;
-        protected int id;
+        protected final 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)
@@ -876,6 +884,9 @@ public class NewTransactionModel extends ViewModel {
                 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;
         }
@@ -924,7 +935,7 @@ public class NewTransactionModel extends ViewModel {
             this.description = description;
         }
         public TransactionHead(TransactionHead origin) {
-            id = origin.id;
+            super(origin.id);
             date = origin.date;
             description = origin.description;
             comment = origin.comment;
@@ -1025,7 +1036,7 @@ public class NewTransactionModel extends ViewModel {
         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;