]> git.ktnx.net Git - mobile-ledger.git/commitdiff
more heuristics when filling accounts from a previous transaction
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 25 Jan 2020 14:07:42 +0000 (16:07 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 25 Jan 2020 14:07:42 +0000 (16:07 +0200)
previously, when there was a single account with negative amount, that
amount was cleared from the row so that it is filled automatically when
the amounts of other accounts change

now this is also done for the single account with a positive amount if
there is one, and there are multiple accounts with negative amounts

app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index 4d18db6a05e2d82d10a4b59c405788b6ceed42f2..710b4d682e1cbace0703111b010042141c9e6aca 100644 (file)
@@ -228,8 +228,8 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
             NewTransactionModel.Item firstNegative = null;
             NewTransactionModel.Item firstPositive = null;
             ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
             NewTransactionModel.Item firstNegative = null;
             NewTransactionModel.Item firstPositive = null;
-            boolean singleNegative = false;
-            boolean singlePositive = false;
+            int singleNegativeIndex = -1;
+            int singlePositiveIndex = -1;
             int negativeCount = 0;
             for (int i = 0; i < accounts.size(); i++) {
                 LedgerTransactionAccount acc = accounts.get(i);
             int negativeCount = 0;
             for (int i = 0; i < accounts.size(); i++) {
                 LedgerTransactionAccount acc = accounts.get(i);
@@ -248,18 +248,18 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     if (acc.getAmount() < 0) {
                         if (firstNegative == null) {
                             firstNegative = item;
                     if (acc.getAmount() < 0) {
                         if (firstNegative == null) {
                             firstNegative = item;
-                            singleNegative = true;
+                            singleNegativeIndex = i;
                         }
                         else
                         }
                         else
-                            singleNegative = false;
+                            singleNegativeIndex = -1;
                     }
                     else {
                         if (firstPositive == null) {
                             firstPositive = item;
                     }
                     else {
                         if (firstPositive == null) {
                             firstPositive = item;
-                            singlePositive = true;
+                            singlePositiveIndex = i;
                         }
                         else
                         }
                         else
-                            singlePositive = false;
+                            singlePositiveIndex = -1;
                     }
                 }
                 else
                     }
                 }
                 else
@@ -268,13 +268,15 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 notifyItemChanged(i + 1);
             }
 
                 notifyItemChanged(i + 1);
             }
 
-            if (singleNegative) {
+            if (singleNegativeIndex != -1) {
                 firstNegative.getAccount()
                              .resetAmount();
                 firstNegative.getAccount()
                              .resetAmount();
+                model.moveItemLast(singleNegativeIndex);
             }
             }
-            else if (singlePositive) {
+            else if (singlePositiveIndex != -1) {
                 firstPositive.getAccount()
                              .resetAmount();
                 firstPositive.getAccount()
                              .resetAmount();
+                model.moveItemLast(singlePositiveIndex);
             }
         }
         model.checkTransactionSubmittable(this);
             }
         }
         model.checkTransactionSubmittable(this);
index 0198b8696a12de0c3cbe929baa3a64461d43c8da..5bc327a78329ffcb6c2ca3b0570d0de13d581b8f 100644 (file)
@@ -304,6 +304,19 @@ public class NewTransactionModel extends ViewModel {
         final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
         commentVisible.postValue(!commentVisible.getValue());
     }
         final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
         commentVisible.postValue(!commentVisible.getValue());
     }
+    public void moveItemLast(int index) {
+        /*   0
+             1   <-- index
+             2
+             3   <-- desired position
+         */
+        int itemCount = items.size();
+
+        if (index < itemCount - 1) {
+            Item acc = items.remove(index);
+            items.add(itemCount - 1, acc);
+        }
+    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================