]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
NT: correctly disable swiping of top and bottom entries, let all the rest be swiped
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemsAdapter.java
index 4d18db6a05e2d82d10a4b59c405788b6ceed42f2..5f2f4c6c4fa653b0a610a0187535a269a8387da1 100644 (file)
@@ -26,8 +26,6 @@ import androidx.annotation.NonNull;
 import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.RecyclerView;
 
-import com.google.android.material.snackbar.Snackbar;
-
 import net.ktnx.mobileledger.App;
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
@@ -84,15 +82,13 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             public int getMovementFlags(@NonNull RecyclerView recyclerView,
                                         @NonNull RecyclerView.ViewHolder viewHolder) {
                 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
-                // the top item is always there (date and description)
-                if (viewHolder.getAdapterPosition() > 0) {
+                // the top (date and description) and the bottom (padding) items are always there
+                final int adapterPosition = viewHolder.getAdapterPosition();
+                if ((adapterPosition > 0) && (adapterPosition < adapter.getItemCount() - 1)) {
                     flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
-                            ItemTouchHelper.UP | ItemTouchHelper.DOWN);
-
-                    if (viewModel.getAccountCount() > 2) {
-                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
-                                ItemTouchHelper.START | ItemTouchHelper.END);
-                    }
+                            ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
+                             makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+                                     ItemTouchHelper.START | ItemTouchHelper.END);
                 }
 
                 return flags;
@@ -108,18 +104,11 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             }
             @Override
             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
-                if (viewModel.getAccountCount() == 2)
-                    Snackbar.make(recyclerView, R.string.msg_at_least_two_accounts_are_required,
-                            Snackbar.LENGTH_LONG)
-                            .setAction("Action", null)
-                            .show();
-                else {
-                    int pos = viewHolder.getAdapterPosition();
-                    viewModel.removeItem(pos - 1);
-                    notifyItemRemoved(pos);
-                    viewModel.sendCountNotifications(); // needed after items re-arrangement
-                    viewModel.checkTransactionSubmittable(adapter);
-                }
+                int pos = viewHolder.getAdapterPosition();
+                viewModel.removeItem(pos - 1);
+                notifyItemRemoved(pos);
+                viewModel.sendCountNotifications(); // needed after items re-arrangement
+                viewModel.checkTransactionSubmittable(adapter);
             }
         });
     }
@@ -228,8 +217,8 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
             ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
             NewTransactionModel.Item firstNegative = null;
             NewTransactionModel.Item firstPositive = null;
-            boolean singleNegative = false;
-            boolean singlePositive = false;
+            int singleNegativeIndex = -1;
+            int singlePositiveIndex = -1;
             int negativeCount = 0;
             for (int i = 0; i < accounts.size(); i++) {
                 LedgerTransactionAccount acc = accounts.get(i);
@@ -248,18 +237,18 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     if (acc.getAmount() < 0) {
                         if (firstNegative == null) {
                             firstNegative = item;
-                            singleNegative = true;
+                            singleNegativeIndex = i;
                         }
                         else
-                            singleNegative = false;
+                            singleNegativeIndex = -1;
                     }
                     else {
                         if (firstPositive == null) {
                             firstPositive = item;
-                            singlePositive = true;
+                            singlePositiveIndex = i;
                         }
                         else
-                            singlePositive = false;
+                            singlePositiveIndex = -1;
                     }
                 }
                 else
@@ -268,13 +257,15 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                 notifyItemChanged(i + 1);
             }
 
-            if (singleNegative) {
+            if (singleNegativeIndex != -1) {
                 firstNegative.getAccount()
                              .resetAmount();
+                model.moveItemLast(singleNegativeIndex);
             }
-            else if (singlePositive) {
+            else if (singlePositiveIndex != -1) {
                 firstPositive.getAccount()
                              .resetAmount();
+                model.moveItemLast(singlePositiveIndex);
             }
         }
         model.checkTransactionSubmittable(this);