]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
rework transaction date handling
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionModel.java
index 8a271fa7c2a2514783c63813314c33dec5a8eb5f..3350664eac43c41a32d9032f8c3edbfee6696988 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
  * This file is part of MoLe.
  * MoLe is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.annotation.SuppressLint;
-
 import androidx.annotation.NonNull;
+import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MutableLiveData;
 import androidx.lifecycle.Observer;
 import androidx.lifecycle.ViewModel;
 
-import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.model.Currency;
+import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
-import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.Misc;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.utils.Globals;
+import net.ktnx.mobileledger.utils.SimpleDate;
 
 import org.jetbrains.annotations.NotNull;
 
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
-import java.util.Date;
 import java.util.GregorianCalendar;
-import java.util.HashMap;
-import java.util.List;
 import java.util.Locale;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import static net.ktnx.mobileledger.utils.Logger.debug;
+import java.util.concurrent.atomic.AtomicInteger;
 
 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*$");
     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 ArrayList<Item> items = new ArrayList<>();
-    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);
-    public boolean getSimulateSave() {
+    private final AtomicInteger busyCounter = new AtomicInteger(0);
+    private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
+    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) {
+        if (!observingDataProfile)
+            Data.profile.observe(activity, profileObserver);
+        observingDataProfile = true;
+    }
+    boolean getSimulateSave() {
         return simulateSave.getValue();
     }
     public void setSimulateSave(boolean simulateSave) {
         this.simulateSave.setValue(simulateSave);
     }
-    public void toggleSimulateSave() {
+    void toggleSimulateSave() {
         simulateSave.setValue(!simulateSave.getValue());
     }
-    public void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                    @NonNull
-                                            androidx.lifecycle.Observer<? super Boolean> observer) {
+    void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                             @NonNull androidx.lifecycle.Observer<? super Boolean> observer) {
         this.simulateSave.observe(owner, observer);
     }
-    public int getAccountCount() {
+    int getAccountCount() {
         return items.size();
     }
-    public Date getDate() {
+    public SimpleDate getDate() {
         return header.date.getValue();
     }
     public String getDescription() {
         return header.description.getValue();
     }
-    public LiveData<Boolean> isSubmittable() {
+    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("")));
         focusedItem.setValue(0);
     }
-    public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                   @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
+    void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                            @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
         this.focusedItem.observe(owner, observer);
     }
-    public void stopObservingFocusedItem(
-            @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
+    void stopObservingFocusedItem(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
         this.focusedItem.removeObserver(observer);
     }
-    public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                    @NonNull
-                                            androidx.lifecycle.Observer<? super Integer> observer) {
+    void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                             @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
         this.accountCount.observe(owner, observer);
     }
