]> git.ktnx.net Git - mobile-ledger.git/commitdiff
model: ensure exactly one empty account row
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 8 Feb 2021 05:58:50 +0000 (05:58 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:35:57 +0000 (07:35 +0000)
also properly apply the check after removing an item. the intermediate
items.setValue(newList) is complete unnecessary

also make the check when receiving items from the database

app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java

index bf1dc35a1d1c904c34c2613d20c4bdeb53cd140d..9f87aa796cdc8605e14c400b004f006a8e2bb40f 100644 (file)
@@ -56,31 +56,94 @@ public class TemplateDetailsViewModel extends ViewModel {
     }
 
     public void resetItems() {
-        checkItemConsistency(new ArrayList<>());
+        applyList(new ArrayList<>());
     }
-    public void checkItemConsistency(List<TemplateDetailsItem> list) {
-        if (list == null)
-            list = new ArrayList<>(items.getValue());
+    public void applyList(List<TemplateDetailsItem> srcList) {
+        applyList(srcList, false);
+    }
+    public void applyList(List<TemplateDetailsItem> srcList, boolean async) {
+        boolean changes;
+        if (srcList == null) {
+            srcList = new ArrayList<>(items.getValue());
+            changes = false;
+        }
+        else
+            changes = true;
+
+        srcList = Collections.unmodifiableList(srcList);
+
+        {
+            Logger.debug("tmpl", "Considering old list");
+            for (TemplateDetailsItem item : srcList)
+                Logger.debug("tmpl", String.format(Locale.US, " id %d pos %d", item.getId(),
+                        item.getPosition()));
+        }
+
+        ArrayList<TemplateDetailsItem> newList = new ArrayList<>();
 
-        boolean changes = false;
-        if (list.size() < 1) {
+        boolean hasEmptyItem = false;
+
+        if (srcList.size() < 1) {
             final TemplateDetailsItem.Header header = TemplateDetailsItem.createHeader();
             header.setName(mDefaultPatternName);
             header.setId(0);
-            list.add(header);
+            newList.add(header);
+            changes = true;
+        }
+        else {
+            newList.add(srcList.get(0));
+        }
+
+        for (int i = 1; i < srcList.size(); i++) {
+            final TemplateDetailsItem.AccountRow accRow = srcList.get(i)
+                                                                 .asAccountRowItem();
+            if (accRow.isEmpty()) {
+                // it is normal to have two empty rows if they are at the
+                // top (position 1 and 2)
+                if (!hasEmptyItem || i < 3) {
+                    accRow.setPosition(newList.size());
+                    newList.add(accRow);
+                }
+                else
+                    changes = true; // row skipped
+
+                hasEmptyItem = true;
+            }
+            else {
+                accRow.setPosition(newList.size());
+                newList.add(accRow);
+            }
+        }
+
+        while (newList.size() < 3) {
+            final TemplateDetailsItem.AccountRow accountRow =
+                    TemplateDetailsItem.createAccountRow();
+            accountRow.setId(genItemId());
+            accountRow.setPosition(newList.size());
+            newList.add(accountRow);
             changes = true;
+            hasEmptyItem = true;
         }
 
-        while (list.size() < 3) {
+        if (!hasEmptyItem) {
             final TemplateDetailsItem.AccountRow accountRow =
                     TemplateDetailsItem.createAccountRow();
             accountRow.setId(genItemId());
-            list.add(accountRow);
+            accountRow.setPosition(newList.size());
+            newList.add(accountRow);
             changes = true;
         }
 
-        if (changes)
-            items.setValue(list);
+        if (changes) {
+            Logger.debug("tmpl", "Changes detected, applying new list");
+
+            if (async)
+                items.postValue(newList);
+            else
+                items.setValue(newList);
+        }
+        else
+            Logger.debug("tmpl", "No changes, ignoring new list");
     }
     public int genItemId() {
         return syntheticItemId.decrementAndGet();
@@ -117,7 +180,7 @@ public class TemplateDetailsViewModel extends ViewModel {
                 for (TemplateDetailsItem i : l) {
                     Logger.debug("patterns-db", "Loaded pattern item " + i);
                 }
-                items.postValue(l);
+                applyList(l, true);
                 itemsLoaded = true;
 
                 dbList.removeObserver(this);
@@ -208,7 +271,6 @@ public class TemplateDetailsViewModel extends ViewModel {
     public void removeItem(int position) {
         ArrayList<TemplateDetailsItem> newList = new ArrayList<>(items.getValue());
         newList.remove(position);
-        checkItemConsistency(newList);
-        items.setValue(newList);
+        applyList(newList);
     }
 }
\ No newline at end of file