From 82cf0169d61ccf2feef4dc9d37f2afb054ee03f4 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 1 Mar 2021 19:58:42 +0200 Subject: [PATCH] optimize calls to checkTransactionSubmittable() this an expensive function traversing over all rows, so a cheap avoidance would be nice --- .../NewTransactionItemHolder.java | 43 +++++++++++++++---- .../net/ktnx/mobileledger/utils/Misc.java | 15 ++++--- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java index d93752e8..d755782f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java @@ -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); diff --git a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java index 927a34d9..5b85f970 100644 --- a/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java +++ b/app/src/main/java/net/ktnx/mobileledger/utils/Misc.java @@ -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); } -- 2.39.2