]> git.ktnx.net Git - mobile-ledger.git/commitdiff
move high-level login out of the new transaction model to the adapter
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 18 Apr 2020 12:50:04 +0000 (15:50 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 18 Apr 2020 12:50:04 +0000 (15:50 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index 89c887310296374f2e708828302aa4f69e898848..3b6f2ecbc2861e90c54f5767604576613c37b696 100644 (file)
@@ -185,7 +185,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 syncData();
                 Logger.debug("textWatcher",
                         "syncData() returned, checking if transaction is submittable");
-                adapter.model.checkTransactionSubmittable(adapter);
+                adapter.checkTransactionSubmittable();
                 Logger.debug("textWatcher", "done");
             }
         };
@@ -215,7 +215,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 }
 
                 if (syncData())
-                    adapter.model.checkTransactionSubmittable(adapter);
+                    adapter.checkTransactionSubmittable();
             }
         };
         tvDescription.addTextChangedListener(tw);
@@ -347,7 +347,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         currencyObserver = currency -> {
             setCurrency(currency);
-            adapter.model.checkTransactionSubmittable(adapter);
+            adapter.checkTransactionSubmittable();
         };
 
         currencyGapObserver = hasGap -> {
index ef5947afd37f8ecf2b9babe3cef7bc63fe2c4ae6..67a90ae10663fc60cdf241a937280625d2a8fe7a 100644 (file)
 
 package net.ktnx.mobileledger.ui.activity;
 
+import android.annotation.SuppressLint;
 import android.database.Cursor;
 import android.view.LayoutInflater;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
 
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
 
 import net.ktnx.mobileledger.App;
+import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
+import net.ktnx.mobileledger.model.Currency;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.utils.Logger;
+import net.ktnx.mobileledger.utils.Misc;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
+import java.util.Set;
 
 import static net.ktnx.mobileledger.utils.Logger.debug;
 
@@ -46,6 +54,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     private MobileLedgerProfile mProfile;
     private ItemTouchHelper touchHelper;
     private RecyclerView recyclerView;
+    private int checkHoldCounter = 0;
     NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
         super();
         model = viewModel;
@@ -108,7 +117,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 viewModel.removeItem(pos - 1);
                 notifyItemRemoved(pos);
                 viewModel.sendCountNotifications(); // needed after items re-arrangement
-                viewModel.checkTransactionSubmittable(adapter);
+                checkTransactionSubmittable();
             }
         });
     }
@@ -271,7 +280,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 model.moveItemLast(singlePositiveIndex);
             }
         }
