]> git.ktnx.net Git - mobile-ledger.git/commitdiff
template details model: method for creating a copy of the data list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 8 Feb 2021 05:56:11 +0000 (05:56 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:35:40 +0000 (07:35 +0000)
this is a clone-like list, that shares no common objects with the original
needed so that any changes to it do not touch the original items and
differ discovers the changes

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

index 7f983807c59e3cd686f6d18fa2e7886b6b77dd9e..bf1dc35a1d1c904c34c2613d20c4bdeb53cd140d 100644 (file)
@@ -184,6 +184,21 @@ public class TemplateDetailsViewModel extends ViewModel {
             taDAO.finishSave(mPatternId);
         });
     }
+    private ArrayList<TemplateDetailsItem> copyItems() {
+        List<TemplateDetailsItem> oldList = items.getValue();
+        ArrayList<TemplateDetailsItem> result = new ArrayList<>(oldList.size());
+
+        for (TemplateDetailsItem item : oldList) {
+            if (item instanceof TemplateDetailsItem.Header)
+                result.add(new TemplateDetailsItem.Header(item.asHeaderItem()));
+            else if (item instanceof TemplateDetailsItem.AccountRow)
+                result.add(new TemplateDetailsItem.AccountRow(item.asAccountRowItem()));
+            else
+                throw new RuntimeException("Unexpected item " + item);
+        }
+
+        return result;
+    }
     public void moveItem(int sourcePos, int targetPos) {
         ArrayList<TemplateDetailsItem> newList = new ArrayList<>(items.getValue());
         TemplateDetailsItem item = newList.remove(sourcePos);