-    public void stopObservingAccountCount(
-            @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
+    void stopObservingAccountCount(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
         this.accountCount.removeObserver(observer);
     }
-    public int getFocusedItem() { return focusedItem.getValue(); }
-    public void setFocusedItem(int position) {
+    int getFocusedItem() { return focusedItem.getValue(); }
+    void setFocusedItem(int position) {
         focusedItem.setValue(position);
     }
-    public int addAccount(LedgerTransactionAccount acc) {
+    int addAccount(LedgerTransactionAccount acc) {
         items.add(new Item(this, acc));
         accountCount.setValue(getAccountCount());
         return items.size();
@@ -136,7 +146,7 @@ public class NewTransactionModel extends ViewModel {
         return items.get(index)
                     .getAccount();
     }
-    public Item getItem(int index) {
+    Item getItem(int index) {
         if (index == 0) {
             return header;
         }
@@ -146,217 +156,34 @@ public class NewTransactionModel extends ViewModel {
 
         return trailer;
     }
-    /*
-     A transaction is submittable if:
-     0) has description
-     1) has at least two account names
-     2) each row with amount has account name
-     3) for each commodity:
-     3a) amounts must balance to 0, or
-     3b) 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 for each commodity
-     6) at least two rows need to be present in the ledger
-
-    */
-    @SuppressLint("DefaultLocale")
-    public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
-        int accounts = 0;
-        int amounts = 0;
-        int empty_rows = 0;
-        final Map<String, Float> balance = new HashMap<>();
-        final String descriptionText = getDescription();
-        boolean submittable = true;
-        final Map<String, List<Item>> itemsWithEmptyAmountForCurrency = new HashMap<>();
-        final Map<String, List<Item>> itemsWithAccountAndEmptyAmountForCurrency = new HashMap<>();
-
-        try {
-            if ((descriptionText == null) || descriptionText.trim()
-                                                            .isEmpty())
-            {
-                Logger.debug("submittable", "Transaction not submittable: missing description");
-                submittable = false;
-            }
-
-            for (int i = 0; i < this.items.size(); i++) {
-                Item item = this.items.get(i);
-
-                LedgerTransactionAccount acc = item.getAccount();
-                String acc_name = acc.getAccountName()
-                                     .trim();
-                String currName = acc.getCurrency();
-
-                if (acc_name.isEmpty()) {
-                    empty_rows++;
-
-                    if (acc.isAmountSet()) {
-                        // 2) each amount has account name
-                        Logger.debug("submittable", String.format(
-                                "Transaction not submittable: row %d has no account name, but has" +
-                                " amount %1.2f", i + 1, acc.getAmount()));
-                        submittable = false;
-                    }
-                }
-                else {
-                    accounts++;
-                }
-
-                if (acc.isAmountSet()) {
-                    amounts++;
-                    Float bal = balance.get(currName);
-                    if (bal == null)
-                        bal = 0f;
-                    bal += acc.getAmount();
-                    balance.put(currName, bal);
-                }
-                else {
-                    {
-                        List<Item> list = itemsWithEmptyAmountForCurrency.get(currName);
-                        if (list == null)
-                            list = new ArrayList<>();
-                        itemsWithEmptyAmountForCurrency.put(currName, list);
-                        list.add(item);
-                    }
-
-                    if (!acc_name.isEmpty()) {
-                        List<Item> list =
-                                itemsWithAccountAndEmptyAmountForCurrency.get(acc.getCurrency());
-                        if (list == null) {
-                            list = new ArrayList<>();
-                            itemsWithAccountAndEmptyAmountForCurrency.put(acc.getCurrency(), list);
-                        }
-                        list.add(item);
-                    }
-                }
-            }
-
-            // 1) has at least two account names
-            if (accounts < 2) {
-                if (accounts == 0)
-                    Logger.debug("submittable", "Transaction not submittable: no account names");
-                else if (accounts == 1)
-                    Logger.debug("submittable",
-                            "Transaction not submittable: only one account name");
-                else
-                    Logger.debug("submittable",
-                            String.format("Transaction not submittable: only %d account names",
-                                    accounts));
-                submittable = false;
-            }
-
-            // 3) for each commodity:
-            // 3a) amount must balance to 0, or
-            // 3b) there must be exactly one empty amount (with account)
-            for (String balCurrency : balance.keySet()) {
-                Float bal = balance.get(balCurrency);
-                float currencyBalance = (bal == null) ? 0f : bal;
-                if (Misc.isZero(currencyBalance)) {
-                    // remove hints from all amount inputs in that currency
-                    for (Item item : items) {
-                        final String itemCurrency = item.getCurrency()
-                                                        .getName();
-                        if (((balCurrency == null) && (null == itemCurrency)) ||
-                            ((balCurrency != null) && balCurrency.equals(itemCurrency)))
-                            item.setAmountHint(null);
-                    }
-                }
-                else {
-                    List<Item> list = itemsWithAccountAndEmptyAmountForCurrency.get(balCurrency);
-                    int balanceReceiversCount = (list == null) ? 0 : list.size();
-                    if (balanceReceiversCount != 1) {
-                        Logger.debug("submittable", (balanceReceiversCount == 0) ? String.format(
-                                "Transaction not submittable [%s]: non-zero balance " +
-                                "with no empty amounts with accounts", balCurrency) : String.format(
-                                "Transaction not submittable [%s]: non-zero balance " +
-                                "with multiple empty amounts with accounts", balCurrency));
-                        submittable = false;
-                    }
-
-                    List<Item> emptyAmountList = itemsWithEmptyAmountForCurrency.get(balCurrency);
-
-                    // suggest off-balance amount to a row and remove hints on other rows
-                    Item receiver = null;
-                    if ((list != null) && !list.isEmpty())
-                        receiver = list.get(0);
-                    else if ((emptyAmountList != null) && !emptyAmountList.isEmpty())
-                        receiver = emptyAmountList.get(0);
-
-                    for (Item item : items) {
-                        if (item.equals(receiver)) {
-                            Logger.debug("submittable",
-                                    String.format("Setting amount hint to %1.2f",
-                                            -currencyBalance));
-                            item.setAmountHint(String.format("%1.2f", -currencyBalance));
-                        }
-                        else
-                            item.setAmountHint(null);
-                    }
-                }
-            }
-
-            // 5) a row with an empty account name or empty amount is guaranteed to exist for
-            // each commodity
-            for (String balCurrency : balance.keySet()) {
-                if ((empty_rows == 0) &&
-                    ((this.items.size() == accounts) || (this.items.size() == amounts)))
-                {
-                    adapter.addRow(balCurrency);
-                }
-            }
-
-            // 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);
-
-            if (BuildConfig.DEBUG) {
-                debug("submittable", "== Dump of all items");
-                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] %s // %s", i,
-                            acc.isAmountSet() ? acc.getAmount() : 0, acc.getCurrency(),
-                            acc.getAccountName(), acc.getComment()));
-                }
-            }
-        }
-        catch (NumberFormatException e) {
-            debug("submittable", "NO (because of NumberFormatException)");
-            isSubmittable.setValue(false);
-        }
-        catch (Exception e) {
-            e.printStackTrace();
-            debug("submittable", "NO (because of an Exception)");
-            isSubmittable.setValue(false);
+    void removeRow(Item item, NewTransactionItemsAdapter adapter) {
+        int pos = items.indexOf(item);
+        items.remove(pos);
+        if (adapter != null) {
+            adapter.notifyItemRemoved(pos + 1);
+            sendCountNotifications();
         }
     }
-    public void removeItem(int pos) {
+    void removeItem(int pos) {
         items.remove(pos);
         accountCount.setValue(getAccountCount());
     }
-    public void sendCountNotifications() {
+    void sendCountNotifications() {
         accountCount.setValue(getAccountCount());
     }
     public void sendFocusedNotification() {
         focusedItem.setValue(focusedItem.getValue());
     }
-    public void updateFocusedItem(int position) {
+    void updateFocusedItem(int position) {
         focusedItem.setValue(position);
     }
-    public void noteFocusChanged(int position, FocusedElement element) {
+    void noteFocusChanged(int position, FocusedElement element) {
         getItem(position).setFocusedElement(element);
     }
-    public void swapItems(int one, int two) {
+    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) {
+    void moveItemLast(int index) {
         /*   0
              1   <-- index
              2
@@ -369,18 +196,39 @@ public class NewTransactionModel extends ViewModel {
             items.add(itemCount - 1, acc);
         }
     }
-    public void toggleCurrencyVisible() {
+    void toggleCurrencyVisible() {
         showCurrency.setValue(!showCurrency.getValue());
     }
+    void stopObservingBusyFlag(Observer<Boolean> observer) {
+        busyFlag.removeObserver(observer);
+    }
+    void incrementBusyCounter() {
+        int newValue = busyCounter.incrementAndGet();
+        if (newValue == 1)
+            busyFlag.postValue(true);
+    }
+    void decrementBusyCounter() {
+        int newValue = busyCounter.decrementAndGet();
+        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, Description, TransactionComment}
+
+
     //==========================================================================================
 
-    enum FocusedElement {Account, Comment, Amount}
 
-    class Item {
+    static class Item {
         private ItemType type;
-        private MutableLiveData<Date> date = new MutableLiveData<>();
+        private MutableLiveData<SimpleDate> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();
         private LedgerTransactionAccount account;
         private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
@@ -388,36 +236,42 @@ public class NewTransactionModel extends ViewModel {
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
         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) {
+        private MutableLiveData<Boolean> amountValid = new MutableLiveData<>(true);
+        private boolean amountHintIsSet = false;
+        Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
             editable.setValue(false);
         }
-        public Item(NewTransactionModel model, Date date, String description) {
+        Item(NewTransactionModel model, SimpleDate 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) {
+        Item(NewTransactionModel model, LedgerTransactionAccount account) {
             this.model = model;
             this.type = ItemType.transactionRow;
             this.account = account;
+            String currName = account.getCurrency();
+            Currency curr = null;
+            if ((currName != null) && !currName.isEmpty())
+                curr = Currency.loadByName(currName);
+            this.currency.setValue(curr);
             this.editable.setValue(true);
         }
-        public FocusedElement getFocusedElement() {
+        FocusedElement getFocusedElement() {
             return focusedElement;
         }
-        public void setFocusedElement(FocusedElement focusedElement) {
+        void setFocusedElement(FocusedElement focusedElement) {
             this.focusedElement = focusedElement;
         }
         public NewTransactionModel getModel() {
             return model;
         }
-        public void setEditable(boolean editable) {
+        void setEditable(boolean editable) {
             ensureType(ItemType.generalData, ItemType.transactionRow);
             this.editable.setValue(editable);
         }
@@ -428,96 +282,69 @@ public class NewTransactionModel extends ViewModel {
                                 type1, type2));
             }
         }
-        public String getAmountHint() {
+        String getAmountHint() {
             ensureType(ItemType.transactionRow);
             return amountHint.getValue();
         }
-        public void setAmountHint(String amountHint) {
+        void setAmountHint(String amountHint) {
             ensureType(ItemType.transactionRow);
 
             // avoid unnecessary triggers
             if (amountHint == null) {
                 if (this.amountHint.getValue() == null)
                     return;
+                amountHintIsSet = false;
             }
             else {
                 if (amountHint.equals(this.amountHint.getValue()))
                     return;
+                amountHintIsSet = true;
             }
 
             this.amountHint.setValue(amountHint);
         }
-        public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                      @NonNull
-                                              androidx.lifecycle.Observer<? super String> observer) {
+        void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                               @NonNull androidx.lifecycle.Observer<? super String> observer) {
             this.amountHint.observe(owner, observer);
         }
-        public void stopObservingAmountHint(
+        void stopObservingAmountHint(
                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
             this.amountHint.removeObserver(observer);
         }
-        public ItemType getType() {
+        ItemType getType() {
             return type;
         }
-        public void ensureType(ItemType wantedType) {
+        void ensureType(ItemType wantedType) {
             if (type != wantedType) {
                 throw new RuntimeException(
                         String.format("Actual type (%s) differs from wanted (%s)", type,
                                 wantedType));
             }
         }
-        public Date getDate() {
+        public SimpleDate getDate() {
             ensureType(ItemType.generalData);
             return date.getValue();
         }
-        public void setDate(Date date) {
+        public void setDate(SimpleDate date) {
             ensureType(ItemType.generalData);
             this.date.setValue(date);
         }
-        public void setDate(String text) {
+        public void setDate(String text) throws ParseException {
             if ((text == null) || text.trim()
                                       .isEmpty())
             {
-                setDate((Date) null);
+                setDate((SimpleDate) null);
                 return;
             }
 
-            int year, month, day;
-            final Calendar c = GregorianCalendar.getInstance();
-            Matcher m = reYMD.matcher(text);
-            if (m.matches()) {
-                year = Integer.parseInt(m.group(1));
-                month = Integer.parseInt(m.group(2)) - 1;   // month is 0-based
-                day = Integer.parseInt(m.group(3));
-            }
-            else {
-                year = c.get(Calendar.YEAR);
-                m = reMD.matcher(text);
-                if (m.matches()) {
-                    month = Integer.parseInt(m.group(1)) - 1;
-                    day = Integer.parseInt(m.group(2));
-                }
-                else {
-                    month = c.get(Calendar.MONTH);
-                    m = reD.matcher(text);
-                    if (m.matches()) {
-                        day = Integer.parseInt(m.group(1));
-                    }
-                    else {
-                        day = c.get(Calendar.DAY_OF_MONTH);
-                    }
-                }
-            }
-
-            c.set(year, month, day);
-
-            this.setDate(c.getTime());
+            SimpleDate date = Globals.parseLedgerDate(text);
+            this.setDate(date);
         }
-        public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                @NonNull androidx.lifecycle.Observer<? super Date> observer) {
+        void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                         @NonNull androidx.lifecycle.Observer<? super SimpleDate> observer) {
             this.date.observe(owner, observer);
         }
-        public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
+        void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super SimpleDate> observer) {
             this.date.removeObserver(observer);
         }
         public String getDescription() {
@@ -528,15 +355,30 @@ public class NewTransactionModel extends ViewModel {
             ensureType(ItemType.generalData);
             this.description.setValue(description);
         }
-        public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
-                                       @NonNull
-                                               androidx.lifecycle.Observer<? super String> observer) {
+        void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+                                @NonNull androidx.lifecycle.Observer<? super String> observer) {
             this.description.observe(owner, observer);
         }
-        public void stopObservingDescription(
+        void stopObservingDescription(
                 @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;
@@ -549,49 +391,35 @@ public class NewTransactionModel extends ViewModel {
          *
          * @return nicely formatted, shortest available date representation
          */
-        public String getFormattedDate() {
+        String getFormattedDate() {
             if (date == null)
                 return null;
-            Date time = date.getValue();
-            if (time == null)
+            SimpleDate d = date.getValue();
+            if (d == null)
                 return null;
 
-            Calendar c = GregorianCalendar.getInstance();
-            c.setTime(time);
             Calendar today = GregorianCalendar.getInstance();
 
-            final int myYear = c.get(Calendar.YEAR);
-            final int myMonth = c.get(Calendar.MONTH);
-            final int myDay = c.get(Calendar.DAY_OF_MONTH);
-
-            if (today.get(Calendar.YEAR) != myYear) {
-                return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth + 1, myDay);
+            if (today.get(Calendar.YEAR) != d.year) {
+                return String.format(Locale.US, "%d/%02d/%02d", d.year, d.month, d.day);
             }
 
-            if (today.get(Calendar.MONTH) != myMonth) {
-                return String.format(Locale.US, "%d/%02d", myMonth + 1, myDay);
+            if (today.get(Calendar.MONTH) != d.month - 1) {
+                return String.format(Locale.US, "%d/%02d", d.month, d.day);
             }
 
-            return String.valueOf(myDay);
+            return String.valueOf(d.day);
         }
-        public void observeEditableFlag(NewTransactionActivity activity,
-                                        Observer<Boolean> observer) {
+        void observeEditableFlag(NewTransactionActivity activity, Observer<Boolean> observer) {
             editable.observe(activity, observer);
         }
-        public void stopObservingEditableFlag(Observer<Boolean> observer) {
+        void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
-        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) {
+        void observeComment(NewTransactionActivity activity, Observer<String> observer) {
             comment.observe(activity, observer);
         }
-        public void stopObservingComment(Observer<String> observer) {
+        void stopObservingComment(Observer<String> observer) {
             comment.removeObserver(observer);
         }
         public void setComment(String comment) {
@@ -602,16 +430,39 @@ public class NewTransactionModel extends ViewModel {
             return this.currency.getValue();
         }
         public void setCurrency(Currency currency) {
-            getAccount().setCurrency((currency != null && !currency.getName()
-                                                                   .isEmpty()) ? currency.getName()
-                                                                               : null);
-            this.currency.setValue(currency);
+            Currency present = this.currency.getValue();
+            if ((currency == null) && (present != null) ||
+                (currency != null) && !currency.equals(present))
+            {
+                getAccount().setCurrency((currency != null && !currency.getName()
+                                                                       .isEmpty())
+                                         ? currency.getName() : null);
+                this.currency.setValue(currency);
+            }
         }
-        public void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
+        void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
             currency.observe(activity, observer);
         }
-        public void stopObservingCurrency(Observer<Currency> observer) {
+        void stopObservingCurrency(Observer<Currency> observer) {
             currency.removeObserver(observer);
         }
+        boolean isOfType(ItemType type) {
+            return this.type == type;
+        }
+        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);
+        }
     }
 }