]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
two fallouts after transaction date reorganisation and 'go to date' feature
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemsAdapter.java
index 741af1b048252dc4ae84ee195f3e114ee8c71957..80deaf27281315af7974e05f2c7def6fd9bb1cae 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
@@ -29,7 +29,8 @@ import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
 
-import net.ktnx.mobileledger.App;
+import com.google.android.material.snackbar.Snackbar;
+
 import net.ktnx.mobileledger.BuildConfig;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
@@ -52,7 +53,7 @@ import static net.ktnx.mobileledger.utils.Logger.debug;
 
 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder>
         implements DescriptionSelectedCallback {
-    NewTransactionModel model;
+    private NewTransactionModel model;
     private MobileLedgerProfile mProfile;
     private ItemTouchHelper touchHelper;
     private RecyclerView recyclerView;
@@ -126,10 +127,10 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     public void setProfile(MobileLedgerProfile profile) {
         mProfile = profile;
     }
-    int addRow() {
+    private int addRow() {
         return addRow(null);
     }
-    int addRow(String commodity) {
+    private int addRow(String commodity) {
         final int newAccountCount = model.addAccount(new LedgerTransactionAccount("", commodity));
         Logger.debug("new-transaction",
                 String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
@@ -160,7 +161,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     public int getItemCount() {
         return model.getAccountCount() + 2;
     }
-    boolean accountListIsEmpty() {
+    private boolean accountListIsEmpty() {
         for (int i = 0; i < model.getAccountCount(); i++) {
             LedgerTransactionAccount acc = model.getAccount(i);
             if (!acc.getAccountName()
@@ -208,7 +209,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             params.add(accFilter);
         }
 
-        sb.append(" ORDER BY t.date DESC LIMIT 1");
+        sb.append(" ORDER BY t.year desc, t.month desc, t.day desc LIMIT 1");
 
         final String sql = sb.toString();
         debug("descr", sql);
@@ -241,11 +242,17 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
 
                 final String broaderSql =
                         "select t.profile, t.id from transactions t where t.description=?" +
-                        " ORDER BY date desc LIMIT 1";
+                        " ORDER BY year desc, month desc, day desc LIMIT 1";
                 params.remove(1);
                 debug("descr", broaderSql);
                 debug("descr", description);
 
+                activity.runOnUiThread(() -> {
+                    Snackbar.make(recyclerView, R.string.ignoring_preferred_account,
+                            Snackbar.LENGTH_LONG)
+                            .show();
+                });
+
                 MLDB.queryInBackground(broaderSql, new String[]{description},
                         new MLDB.CallbackHelper() {
                             @Override
@@ -344,7 +351,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             // TODO perhaps do only one notification about the whole range (notifyDatasetChanged)?
         }
     }
-    public void reset() {
+    void reset() {
         int presentItemCount = model.getAccountCount();
         model.reset();
         notifyItemChanged(0);       // header changed
@@ -352,18 +359,24 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         if (presentItemCount > 2)
             notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
     }
-    public void updateFocusedItem(int position) {
+    void updateFocusedItem(int position) {
         model.updateFocusedItem(position);
     }
-    public void noteFocusIsOnAccount(int position) {
+    void noteFocusIsOnAccount(int position) {
         model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account);
     }
-    public void noteFocusIsOnAmount(int position) {
+    void noteFocusIsOnAmount(int position) {
         model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount);
     }
-    public void noteFocusIsOnComment(int position) {
+    void noteFocusIsOnComment(int position) {
         model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
     }
+    void noteFocusIsOnTransactionComment(int position) {
+        model.noteFocusChanged(position, NewTransactionModel.FocusedElement.TransactionComment);
+    }
+    public void noteFocusIsOnDescription(int pos) {
+        model.noteFocusChanged(pos, NewTransactionModel.FocusedElement.Description);
+    }
     private void holdSubmittableChecks() {
         checkHoldCounter++;
     }
@@ -460,7 +473,12 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     itemsWithAccountForCurrency.add(currName, item);
                 }
 
-                if (acc.isAmountSet()) {
+                if (!acc.isAmountValid()) {
+                    Logger.debug("submittable",
+                            String.format("Not submittable: row %d has an invalid amount", i + 1));
+                    submittable = false;
+                }
+                else if (acc.isAmountSet()) {
                     itemsWithAmountForCurrency.add(currName, item);
                     balance.add(currName, acc.getAmount());
                 }
@@ -632,7 +650,8 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             model.isSubmittable.setValue(false);
         }
     }
-    private class BalanceForCurrency {
+
+    private static class BalanceForCurrency {
         private HashMap<String, Float> hashMap = new HashMap<>();
         float get(String currencyName) {
             Float f = hashMap.get(currencyName);
@@ -653,7 +672,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         }
     }
 
-    private class ItemsForCurrency {
+    private static class ItemsForCurrency {
         private HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
         @NonNull
         List<NewTransactionModel.Item> getList(@Nullable String currencyName) {