]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
New transaction: when toggling editting, include the head row
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemsAdapter.java
index 76894bc33524d6257261ea7f78bf4f7bb371379e..2466d70c2bc7621cbc35a99cd31bbb73ab579a51 100644 (file)
@@ -21,7 +21,6 @@ import android.database.Cursor;
 import android.view.LayoutInflater;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
-import android.widget.TableRow;
 
 import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.RecyclerView;
@@ -92,15 +91,18 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         for (int i = 0; i < model.getAccountCount(); i++) {
             LedgerTransactionAccount acc = model.getAccount(i);
             if (!acc.getAccountName()
-                    .isEmpty()) return false;
-            if (acc.isAmountSet()) return false;
+                    .isEmpty())
+                return false;
+            if (acc.isAmountSet())
+                return false;
         }
 
         return true;
     }
     public void descriptionSelected(String description) {
         debug("descr selected", description);
-        if (!accountListIsEmpty()) return;
+        if (!accountListIsEmpty())
+            return;
 
         String accFilter = mProfile.getPreferredAccountsFilter();
 
@@ -127,24 +129,28 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         try (Cursor c = App.getDatabase()
                            .rawQuery(sql, params.toArray(new String[]{})))
         {
-            if (!c.moveToNext()) return;
+            if (!c.moveToNext())
+                return;
 
             String profileUUID = c.getString(0);
             int transactionId = c.getInt(1);
             LedgerTransaction tr;
             MobileLedgerProfile profile = Data.getProfile(profileUUID);
-            if (profile == null) throw new RuntimeException(String.format(
-                    "Unable to find profile %s, which is supposed to contain " +
-                    "transaction %d with description %s", profileUUID, transactionId, description));
+            if (profile == null)
+                throw new RuntimeException(String.format(
+                        "Unable to find profile %s, which is supposed to contain " +
+                        "transaction %d with description %s", profileUUID, transactionId,
+                        description));
 
             tr = profile.loadTransaction(transactionId);
             ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
-            TableRow firstNegative = null;
+            NewTransactionModel.Item firstNegative = null;
+            boolean singleNegative = false;
             int negativeCount = 0;
             for (int i = 0; i < accounts.size(); i++) {
                 LedgerTransactionAccount acc = accounts.get(i);
                 NewTransactionModel.Item item;
-                if (model.getAccountCount() < i) {
+                if (model.getAccountCount() < i + 1) {
                     model.addAccount(acc);
                     notifyItemInserted(i + 1);
                 }
@@ -152,22 +158,39 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
 
                 item.getAccount()
                     .setAccountName(acc.getAccountName());
-                if (acc.isAmountSet()) item.getAccount()
-                                           .setAmount(acc.getAmount());
-                else item.getAccount()
-                         .resetAmount();
+                if (acc.isAmountSet()) {
+                    item.getAccount()
+                        .setAmount(acc.getAmount());
+                    if (acc.getAmount() < 0) {
+                        if (firstNegative == null) {
+                            firstNegative = item;
+                            singleNegative = true;
+                        }
+                        else
+                            singleNegative = false;
+                    }
+                }
+                else
+                    item.getAccount()
+                        .resetAmount();
                 notifyItemChanged(i + 1);
             }
+
+            if (singleNegative) {
+                firstNegative.getAccount()
+                             .resetAmount();
+            }
         }
         model.checkTransactionSubmittable(this);
         model.setFocusedItem(1);
     }
     public void toggleAllEditing(boolean editable) {
-        for (int i = 0; i < model.getAccountCount(); i++) {
-            model.getItem(i + 1)
+        // item 0 is the header
+        for (int i = 0; i <= model.getAccountCount(); i++) {
+            model.getItem(i)
                  .setEditable(editable);
-            notifyItemChanged(i + 1);
-            // TODO perhaps do only one notification about the whole range [1…count]?
+            notifyItemChanged(i);
+            // TODO perhaps do only one notification about the whole range (notifyDatasetChanged)?
         }
     }
     public void reset() {