]> git.ktnx.net Git - mobile-ledger.git/commitdiff
optimize calls to checkTransactionSubmittable()
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 1 Mar 2021 17:58:42 +0000 (19:58 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 1 Mar 2021 17:58:42 +0000 (19:58 +0200)
this an expensive function traversing over all rows, so a cheap
avoidance would be nice

app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/utils/Misc.java

index d93752e8da660ea53cef2fb9798b05c1b8de85fb..d755782fb1f7864353c5855b12d4847142a9cfd6 100644 (file)
@@ -176,10 +176,11 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     return;
 
                 Logger.debug("textWatcher", "calling syncData()");
-                syncData();
-                Logger.debug("textWatcher",
-                        "syncData() returned, checking if transaction is submittable");
-                adapter.model.checkTransactionSubmittable(null);
+                if (syncData()) {
+                    Logger.debug("textWatcher",
+                            "syncData() returned, checking if transaction is submittable");
+                    adapter.model.checkTransactionSubmittable(null);
+                }
                 Logger.debug("textWatcher", "done");
             }
         };
@@ -487,16 +488,30 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         syncingData = true;
 
+        boolean significantChange = false;
+
         try {
             if (item instanceof NewTransactionModel.TransactionHead) {
                 NewTransactionModel.TransactionHead head = item.toTransactionHead();
 
                 head.setDate(String.valueOf(b.newTransactionDate.getText()));
+
+                // transaction description is required
+                if (!significantChange && TextUtils.isEmpty(head.getDescription()) !=
+                                          TextUtils.isEmpty(b.newTransactionDescription.getText()))
+                    significantChange = true;
+
                 head.setDescription(String.valueOf(b.newTransactionDescription.getText()));
                 head.setComment(String.valueOf(b.transactionComment.getText()));
             }
             else if (item instanceof NewTransactionModel.TransactionAccount) {
                 NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
+
+                // having account name is important
+                if (!significantChange && TextUtils.isEmpty(acc.getAccountName()) !=
+                                          TextUtils.isEmpty(b.accountRowAccName.getText()))
+                    significantChange = true;
+
                 acc.setAccountName(String.valueOf(b.accountRowAccName.getText()));
 
                 acc.setComment(String.valueOf(b.comment.getText()));
@@ -505,36 +520,48 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 amount = amount.trim();
 
                 if (amount.isEmpty()) {
+                    if (acc.isAmountSet())
+                        significantChange = true;
                     acc.resetAmount();
                     acc.setAmountValid(true);
                 }
                 else {
                     try {
                         amount = amount.replace(decimalSeparator, decimalDot);
-                        acc.setAmount(Float.parseFloat(amount));
+                        final float parsedAmount = Float.parseFloat(amount);
+                        if (!acc.isAmountSet() || !Misc.equalFloats(parsedAmount, acc.getAmount()))
+                            significantChange = true;
+                        acc.setAmount(parsedAmount);
                         acc.setAmountValid(true);
                     }
                     catch (NumberFormatException e) {
                         Logger.debug("new-trans", String.format(
                                 "assuming amount is not set due to number format exception. " +
                                 "input was '%s'", amount));
+                        if (acc.isAmountValid())
+                            significantChange = true;
                         acc.setAmountValid(false);
                     }
                     final String curr = String.valueOf(b.currency.getText());
+                    final String currValue;
                     if (curr.equals(b.currency.getContext()
                                               .getResources()
                                               .getString(R.string.currency_symbol)) ||
                         curr.isEmpty())
-                        acc.setCurrency(null);
+                        currValue = null;
                     else
-                        acc.setCurrency(curr);
+                        currValue = curr;
+
+                    if (!significantChange && !TextUtils.equals(acc.getCurrency(), currValue))
+                        significantChange = true;
+                    acc.setCurrency(currValue);
                 }
             }
             else {
                 throw new RuntimeException("Should not happen");
             }
 
-            return true;
+            return significantChange;
         }
         catch (ParseException e) {
             throw new RuntimeException("Should not happen", e);
index 927a34d936b44326cfeed93aa9866ccd64ff770f..5b85f97097a0c35ebeb16c085586f5d749c09008 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2020 Damyan Ivanov.
+ * Copyright © 2021 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
@@ -32,6 +32,7 @@ public class Misc {
     public static boolean isZero(float f) {
         return (f < 0.005) && (f > -0.005);
     }
+    public static boolean equalFloats(float a, float b) { return isZero(a - b); }
     public static void showSoftKeyboard(Activity activity) {
         // make the keyboard appear
         Configuration cf = activity.getResources()
@@ -85,15 +86,19 @@ public class Misc {
     }
     @Contract(value = "null, null -> true; null, !null -> false; !null, null -> false", pure = true)
     public static boolean equalIntegers(Integer a, Integer b) {
-        if ( a == null && b == null) return true;
-        if (a == null || b == null) return false;
+        if (a == null && b == null)
+            return true;
+        if (a == null || b == null)
+            return false;
 
         return a.equals(b);
     }
     @Contract(value = "null, null -> true; null, !null -> false; !null, null -> false", pure = true)
     public static boolean equalLongs(Long a, Long b) {
-        if ( a == null && b == null) return true;
-        if (a == null || b == null) return false;
+        if (a == null && b == null)
+            return true;
+        if (a == null || b == null)
+            return false;
 
         return a.equals(b);
     }