fix swipe-away in reworked new transaction activity
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 10 Nov 2019 17:44:16 +0000 (19:44 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 10 Nov 2019 17:45:13 +0000 (19:45 +0200)
the first item (date and description) can't be swiped, as well as the last
two remaining accounts

app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index 950bce3..4e6ee19 100644 (file)
@@ -91,9 +91,14 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
             public int getMovementFlags(@NonNull RecyclerView recyclerView,
                                         @NonNull RecyclerView.ViewHolder viewHolder) {
                 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
-                if (viewModel.getAccountCount() > 2) flags |=
-                        makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+                // the top item is always there (date and description)
+                if (viewHolder.getAdapterPosition() > 0) {
+                    if (viewModel.getAccountCount() > 2) {
+                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
                                 ItemTouchHelper.START | ItemTouchHelper.END);
+                    }
+                }
+
                 return flags;
             }
             @Override
@@ -109,9 +114,10 @@ public class NewTransactionActivity extends ProfileThemedActivity implements Tas
                             Snackbar.LENGTH_LONG).setAction("Action", null).show();
                 else {
                     int pos = viewHolder.getAdapterPosition();
-                    listAdapter.removeItem(pos);
-                    // FIXME hook next/prev links somehow
-                    throw new RuntimeException("TODO");
+                    viewModel.removeItem(pos - 1);
+                    listAdapter.notifyItemRemoved(pos);
+                    viewModel.sendCountNotifications(); // needed after items re-arrangement
+                    viewModel.checkTransactionSubmittable(listAdapter);
                 }
             }
         }).attachToRecyclerView(list);
index 1585f1d..91c6d31 100644 (file)
@@ -64,6 +64,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
@@ -177,8 +178,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);
-    }
 }
index 330b5ac..f6ecb44 100644 (file)
@@ -207,10 +207,12 @@ public class NewTransactionModel extends ViewModel {
             isSubmittable.setValue(false);
         }
     }
-    public void removeItem(int pos, NewTransactionItemsAdapter adapter) {
+    public void removeItem(int pos) {
         items.remove(pos);
         accountCount.setValue(getAccountCount());
-        checkTransactionSubmittable(adapter);
+    }
+    public void sendCountNotifications() {
+        accountCount.setValue(getAccountCount());
     }
     enum ItemType {generalData, transactionRow, bottomFiller}