]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
UI and machinery for detecting hledger-web version
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemsAdapter.java
index 5caa38a5581ae19d3b4fb403d5125a90e741ce0d..0da28e8e07bd69823dae2824a9de298d51ee4885 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -20,6 +20,7 @@ package net.ktnx.mobileledger.ui.activity;
 import android.annotation.SuppressLint;
 import android.app.Activity;
 import android.database.Cursor;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
@@ -53,9 +54,9 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder>
         implements DescriptionSelectedCallback {
-    private NewTransactionModel model;
+    private final NewTransactionModel model;
     private MobileLedgerProfile mProfile;
-    private ItemTouchHelper touchHelper;
+    private final ItemTouchHelper touchHelper;
     private RecyclerView recyclerView;
     private int checkHoldCounter = 0;
     NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
@@ -186,7 +187,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         this.recyclerView = null;
     }
     public void descriptionSelected(String description) {
-        debug("descr selected", description);
+        debug("description selected", description);
         if (!accountListIsEmpty())
             return;
 
@@ -195,7 +196,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         ArrayList<String> params = new ArrayList<>();
         StringBuilder sb = new StringBuilder("select t.profile, t.id from transactions t");
 
-        if (!Misc.isEmptyOrNull(accFilter)) {
+        if (!TextUtils.isEmpty(accFilter)) {
             sb.append(" JOIN transaction_accounts ta")
               .append(" ON ta.profile = t.profile")
               .append(" AND ta.transaction_id = t.id");
@@ -204,16 +205,16 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         sb.append(" WHERE t.description=?");
         params.add(description);
 
-        if (!Misc.isEmptyOrNull(accFilter)) {
+        if (!TextUtils.isEmpty(accFilter)) {
             sb.append(" AND ta.account_name LIKE '%'||?||'%'");
             params.add(accFilter);
         }
 
-        sb.append(" ORDER BY t.date DESC LIMIT 1");
+        sb.append(" ORDER BY t.year desc, t.month desc, t.day desc LIMIT 1");
 
         final String sql = sb.toString();
-        debug("descr", sql);
-        debug("descr", params.toString());
+        debug("description", sql);
+        debug("description", params.toString());
 
         Activity activity = (Activity) recyclerView.getContext();
         // FIXME: handle exceptions?
@@ -235,23 +236,22 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             }
             @Override
             public void onNoRows() {
-                if (Misc.isEmptyOrNull(accFilter))
+                if (TextUtils.isEmpty(accFilter))
                     return;
 
-                debug("descr", "Trying transaction search without preferred account filter");
+                debug("description", "Trying transaction search without preferred account filter");
 
                 final String broaderSql =
                         "select t.profile, t.id from transactions t where t.description=?" +
-                        " ORDER BY date desc LIMIT 1";
+                        " ORDER BY year desc, month desc, day desc LIMIT 1";
                 params.remove(1);
-                debug("descr", broaderSql);
-                debug("descr", description);
+                debug("description", broaderSql);
+                debug("description", description);
 
-                activity.runOnUiThread(() -> {
-                    Snackbar.make(recyclerView, R.string.ignoring_preferred_account,
-                            Snackbar.LENGTH_LONG)
-                            .show();
-                });
+                activity.runOnUiThread(
+                        () -> Snackbar.make(recyclerView, R.string.ignoring_preferred_account,
+                                Snackbar.LENGTH_LONG)
+                                      .show());
 
                 MLDB.queryInBackground(broaderSql, new String[]{description},
                         new MLDB.CallbackHelper() {
@@ -284,7 +284,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     profileUUID, transactionId));
 
         tr = profile.loadTransaction(transactionId);
-        ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
+        List<LedgerTransactionAccount> accounts = tr.getAccounts();
         NewTransactionModel.Item firstNegative = null;
         NewTransactionModel.Item firstPositive = null;
         int singleNegativeIndex = -1;
@@ -473,7 +473,12 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     itemsWithAccountForCurrency.add(currName, item);
                 }
 
-                if (acc.isAmountSet()) {
+                if (!acc.isAmountValid()) {
+                    Logger.debug("submittable",
+                            String.format("Not submittable: row %d has an invalid amount", i + 1));
+                    submittable = false;
+                }
+                else if (acc.isAmountSet()) {
                     itemsWithAmountForCurrency.add(currName, item);
                     balance.add(currName, acc.getAmount());
                 }
@@ -647,7 +652,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     }
 
     private static class BalanceForCurrency {
-        private HashMap<String, Float> hashMap = new HashMap<>();
+        private final HashMap<String, Float> hashMap = new HashMap<>();
         float get(String currencyName) {
             Float f = hashMap.get(currencyName);
             if (f == null) {
@@ -668,7 +673,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     }
 
     private static class ItemsForCurrency {
-        private HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
+        private final HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
         @NonNull
         List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
             List<NewTransactionModel.Item> list = hashMap.get(currencyName);