]> git.ktnx.net Git - mobile-ledger.git/commitdiff
smooth items replacement when old transaction is loaded and upon reset
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 11 Mar 2021 06:52:11 +0000 (08:52 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 11 Mar 2021 06:52:11 +0000 (08:52 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java

index 36b457ff4bf76bc1c0973a1db085196c2f3ee4e3..9539476d77f063e3e7239e95591831b7a64c4052 100644 (file)
@@ -92,6 +92,24 @@ public class NewTransactionModel extends ViewModel {
         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++) {
@@ -175,6 +193,7 @@ public class NewTransactionModel extends ViewModel {
         list.add(new TransactionAccount(""));
         list.add(new TransactionAccount(""));
         noteFocusChanged(0, FocusedElement.Description);
+        renumberItems();
         isSubmittable.setValue(false);
         setItemsWithoutSubmittableChecks(list);
     }
@@ -865,7 +884,7 @@ public class NewTransactionModel extends ViewModel {
 
     static abstract class Item {
         private static int idDispenser = 0;
-        protected final int id;
+        protected int id;
         private Item() {
             if (this instanceof TransactionHead)
                 id = 0;