]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
NT: correctly disable swiping of top and bottom entries, let all the rest be swiped
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionModel.java
index 9017973998da8c39efaf91b110d640de1b22decf..7c49bf1b4b08a0e49ad9b166ed147367ea7ab894 100644 (file)
@@ -26,6 +26,7 @@ import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
 import net.ktnx.mobileledger.BuildConfig;
 import androidx.lifecycle.ViewModel;
 
 import net.ktnx.mobileledger.BuildConfig;
+import net.ktnx.mobileledger.model.Currency;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
@@ -34,6 +35,7 @@ import org.jetbrains.annotations.NotNull;
 
 import java.util.ArrayList;
 import java.util.Calendar;
 
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
@@ -53,6 +55,22 @@ public class NewTransactionModel extends ViewModel {
     private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
     private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
     private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
     private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
     private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
     private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
+    private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
+    final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
+    public boolean getSimulateSave() {
+        return simulateSave.getValue();
+    }
+    public void setSimulateSave(boolean simulateSave) {
+        this.simulateSave.setValue(simulateSave);
+    }
+    public void toggleSimulateSave() {
+        simulateSave.setValue(!simulateSave.getValue());
+    }
+    public void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                                    @NonNull
+                                            androidx.lifecycle.Observer<? super Boolean> observer) {
+        this.simulateSave.observe(owner, observer);
+    }
     public int getAccountCount() {
         return items.size();
     }
     public int getAccountCount() {
         return items.size();
     }
@@ -135,6 +153,8 @@ public class NewTransactionModel extends ViewModel {
      3a) there must be exactly one empty amount (with account)
      4) empty accounts with empty amounts are ignored
      5) a row with an empty account name or empty amount is guaranteed to exist
      3a) there must be exactly one empty amount (with account)
      4) empty accounts with empty amounts are ignored
      5) a row with an empty account name or empty amount is guaranteed to exist
+     6) at least two rows need to be present in the ledger
+
     */
     @SuppressLint("DefaultLocale")
     public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
     */
     @SuppressLint("DefaultLocale")
     public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
@@ -240,6 +260,9 @@ public class NewTransactionModel extends ViewModel {
                 adapter.addRow();
             }
 
                 adapter.addRow();
             }
 
+            // 6) at least two rows need to be present in the ledger
+            while (this.items.size() < 2) adapter.addRow();
+
 
             debug("submittable", submittable ? "YES" : "NO");
             isSubmittable.setValue(submittable);
 
             debug("submittable", submittable ? "YES" : "NO");
             isSubmittable.setValue(submittable);
@@ -249,8 +272,9 @@ public class NewTransactionModel extends ViewModel {
                 for (int i = 0; i < items.size(); i++) {
                     Item item = items.get(i);
                     LedgerTransactionAccount acc = item.getAccount();
                 for (int i = 0; i < items.size(); i++) {
                     Item item = items.get(i);
                     LedgerTransactionAccount acc = item.getAccount();
-                    debug("submittable", String.format("Item %2d: [%4.2f] %s", i,
-                            acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName()));
+                    debug("submittable", String.format("Item %2d: [%4.2f] %s (%s)", i,
+                            acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName(),
+                            acc.getComment()));
                 }
             }
         }
                 }
             }
         }
