X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionModel.java;h=443c21f911ff310681c683a60fdd744e77a95c5f;hp=f6ecb443f49fe775413d50b9df536fe0dedd352f;hb=f50575e1d86035e9328cf0296e1dc09fda70e442;hpb=d4e592c5aed998f3b3e5afcf2150fb051aaf3e6f diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index f6ecb443..443c21f9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -17,9 +17,12 @@ package net.ktnx.mobileledger.ui.activity; +import android.annotation.SuppressLint; + import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModel; import net.ktnx.mobileledger.model.LedgerTransactionAccount; @@ -96,10 +99,12 @@ public class NewTransactionModel extends ViewModel { boolean accountsInInitialState() { for (Item item : items) { LedgerTransactionAccount acc = item.getAccount(); - if (acc.isAmountSet()) return false; + if (acc.isAmountSet()) + return false; if (!acc.getAccountName() .trim() - .isEmpty()) return false; + .isEmpty()) + return false; } return true; @@ -112,8 +117,11 @@ public class NewTransactionModel extends ViewModel { if (index == 0) { return header; } - else if (index <= items.size()) return items.get(index - 1); - else return trailer; + + if (index <= items.size()) + return items.get(index - 1); + + return trailer; } // rules: // 1) at least two account names @@ -122,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; @@ -142,27 +151,31 @@ public class NewTransactionModel extends ViewModel { LedgerTransactionAccount acc = item.getAccount(); String acc_name = acc.getAccountName() .trim(); - if (!acc_name.isEmpty()) { + if (acc_name.isEmpty()) { + empty_rows++; + } + else { accounts++; if (acc.isAmountSet()) { accounts_with_values++; } } - else empty_rows++; - if (!acc.isAmountSet()) { + if (acc.isAmountSet()) { + amounts++; + if (!acc_name.isEmpty()) + amounts_with_accounts++; + running_total += acc.getAmount(); + } + else { if (empty_amount == null) { empty_amount = item; single_empty_amount = true; single_empty_amount_has_account = !acc_name.isEmpty(); } - else if (!acc_name.isEmpty()) single_empty_amount = false; - } - else { - amounts++; - if (!acc_name.isEmpty()) amounts_with_accounts++; - running_total += acc.getAmount(); + else if (!acc_name.isEmpty()) + single_empty_amount = false; } } @@ -172,9 +185,27 @@ public class NewTransactionModel extends ViewModel { adapter.addRow(); } - if (single_empty_amount) { - empty_amount.setAmountHint(String.format(Locale.US, "%1.2f", - Misc.isZero(running_total) ? 0f : -running_total)); + for (NewTransactionModel.Item item : items) { + + final LedgerTransactionAccount acc = item.getAccount(); + if (acc.isAmountSet()) + continue; + + if (single_empty_amount) { + if (item.equals(empty_amount)) { + 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' + item.setAmountHint(null); + } + } debug("submittable", String.format(Locale.US, @@ -221,34 +252,40 @@ public class NewTransactionModel extends ViewModel { private MutableLiveData date = new MutableLiveData<>(); private MutableLiveData description = new MutableLiveData<>(); private LedgerTransactionAccount account; - private MutableLiveData amountHint = new MutableLiveData<>(); + private MutableLiveData amountHint = new MutableLiveData<>(null); private NewTransactionModel model; - private boolean editable = true; + private MutableLiveData editable = new MutableLiveData<>(true); public Item(NewTransactionModel model) { this.model = model; type = ItemType.bottomFiller; + editable.setValue(false); } public Item(NewTransactionModel model, Date date, String description) { this.model = model; this.type = ItemType.generalData; this.date.setValue(date); this.description.setValue(description); + this.editable.setValue(true); } public Item(NewTransactionModel model, LedgerTransactionAccount account) { this.model = model; this.type = ItemType.transactionRow; this.account = account; + this.editable.setValue(true); } public NewTransactionModel getModel() { return model; } - public boolean isEditable() { - ensureType(ItemType.transactionRow); - return editable; - } public void setEditable(boolean editable) { - ensureType(ItemType.transactionRow); - this.editable = editable; + 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); @@ -256,6 +293,17 @@ public class NewTransactionModel extends ViewModel { } 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, @@ -273,7 +321,7 @@ public class NewTransactionModel extends ViewModel { public void ensureType(ItemType wantedType) { if (type != wantedType) { throw new RuntimeException( - String.format("Actual type (%d) differs from wanted (%s)", type, + String.format("Actual type (%s) differs from wanted (%s)", type, wantedType)); } } @@ -354,9 +402,11 @@ public class NewTransactionModel extends ViewModel { * @return nicely formatted, shortest available date representation */ public String getFormattedDate() { - if (date == null) return null; + if (date == null) + return null; Date time = date.getValue(); - if (time == null) return null; + if (time == null) + return null; Calendar c = GregorianCalendar.getInstance(); c.setTime(time); @@ -376,5 +426,12 @@ public class NewTransactionModel extends ViewModel { return String.valueOf(myDay); } + public void observeEditableFlag(NewTransactionActivity activity, + Observer observer) { + editable.observe(activity, observer); + } + public void stopObservingEditableFlag(Observer observer) { + editable.removeObserver(observer); + } } }