]> git.ktnx.net Git - mobile-ledger.git/commitdiff
when a preferred account name filter yields no results, ignore it
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 25 Apr 2020 19:29:00 +0000 (22:29 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 25 Apr 2020 19:29:00 +0000 (22:29 +0300)
so that there is some historical transaction used as a template even
when there is none that includes an account matching the preferred
filter

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

index 67a90ae10663fc60cdf241a937280625d2a8fe7a..9b4022daf619340460b50cc1ba82e3d285dd1dc7 100644 (file)
@@ -212,74 +212,94 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         try (Cursor c = App.getDatabase()
                            .rawQuery(sql, params.toArray(new String[]{})))
         {
-            if (!c.moveToNext())
-                return;
-
-            String profileUUID = c.getString(0);
-            int transactionId = c.getInt(1);
-            LedgerTransaction tr;
-            MobileLedgerProfile profile = Data.getProfile(profileUUID);
-            if (profile == null)
-                throw new RuntimeException(String.format(
-                        "Unable to find profile %s, which is supposed to contain " +
-                        "transaction %d with description %s", profileUUID, transactionId,
-                        description));
-
-            tr = profile.loadTransaction(transactionId);
-            ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
-            NewTransactionModel.Item firstNegative = null;
-            NewTransactionModel.Item firstPositive = null;
-            int singleNegativeIndex = -1;
-            int singlePositiveIndex = -1;
-            int negativeCount = 0;
-            for (int i = 0; i < accounts.size(); i++) {
-                LedgerTransactionAccount acc = accounts.get(i);
-                NewTransactionModel.Item item;
-                if (model.getAccountCount() < i + 1) {
-                    model.addAccount(acc);
-                    notifyItemInserted(i + 1);
+            String profileUUID;
+            int transactionId;
+
+            if (!c.moveToNext()) {
+                sb = new StringBuilder("select t.profile, t.id from transactions t where t.description=?");
+                sb.append(" ORDER BY date desc LIMIT 1");
+
+                final String broaderSql = sb.toString();
+                debug("descr", broaderSql);
+                debug("descr", params.toString());
+                try (Cursor c2 = App.getDatabase().rawQuery(broaderSql, new String[]{description})) {
+                    if (!c2.moveToNext()) return;
+
+                    profileUUID = c2.getString(0);
+                    transactionId = c2.getInt(1);
                 }
-                item = model.getItem(i + 1);
+            }
+            else {
+                profileUUID = c.getString(0);
+                transactionId = c.getInt(1);
+            }
 
+            loadTransactionIntoModel(profileUUID, transactionId);
+        }
+    }
+    private void loadTransactionIntoModel(String profileUUID, int transactionId) {
+        LedgerTransaction tr;
+        MobileLedgerProfile profile = Data.getProfile(profileUUID);
+        if (profile == null)
+            throw new RuntimeException(String.format(
+                    "Unable to find profile %s, which is supposed to contain transaction %d",
+                    profileUUID, transactionId));
+
+        tr = profile.loadTransaction(transactionId);
+        ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
+        NewTransactionModel.Item firstNegative = null;
+        NewTransactionModel.Item firstPositive = null;
+        int singleNegativeIndex = -1;
+        int singlePositiveIndex = -1;
+        int negativeCount = 0;
+        for (int i = 0; i < accounts.size(); i++) {
+            LedgerTransactionAccount acc = accounts.get(i);
+            NewTransactionModel.Item item;
+            if (model.getAccountCount() < i + 1) {
+                model.addAccount(acc);
+                notifyItemInserted(i + 1);
+            }
+            item = model.getItem(i + 1);
+
+            item.getAccount()
+                .setAccountName(acc.getAccountName());
+            if (acc.isAmountSet()) {
                 item.getAccount()
-                    .setAccountName(acc.getAccountName());
-                if (acc.isAmountSet()) {
-                    item.getAccount()
-                        .setAmount(acc.getAmount());
-                    if (acc.getAmount() < 0) {
-                        if (firstNegative == null) {
-                            firstNegative = item;
-                            singleNegativeIndex = i;
-                        }
-                        else
-                            singleNegativeIndex = -1;
+                    .setAmount(acc.getAmount());
+                if (acc.getAmount() < 0) {
+                    if (firstNegative == null) {
+                        firstNegative = item;
+                        singleNegativeIndex = i;
                     }
-                    else {
-                        if (firstPositive == null) {
-                            firstPositive = item;
-                            singlePositiveIndex = i;
-                        }
-                        else
-                            singlePositiveIndex = -1;
+                    else
+                        singleNegativeIndex = -1;
+                }
+                else {
+                    if (firstPositive == null) {
+                        firstPositive = item;
+                        singlePositiveIndex = i;
                     }
+                    else
+                        singlePositiveIndex = -1;
                 }
-                else
-                    item.getAccount()
-                        .resetAmount();
-                notifyItemChanged(i + 1);
             }
+            else
+                item.getAccount()
+                    .resetAmount();
+            notifyItemChanged(i + 1);
+        }
 
-            if (singleNegativeIndex != -1) {
-                firstNegative.getAccount()
-                             .resetAmount();
-                model.moveItemLast(singleNegativeIndex);
-            }
-            else if (singlePositiveIndex != -1) {
-                firstPositive.getAccount()
-                             .resetAmount();
-                model.moveItemLast(singlePositiveIndex);
-            }
+        if (singleNegativeIndex != -1) {
+            firstNegative.getAccount()
+                         .resetAmount();
+            model.moveItemLast(singleNegativeIndex);
         }
+        else if (singlePositiveIndex != -1) {
+            firstPositive.getAccount()
+                         .resetAmount();
+            model.moveItemLast(singlePositiveIndex);
+        }
+
         checkTransactionSubmittable();
         model.setFocusedItem(1);
     }