@@ -277,17 +301,39 @@ public class NewTransactionModel extends ViewModel {
     public void updateFocusedItem(int position) {
         focusedItem.setValue(position);
     }
     public void updateFocusedItem(int position) {
         focusedItem.setValue(position);
     }
-    public void noteFocusIsOnAccount(int position) {
-        getItem(position).setFocusIsOnAmount(false);
+    public void noteFocusChanged(int position, FocusedElement element) {
+        getItem(position).setFocusedElement(element);
     }
     }
-    public void noteFocusIsOnAmount(int position) {
-        getItem(position).setFocusIsOnAmount(true);
+    public void swapItems(int one, int two) {
+        Collections.swap(items, one - 1, two - 1);
+    }
+    public void toggleComment(int position) {
+        final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
+        commentVisible.postValue(!commentVisible.getValue());
+    }
+    public void moveItemLast(int index) {
+        /*   0
+             1   <-- index
+             2
+             3   <-- desired position
+         */
+        int itemCount = items.size();
+
+        if (index < itemCount - 1) {
+            Item acc = items.remove(index);
+            items.add(itemCount - 1, acc);
+        }
+    }
+    public void toggleCurrencyVisible() {
+        showCurrency.setValue(!showCurrency.getValue());
     }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================
 
     }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================
 
-    class Item extends Object {
+    enum FocusedElement {Account, Comment, Amount}
+
+    class Item {
         private ItemType type;
         private MutableLiveData<Date> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();
         private ItemType type;
         private MutableLiveData<Date> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();
@@ -295,10 +341,10 @@ public class NewTransactionModel extends ViewModel {
         private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
         private NewTransactionModel model;
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
         private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
         private NewTransactionModel model;
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
-        public boolean focusIsOnAmount() {
-            return focusIsOnAmount;
-        }
-        private boolean focusIsOnAmount = false;
+        private FocusedElement focusedElement = FocusedElement.Account;
+        private MutableLiveData<String> comment = new MutableLiveData<>(null);
+        private MutableLiveData<Boolean> commentVisible = new MutableLiveData<>(false);
+        private MutableLiveData<Currency> currency = new MutableLiveData<>(null);
         public Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
         public Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
@@ -317,6 +363,12 @@ public class NewTransactionModel extends ViewModel {
             this.account = account;
             this.editable.setValue(true);
         }
             this.account = account;
             this.editable.setValue(true);
         }
+        public FocusedElement getFocusedElement() {
+            return focusedElement;
+        }
+        public void setFocusedElement(FocusedElement focusedElement) {
+            this.focusedElement = focusedElement;
+        }
         public NewTransactionModel getModel() {
             return model;
         }
         public NewTransactionModel getModel() {
             return model;
         }
@@ -378,6 +430,13 @@ public class NewTransactionModel extends ViewModel {
             this.date.setValue(date);
         }
         public void setDate(String text) {
             this.date.setValue(date);
         }
         public void setDate(String text) {
+            if ((text == null) || text.trim()
+                                      .isEmpty())
+            {
+                setDate((Date) null);
+                return;
+            }
+
             int year, month, day;
             final Calendar c = GregorianCalendar.getInstance();
             Matcher m = reYMD.matcher(text);
             int year, month, day;
             final Calendar c = GregorianCalendar.getInstance();
             Matcher m = reYMD.matcher(text);
@@ -477,8 +536,37 @@ public class NewTransactionModel extends ViewModel {
         public void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
         public void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
-        public void setFocusIsOnAmount(boolean flag) {
-            focusIsOnAmount = flag;
+        public void observeCommentVisible(NewTransactionActivity activity,
+                                          Observer<Boolean> observer) {
+            commentVisible.observe(activity, observer);
+        }
+        public void stopObservingCommentVisible(Observer<Boolean> observer) {
+            commentVisible.removeObserver(observer);
+        }
+        public void observeComment(NewTransactionActivity activity, Observer<String> observer) {
+            comment.observe(activity, observer);
+        }
+        public void stopObservingComment(Observer<String> observer) {
+            comment.removeObserver(observer);
+        }
+        public void setComment(String comment) {
+            getAccount().setComment(comment);
+            this.comment.postValue(comment);
+        }
+        public Currency getCurrency() {
+            return this.currency.getValue();
+        }
+        public void setCurrency(Currency currency) {
+            getAccount().setCurrency((currency != null && !currency.getName()
+                                                                   .isEmpty()) ? currency.getName()
+                                                                               : null);
+            this.currency.setValue(currency);
+        }
+        public void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
+            currency.observe(activity, observer);
+        }
+        public void stopObservingCurrency(Observer<Currency> observer) {
+            currency.removeObserver(observer);
         }
     }
 }
         }
     }
 }