]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
NewTransaction Item.setEditable: allow to be called for head row too
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionModel.java
index bbfedcbc1aae2a870a10934f91023d67bd7c60ec..443c21f911ff310681c683a60fdd744e77a95c5f 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.ktnx.mobileledger.ui.activity;
 
+import android.annotation.SuppressLint;
+
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
@@ -43,7 +45,6 @@ public class NewTransactionModel extends ViewModel {
     static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
     static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
     static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
-    private static final String ZERO_AMOUNT_HINT = "0.00";
     private final Item header = new Item(this, null, "");
     private final Item trailer = new Item(this);
     private final ArrayList<Item> items = new ArrayList<>();
@@ -129,6 +130,7 @@ public class NewTransactionModel extends ViewModel {
     // 3a) there must be exactly one empty amount
     // 4) empty accounts with empty amounts are ignored
     // 5) a row with an empty account name or empty amount is guaranteed to exist
+    @SuppressLint("DefaultLocale")
     public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
         int accounts = 0;
         int accounts_with_values = 0;
@@ -191,18 +193,17 @@ public class NewTransactionModel extends ViewModel {
 
                 if (single_empty_amount) {
                     if (item.equals(empty_amount)) {
-                        empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
-                                Misc.isZero(running_total) ? 0f : -running_total));
+                        empty_amount.setAmountHint(Misc.isZero(running_total) ? null
+                                                                              : String.format(
+                                                                                      "%1.2f",
+                                                                                      -running_total));
                         continue;
                     }
                 }
                 else {
                     // no single empty account and this account's amount is not set
                     // => hint should be '0.00'
-                    String hint = item.getAmountHint();
-                    if ((hint == null) || !hint.equals(ZERO_AMOUNT_HINT)) {
-                        item.setAmountHint(ZERO_AMOUNT_HINT);
-                    }
+                    item.setAmountHint(null);
                 }
 
             }
@@ -251,7 +252,7 @@ public class NewTransactionModel extends ViewModel {
         private MutableLiveData<Date> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();
         private LedgerTransactionAccount account;
-        private MutableLiveData<String> amountHint = new MutableLiveData<>();
+        private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
         private NewTransactionModel model;
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
         public Item(NewTransactionModel model) {
@@ -275,20 +276,34 @@ public class NewTransactionModel extends ViewModel {
         public NewTransactionModel getModel() {
             return model;
         }
-        public boolean isEditable() {
-            ensureType(ItemType.transactionRow);
-            return this.editable.getValue();
-        }
         public void setEditable(boolean editable) {
-            ensureType(ItemType.transactionRow);
+            ensureType(ItemType.generalData, ItemType.transactionRow);
             this.editable.setValue(editable);
         }
+        private void ensureType(ItemType type1, ItemType type2) {
+            if ((type != type1) && (type != type2)) {
+                throw new RuntimeException(
+                        String.format("Actual type (%s) differs from wanted (%s or %s)", type,
+                                type1, type2));
+            }
+        }
         public String getAmountHint() {
             ensureType(ItemType.transactionRow);
             return amountHint.getValue();
         }
         public void setAmountHint(String amountHint) {
             ensureType(ItemType.transactionRow);
+
+            // avoid unnecessary triggers
+            if (amountHint == null) {
+                if (this.amountHint.getValue() == null)
+                    return;
+            }
+            else {
+                if (amountHint.equals(this.amountHint.getValue()))
+                    return;
+            }
+
             this.amountHint.setValue(amountHint);
         }
         public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,