]> git.ktnx.net Git - mobile-ledger.git/commitdiff
model: adjust items' positions when an item is moved
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 8 Feb 2021 06:05:17 +0000 (06:05 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 18 Feb 2021 07:36:40 +0000 (07:36 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplateDetailsViewModel.java

index 5d8ef18a084dddb696721a574aa7c25b92bcf3c1..8357678ab18167954f467d14e78406d3d57cac64 100644 (file)
@@ -24,6 +24,7 @@ import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
+import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.dao.TemplateAccountDAO;
 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
 import net.ktnx.mobileledger.db.DB;
@@ -265,9 +266,53 @@ public class TemplateDetailsViewModel extends ViewModel {
         return result;
     }
     public void moveItem(int sourcePos, int targetPos) {
-        ArrayList<TemplateDetailsItem> newList = new ArrayList<>(items.getValue());
-        TemplateDetailsItem item = newList.remove(sourcePos);
-        newList.add(targetPos, item);
+        final List<TemplateDetailsItem> newList = copyItems();
+
+        if (BuildConfig.DEBUG) {
+            Logger.debug("drag", "Before move:");
+            for (int i = 1; i < newList.size(); i++) {
+                final TemplateDetailsItem item = newList.get(i);
+                Logger.debug("drag",
+                        String.format(Locale.US, "  %d: id %d, pos %d", i, item.getId(),
+                                item.getPosition()));
+            }
+        }
+
+        {
+            TemplateDetailsItem item = newList.remove(sourcePos);
+            newList.add(targetPos, item);
+        }
+
+        // adjust affected items' positions
+        {
+            int startPos, endPos;
+            if (sourcePos < targetPos) {
+                // moved down
+                startPos = sourcePos;
+                endPos = targetPos;
+            }
+            else {
+                // moved up
+                startPos = targetPos;
+                endPos = sourcePos;
+            }
+
+            for (int i = startPos; i <= endPos; i++) {
+                newList.get(i)
+                       .setPosition(i);
+            }
+        }
+
+        if (BuildConfig.DEBUG) {
+            Logger.debug("drag", "After move:");
+            for (int i = 1; i < newList.size(); i++) {
+                final TemplateDetailsItem item = newList.get(i);
+                Logger.debug("drag",
+                        String.format(Locale.US, "  %d: id %d, pos %d", i, item.getId(),
+                                item.getPosition()));
+            }
+        }
+
         items.setValue(newList);
     }
     public void removeItem(int position) {