]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
New transaction: fix clearing the single negative amount broken after ReecyclerView...
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemsAdapter.java
index 1585f1d8d48bde6c1cbb85f0ce04b93f3fb9373b..6844b6ce3d743f471db420ec7d1723dd33f1cd26 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;
@@ -64,6 +63,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
         // the header is at position 0
         notifyItemInserted(newAccountCount);
+        model.sendCountNotifications(); // needed after holders' positions have changed
         return newAccountCount;
     }
     @NonNull
@@ -77,15 +77,15 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     @Override
     public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
         Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
-        holder.setData(model.getItem(position));
-        Logger.debug("bind", String.format(Locale.US, "Bound item at position %d", position));
+        NewTransactionModel.Item item = model.getItem(position);
+        holder.setData(item);
+        Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
+                                                                                          .toString(),
+                position));
     }
     @Override
     public int getItemCount() {
-        final int itemCount = model.getAccountCount() + 2;
-        Logger.debug("new-transaction",
-                String.format(Locale.US, "getItemCount() returning %d", itemCount));
-        return itemCount;
+        return model.getAccountCount() + 2;
     }
     boolean accountListIsEmpty() {
         for (int i = 0; i < model.getAccountCount(); i++) {
@@ -138,12 +138,13 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
 
             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);
                 }
@@ -151,12 +152,28 @@ 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);
@@ -177,8 +194,4 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         if (presentItemCount > 2)
             notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
     }
-    public void removeItem(int pos) {
-        model.removeItem(pos - 1, this);
-        notifyItemRemoved(pos);
-    }
 }