X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionModel.java;h=7cbbeca219a47159b6970b301ddb047fb3ca29e2;hb=a464909fb8bb0d3093579c4c4224304dabb2d2b2;hp=0f46d8aeb4815672d7af952c333e273a51845f37;hpb=865334093695e52f99d93ad9a255252b9d575053;p=mobile-ledger.git 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 0f46d8ae..7cbbeca2 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 @@ -20,9 +20,11 @@ package net.ktnx.mobileledger.ui.activity; 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; +import net.ktnx.mobileledger.utils.Misc; import org.jetbrains.annotations.NotNull; @@ -111,8 +113,10 @@ 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 @@ -173,7 +177,7 @@ public class NewTransactionModel extends ViewModel { if (single_empty_amount) { empty_amount.setAmountHint(String.format(Locale.US, "%1.2f", - (Math.abs(running_total) > 0.005) ? -running_total : 0f)); + Misc.isZero(running_total) ? 0f : -running_total)); } debug("submittable", String.format(Locale.US, @@ -206,10 +210,12 @@ public class NewTransactionModel extends ViewModel { isSubmittable.setValue(false); } } - public void removeItem(int pos, NewTransactionItemsAdapter adapter) { + public void removeItem(int pos) { items.remove(pos); accountCount.setValue(getAccountCount()); - checkTransactionSubmittable(adapter); + } + public void sendCountNotifications() { + accountCount.setValue(getAccountCount()); } enum ItemType {generalData, transactionRow, bottomFiller} @@ -220,32 +226,35 @@ public class NewTransactionModel extends ViewModel { private LedgerTransactionAccount account; private MutableLiveData amountHint = new MutableLiveData<>(); 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; + return this.editable.getValue(); } public void setEditable(boolean editable) { ensureType(ItemType.transactionRow); - this.editable = editable; + this.editable.setValue(editable); } public String getAmountHint() { ensureType(ItemType.transactionRow); @@ -270,7 +279,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)); } } @@ -373,5 +382,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); + } } }