]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
posting-level flag for amount validity; drop custom amount text filter
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionModel.java
index 00e44508a33d4ac0856aa39b3c2485c8dbcaf252..83ffbb8f7bed90cdf747e60cc0daf6ef3d61e182 100644 (file)
@@ -49,17 +49,23 @@ public class NewTransactionModel extends ViewModel {
     final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
     final ArrayList<Item> items = new ArrayList<>();
     final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
+    final MutableLiveData<Boolean> showComments = new MutableLiveData<>(true);
     private final Item header = new Item(this, null, "");
     private final Item trailer = new Item(this);
     private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
     private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
     private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
-    private boolean observingDataProfile;
-    private Observer<MobileLedgerProfile> profileObserver =
-            profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
     private final AtomicInteger busyCounter = new AtomicInteger(0);
     private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
-    void observeBusyFlag(LifecycleOwner owner, Observer<? super Boolean> observer) {
+    private boolean observingDataProfile;
+    private Observer<MobileLedgerProfile> profileObserver = profile -> {
+        showCurrency.postValue(profile.getShowCommodityByDefault());
+        showComments.postValue(profile.getShowCommentsByDefault());
+    };
+    void observeShowComments(LifecycleOwner owner, Observer<? super Boolean> observer) {
+        showComments.observe(owner, observer);
+    }
+    void observeBusyFlag(@NonNull LifecycleOwner owner, Observer<? super Boolean> observer) {
         busyFlag.observe(owner, observer);
     }
     void observeDataProfile(LifecycleOwner activity) {
@@ -89,12 +95,16 @@ public class NewTransactionModel extends ViewModel {
     public String getDescription() {
         return header.description.getValue();
     }
+    public String getComment() {
+        return header.comment.getValue();
+    }
     LiveData<Boolean> isSubmittable() {
         return this.isSubmittable;
     }
     void reset() {
         header.date.setValue(null);
         header.description.setValue(null);
+        header.comment.setValue(null);
         items.clear();
         items.add(new Item(this, new LedgerTransactionAccount("")));
         items.add(new Item(this, new LedgerTransactionAccount("")));
@@ -198,15 +208,23 @@ public class NewTransactionModel extends ViewModel {
     }
     void incrementBusyCounter() {
         int newValue = busyCounter.incrementAndGet();
-        if (newValue == 1) busyFlag.postValue(true);
+        if (newValue == 1)
+            busyFlag.postValue(true);
     }
     void decrementBusyCounter() {
         int newValue = busyCounter.decrementAndGet();
-        if (newValue == 0) busyFlag.postValue(false);
+        if (newValue == 0)
+            busyFlag.postValue(false);
+    }
+    public boolean getBusyFlag() {
+        return busyFlag.getValue();
+    }
+    public void toggleShowComments() {
+        showComments.setValue(!showComments.getValue());
     }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
-    enum FocusedElement {Account, Comment, Amount}
+    enum FocusedElement {Account, Comment, Amount, Description, TransactionComment}
 
 
     //==========================================================================================
@@ -223,6 +241,7 @@ public class NewTransactionModel extends ViewModel {
         private FocusedElement focusedElement = FocusedElement.Account;
         private MutableLiveData<String> comment = new MutableLiveData<>(null);
         private MutableLiveData<Currency> currency = new MutableLiveData<>(null);
+        private MutableLiveData<Boolean> amountValid = new MutableLiveData<>(true);
         private boolean amountHintIsSet = false;
         Item(NewTransactionModel model) {
             this.model = model;
@@ -376,6 +395,22 @@ public class NewTransactionModel extends ViewModel {
                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
             this.description.removeObserver(observer);
         }
+        public String getTransactionComment() {
+            ensureType(ItemType.generalData);
+            return comment.getValue();
+        }
+        public void setTransactionComment(String transactionComment) {
+            ensureType(ItemType.generalData);
+            this.comment.setValue(transactionComment);
+        }
+        void observeTransactionComment(@NonNull @NotNull LifecycleOwner owner,
+                                       @NonNull Observer<? super String> observer) {
+            ensureType(ItemType.generalData);
+            this.comment.observe(owner, observer);
+        }
+        void stopObservingTransactionComment(@NonNull Observer<? super String> observer) {
+            this.comment.removeObserver(observer);
+        }
         public LedgerTransactionAccount getAccount() {
             ensureType(ItemType.transactionRow);
             return account;
@@ -455,5 +490,17 @@ public class NewTransactionModel extends ViewModel {
         boolean isAmountHintSet() {
             return amountHintIsSet;
         }
+        void validateAmount() {
+            amountValid.setValue(true);
+        }
+        void invalidateAmount() {
+            amountValid.setValue(false);
+        }
+        void observeAmountValidity(NewTransactionActivity activity, Observer<Boolean> observer) {
+            amountValid.observe(activity, observer);
+        }
+        void stopObservingAmountValidity(Observer<Boolean> observer) {
+            amountValid.removeObserver(observer);
+        }
     }
 }