-        model.checkTransactionSubmittable(this);
+        checkTransactionSubmittable();
         model.setFocusedItem(1);
     }
     public void toggleAllEditing(boolean editable) {
@@ -306,4 +315,321 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     public void toggleComment(int position) {
         model.toggleComment(position);
     }
+    private void holdSubmittableChecks() {
+        checkHoldCounter++;
+    }
+    private void releaseSubmittableChecks() {
+        if (checkHoldCounter == 0)
+            throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
+        checkHoldCounter--;
+    }
+    void setItemCurrency(NewTransactionModel.Item item, Currency newCurrency) {
+        Currency oldCurrency = item.getCurrency();
+        if (!Currency.equal(newCurrency, oldCurrency)) {
+            holdSubmittableChecks();
+            try {
+                item.setCurrency(newCurrency);
+//                for (Item i : items) {
+//                    if (Currency.equal(i.getCurrency(), oldCurrency))
+//                        i.setCurrency(newCurrency);
+//                }
+            }
+            finally {
+                releaseSubmittableChecks();
+            }
+
+            checkTransactionSubmittable();
+        }
+    }
+    /*
+         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
+         Side effects:
+         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")
+    void checkTransactionSubmittable() {
+        if (checkHoldCounter > 0)
+            return;
+
+        int accounts = 0;
+        final BalanceForCurrency balance = new BalanceForCurrency();
+        final String descriptionText = model.getDescription();
+        boolean submittable = true;
+        final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
+        final ItemsForCurrency itemsWithEmptyAmountForCurrency =
+                new ItemsForCurrency();
+        final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency =
+                new ItemsForCurrency();
+        final ItemsForCurrency itemsWithEmptyAccountForCurrency =
+                new ItemsForCurrency();
+        final ItemsForCurrency itemsWithAmountForCurrency =
+                new ItemsForCurrency();
+        final ItemsForCurrency itemsWithAccountForCurrency =
+                new ItemsForCurrency();
+        final ItemsForCurrency emptyRowsForCurrency =
+                new ItemsForCurrency();
+        final List<NewTransactionModel.Item> emptyRows = new ArrayList<>();
+
+        try {
+            if ((descriptionText == null) || descriptionText.trim()
+                                                            .isEmpty())
+            {
+                Logger.debug("submittable", "Transaction not submittable: missing description");
+                submittable = false;
+            }
+
+            for (int i = 0; i < model.items.size(); i++) {
+                NewTransactionModel.Item item = model.items.get(i);
+
+                LedgerTransactionAccount acc = item.getAccount();
+                String acc_name = acc.getAccountName()
+                                     .trim();
+                String currName = acc.getCurrency();
+
+                itemsForCurrency.add(currName, item);
+
+                if (acc_name.isEmpty()) {
+                    itemsWithEmptyAccountForCurrency.add(currName, item);
+
+                    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 {
+                        emptyRowsForCurrency.add(currName, item);
+                    }
+                }
+                else {
+                    accounts++;
+                    itemsWithAccountForCurrency.add(currName, item);
+                }
+
+                if (acc.isAmountSet()) {
+                    itemsWithAmountForCurrency.add(currName, item);
+                    balance.add(currName, acc.getAmount());
+                }
+                else {
+                    itemsWithEmptyAmountForCurrency.add(currName, item);
+
+                    if (!acc_name.isEmpty())
+                        itemsWithAccountAndEmptyAmountForCurrency.add(currName, 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 : itemsForCurrency.currencies()) {
+                float currencyBalance = balance.get(balCurrency);
+                if (Misc.isZero(currencyBalance)) {
+                    // remove hints from all amount inputs in that currency
+                    for (NewTransactionModel.Item item : model.items) {
+                        if (Currency.equal(item.getCurrency(), balCurrency))
+                            item.setAmountHint(null);
+                    }
+                }
+                else {
+                    List<NewTransactionModel.Item> list =
+                            itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
+                    int balanceReceiversCount = list.size();
+                    if (balanceReceiversCount != 1) {
+                        if (BuildConfig.DEBUG) {
+                            if (balanceReceiversCount == 0)
+                                Logger.debug("submittable", String.format(
+                                        "Transaction not submittable [%s]: non-zero balance " +
+                                        "with no empty amounts with accounts", balCurrency));
+                            else
+                                Logger.debug("submittable", String.format(
+                                        "Transaction not submittable [%s]: non-zero balance " +
+                                        "with multiple empty amounts with accounts", balCurrency));
+                        }
+                        submittable = false;
+                    }
+
+                    List<NewTransactionModel.Item> emptyAmountList =
+                            itemsWithEmptyAmountForCurrency.getList(balCurrency);
+
+                    // suggest off-balance amount to a row and remove hints on other rows
+                    NewTransactionModel.Item receiver = null;
+                    if (!list.isEmpty())
+                        receiver = list.get(0);
+                    else if (!emptyAmountList.isEmpty())
+                        receiver = emptyAmountList.get(0);
+
+                    for (NewTransactionModel.Item item : model.items) {
+                        if (!Currency.equal(item.getCurrency(), balCurrency))
+                            continue;
+
+                        if (item.equals(receiver)) {
+                            if (BuildConfig.DEBUG)
+                                Logger.debug("submittable",
+                                        String.format("Setting amount hint to %1.2f [%s]",
+                                                -currencyBalance, balCurrency));
+                            item.setAmountHint(String.format("%1.2f", -currencyBalance));
+                        }
+                        else {
+                            if (BuildConfig.DEBUG)
+                                Logger.debug("submittable",
+                                        String.format("Resetting hint of '%s' [%s]",
+                                                (item.getAccount() == null) ? "" : item.getAccount()
+                                                                                       .getAccountName(),
+                                                balCurrency));
+                            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.currencies()) {
+                int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
+                int currRows = itemsForCurrency.size(balCurrency);
+                int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
+                int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
+                if ((currEmptyRows == 0) &&
+                    ((currRows == currAccounts) || (currRows == currAmounts)))
+                {
+                    // perhaps there already is an unused empty row for another currency that
+                    // is not used?
+//                        boolean foundIt = false;
+//                        for (Item item : emptyRows) {
+//                            Currency itemCurrency = item.getCurrency();
+//                            String itemCurrencyName =
+//                                    (itemCurrency == null) ? "" : itemCurrency.getName();
+//                            if (Misc.isZero(balance.get(itemCurrencyName))) {
+//                                item.setCurrency(Currency.loadByName(balCurrency));
+//                                item.setAmountHint(
+//                                        String.format("%1.2f", -balance.get(balCurrency)));
+//                                foundIt = true;
+//                                break;
+//                            }
+//                        }
+//
+//                        if (!foundIt)
+                    addRow(balCurrency);
+                }
+            }
+
+            // drop extra empty rows, not needed
+            for (String currName : emptyRowsForCurrency.currencies()) {
+                List<NewTransactionModel.Item> emptyItems = emptyRowsForCurrency.getList(currName);
+                while ((model.items.size() > 2) && (emptyItems.size() > 1)) {
+                    NewTransactionModel.Item item = emptyItems.get(1);
+                    emptyItems.remove(1);
+                    model.removeRow(item, this);
+                }
+
+                // unused currency, remove last item (which is also an empty one)
+                if ((model.items.size() > 2) && (emptyItems.size() == 1)) {
+                    List<NewTransactionModel.Item> currItems = itemsForCurrency.getList(currName);
+
+                    if (currItems.size() == 1) {
+                        NewTransactionModel.Item item = emptyItems.get(0);
+                        model.removeRow(item, this);
+                    }
+                }
+            }
+
+            // 6) at least two rows need to be present in the ledger
+            while (model.items.size() < 2)
+                addRow();
+
+
+            debug("submittable", submittable ? "YES" : "NO");
+            model.isSubmittable.setValue(submittable);
+
+            if (BuildConfig.DEBUG) {
+                debug("submittable", "== Dump of all items");
+                for (int i = 0; i < model.items.size(); i++) {
+                    NewTransactionModel.Item item = model.items.get(i);
+                    LedgerTransactionAccount acc = item.getAccount();
+                    debug("submittable", String.format("Item %2d: [%4.2f(%s) %s] %s ; %s", i,
+                            acc.isAmountSet() ? acc.getAmount() : 0,
+                            item.isAmountHintSet() ? item.getAmountHint() : "ΓΈ", acc.getCurrency(),
+                            acc.getAccountName(), acc.getComment()));
+                }
+            }
+        }
+        catch (NumberFormatException e) {
+            debug("submittable", "NO (because of NumberFormatException)");
+            model.isSubmittable.setValue(false);
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            debug("submittable", "NO (because of an Exception)");
+            model.isSubmittable.setValue(false);
+        }
+    }
+    private class BalanceForCurrency {
+        private HashMap<String, Float> hashMap = new HashMap<>();
+        float get(String currencyName) {
+            Float f = hashMap.get(currencyName);
+            if (f == null) {
+                f = 0f;
+                hashMap.put(currencyName, f);
+            }
+            return f;
+        }
+        void add(String currencyName, float amount) {
+            hashMap.put(currencyName, get(currencyName) + amount);
+        }
+        Set<String> currencies() {
+            return hashMap.keySet();
+        }
+        boolean containsCurrency(String currencyName) {
+            return hashMap.containsKey(currencyName);
+        }
+    }
+
+    private class ItemsForCurrency {
+        private HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
+        @NonNull
+        List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
+            List<NewTransactionModel.Item> list = hashMap.get(currencyName);
+            if (list == null) {
+                list = new ArrayList<>();
+                hashMap.put(currencyName, list);
+            }
+            return list;
+        }
+        void add(@Nullable String currencyName, @NonNull NewTransactionModel.Item item) {
+            getList(currencyName).add(item);
+        }
+        int size(@Nullable String currencyName) {
+            return this.getList(currencyName)
+                       .size();
+        }
+        Set<String> currencies() {
+            return hashMap.keySet();
+        }
+    }
 }
index 34d3a8493a3a9e782de7d315b1f25d9e6656d7bb..ed283b211685798f80ea5c64851f4730094ee759 100644 (file)
 
 package net.ktnx.mobileledger.ui.activity;
 
-import android.annotation.SuppressLint;
-
 import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
 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.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.utils.Logger;
-import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NotNull;
 
@@ -42,52 +36,31 @@ 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.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import static net.ktnx.mobileledger.utils.Logger.debug;
-
 public class NewTransactionModel extends ViewModel {
     private static final Pattern reYMD =
             Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
     private static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
     private 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);
     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);
-    /*
-     Slots contain lists of items, all using the same currency, including the possible
-     item with no account/amount that is used to help balancing the transaction
-
-     There is one slot per currency
-     */
-    private final HashMap<String, List<Item>> slots = new HashMap<>();
-    private int checkHoldCounter = 0;
     private boolean observingDataProfile;
     private Observer<MobileLedgerProfile> profileObserver =
             profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
-    public void observeDataProfile(LifecycleOwner activity) {
+    void observeDataProfile(LifecycleOwner activity) {
         if (!observingDataProfile)
             Data.profile.observe(activity, profileObserver);
         observingDataProfile = true;
     }
-    void holdSubmittableChecks() {
-        checkHoldCounter++;
-    }
-    void releaseSubmittableChecks() {
-        if (checkHoldCounter == 0)
-            throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
-        checkHoldCounter--;
-    }
     boolean getSimulateSave() {
         return simulateSave.getValue();
     }
@@ -171,248 +144,7 @@ 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
-     Side effects:
-     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")
-    void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
-        if (checkHoldCounter > 0)
-            return;
-
-        int accounts = 0;
-        final BalanceForCurrency balance = new BalanceForCurrency();
-        final String descriptionText = getDescription();
-        boolean submittable = true;
-        final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
-        final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
-        final List<Item> emptyRows = new ArrayList<>();
-
-        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();
-
-                itemsForCurrency.add(currName, item);
-
-                if (acc_name.isEmpty()) {
-                    itemsWithEmptyAccountForCurrency.add(currName, item);
-
-                    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 {
-                        emptyRowsForCurrency.add(currName, item);
-                    }
-                }
-                else {
-                    accounts++;
-                    itemsWithAccountForCurrency.add(currName, item);
-                }
-
-                if (acc.isAmountSet()) {
-                    itemsWithAmountForCurrency.add(currName, item);
-                    balance.add(currName, acc.getAmount());
-                }
-                else {
-                    itemsWithEmptyAmountForCurrency.add(currName, item);
-
-                    if (!acc_name.isEmpty())
-                        itemsWithAccountAndEmptyAmountForCurrency.add(currName, 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 : itemsForCurrency.currencies()) {
-                float currencyBalance = balance.get(balCurrency);
-                if (Misc.isZero(currencyBalance)) {
-                    // remove hints from all amount inputs in that currency
-                    for (Item item : items) {
-                        if (Currency.equal(item.getCurrency(), balCurrency))
-                            item.setAmountHint(null);
-                    }
-                }
-                else {
-                    List<Item> list =
-                            itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
-                    int balanceReceiversCount = list.size();
-                    if (balanceReceiversCount != 1) {
-                        if (BuildConfig.DEBUG) {
-                            if (balanceReceiversCount == 0)
-                                Logger.debug("submittable", String.format(
-                                        "Transaction not submittable [%s]: non-zero balance " +
-                                        "with no empty amounts with accounts", balCurrency));
-                            else
-                                Logger.debug("submittable", String.format(
-                                        "Transaction not submittable [%s]: non-zero balance " +
-                                        "with multiple empty amounts with accounts", balCurrency));
-                        }
-                        submittable = false;
-                    }
-
-                    List<Item> emptyAmountList =
-                            itemsWithEmptyAmountForCurrency.getList(balCurrency);
-
-                    // suggest off-balance amount to a row and remove hints on other rows
-                    Item receiver = null;
-                    if (!list.isEmpty())
-                        receiver = list.get(0);
-                    else if (!emptyAmountList.isEmpty())
-                        receiver = emptyAmountList.get(0);
-
-                    for (Item item : items) {
-                        if (!Currency.equal(item.getCurrency(), balCurrency))
-                            continue;
-
-                        if (item.equals(receiver)) {
-                            if (BuildConfig.DEBUG)
-                                Logger.debug("submittable",
-                                        String.format("Setting amount hint to %1.2f [%s]",
-                                                -currencyBalance, balCurrency));
-                            item.setAmountHint(String.format("%1.2f", -currencyBalance));
-                        }
-                        else {
-                            if (BuildConfig.DEBUG)
-                                Logger.debug("submittable",
-                                        String.format("Resetting hint of '%s' [%s]",
-                                                (item.getAccount() == null) ? "" : item.getAccount()
-                                                                                       .getAccountName(),
-                                                balCurrency));
-                            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.currencies()) {
-                int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
-                int currRows = itemsForCurrency.size(balCurrency);
-                int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
-                int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
-                if ((currEmptyRows == 0) &&
-                    ((currRows == currAccounts) || (currRows == currAmounts)))
-                {
-                    // perhaps there already is an unused empty row for another currency that
-                    // is not used?
-//                        boolean foundIt = false;
-//                        for (Item item : emptyRows) {
-//                            Currency itemCurrency = item.getCurrency();
-//                            String itemCurrencyName =
-//                                    (itemCurrency == null) ? "" : itemCurrency.getName();
-//                            if (Misc.isZero(balance.get(itemCurrencyName))) {
-//                                item.setCurrency(Currency.loadByName(balCurrency));
-//                                item.setAmountHint(
-//                                        String.format("%1.2f", -balance.get(balCurrency)));
-//                                foundIt = true;
-//                                break;
-//                            }
-//                        }
-//
-//                        if (!foundIt)
-                    adapter.addRow(balCurrency);
-                }
-            }
-
-            // drop extra empty rows, not needed
-            for (String currName : emptyRowsForCurrency.currencies()) {
-                List<Item> emptyItems = emptyRowsForCurrency.getList(currName);
-                while ((this.items.size() > 2) && (emptyItems.size() > 1)) {
-                    Item item = emptyItems.get(1);
-                    emptyItems.remove(1);
-                    removeRow(item, adapter);
-                }
-
-                // unused currency, remove last item (which is also an empty one)
-                if ((items.size() > 2) && (emptyItems.size() == 1)) {
-                    List<Item> currItems = itemsForCurrency.getList(currName);
-
-                    if (currItems.size() == 1) {
-                        Item item = emptyItems.get(0);
-                        removeRow(item, adapter);
-                    }
-                }
-            }
-
-            // 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 ; %s", i,
-                            acc.isAmountSet() ? acc.getAmount() : 0,
-                            item.isAmountHintSet() ? item.getAmountHint() : "ΓΈ", 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);
-        }
-    }
-    private void removeRow(Item item, NewTransactionItemsAdapter adapter) {
+    void removeRow(Item item, NewTransactionItemsAdapter adapter) {
         int pos = items.indexOf(item);
         items.remove(pos);
         if (adapter != null) {
@@ -459,76 +191,15 @@ public class NewTransactionModel extends ViewModel {
     void toggleCurrencyVisible() {
         showCurrency.setValue(!showCurrency.getValue());
     }
-    public void setItemCurrency(Item item, Currency newCurrency,
-                                NewTransactionItemsAdapter adapter) {
-        Currency oldCurrency = item.getCurrency();
-        if (!Currency.equal(newCurrency, oldCurrency)) {
-            holdSubmittableChecks();
-            try {
-                item.setCurrency(newCurrency);
-//                for (Item i : items) {
-//                    if (Currency.equal(i.getCurrency(), oldCurrency))
-//                        i.setCurrency(newCurrency);
-//                }
-            }
-            finally {
-                releaseSubmittableChecks();
-            }
-
-            checkTransactionSubmittable(adapter);
-        }
-    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     enum FocusedElement {Account, Comment, Amount}
 
-    private class ItemsForCurrency {
-        private HashMap<String, List<Item>> hashMap = new HashMap<>();
-        @NonNull
-        List<Item> getList(@Nullable String currencyName) {
-            List<Item> list = hashMap.get(currencyName);
-            if (list == null) {
-                list = new ArrayList<>();
-                hashMap.put(currencyName, list);
-            }
-            return list;
-        }
-        void add(@Nullable String currencyName, @NonNull Item item) {
-            getList(currencyName).add(item);
-        }
-        int size(@Nullable String currencyName) {
-            return this.getList(currencyName)
-                       .size();
-        }
-        Set<String> currencies() {
-            return hashMap.keySet();
-        }
-    }
 
     //==========================================================================================
 
-    private class BalanceForCurrency {
-        private HashMap<String, Float> hashMap = new HashMap<>();
-        float get(String currencyName) {
-            Float f = hashMap.get(currencyName);
-            if (f == null) {
-                f = 0f;
-                hashMap.put(currencyName, f);
-            }
-            return f;
-        }
-        void add(String currencyName, float amount) {
-            hashMap.put(currencyName, get(currencyName) + amount);
-        }
-        Set<String> currencies() {
-            return hashMap.keySet();
-        }
-        boolean containsCurrency(String currencyName) {
-            return hashMap.containsKey(currencyName);
-        }
-    }
 
-    class Item {
+    static class Item {
         private ItemType type;
         private MutableLiveData<Date> date = new MutableLiveData<>();
         private MutableLiveData<String> description = new MutableLiveData<>();