]> git.ktnx.net Git - mobile-ledger.git/commitdiff
NT: move item touch helper to the adapter
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 23 Dec 2019 17:31:06 +0000 (19:31 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 23 Dec 2019 17:31:06 +0000 (19:31 +0200)
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index f9e8a2637de27ff5195044249bc0604c5186f5fa..f212bc0902eda6c7efb3cabadf8486f95d722419 100644 (file)
@@ -31,7 +31,6 @@ import androidx.annotation.Nullable;
 import androidx.fragment.app.Fragment;
 import androidx.fragment.app.FragmentActivity;
 import androidx.lifecycle.ViewModelProvider;
-import androidx.recyclerview.widget.ItemTouchHelper;
 import androidx.recyclerview.widget.LinearLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -106,44 +105,6 @@ public class NewTransactionFragment extends Fragment {
             listAdapter.setProfile(profile);
         });
         listAdapter.notifyDataSetChanged();
-        new ItemTouchHelper(new ItemTouchHelper.Callback() {
-            @Override
-            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) {
-                    if (viewModel.getAccountCount() > 2) {
-                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
-                                ItemTouchHelper.START | ItemTouchHelper.END);
-                    }
-                }
-
-                return flags;
-            }
-            @Override
-            public boolean onMove(@NonNull RecyclerView recyclerView,
-                                  @NonNull RecyclerView.ViewHolder viewHolder,
-                                  @NonNull RecyclerView.ViewHolder target) {
-                return false;
-            }
-            @Override
-            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
-                if (viewModel.getAccountCount() == 2)
-                    Snackbar.make(list, 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);
-                    listAdapter.notifyItemRemoved(pos);
-                    viewModel.sendCountNotifications(); // needed after items re-arrangement
-                    viewModel.checkTransactionSubmittable(listAdapter);
-                }
-            }
-        }).attachToRecyclerView(list);
-
         viewModel.isSubmittable()
                  .observe(getViewLifecycleOwner(), isSubmittable -> {
                      if (isSubmittable) {
index cb03f3d1d29425303f47ca1f9cc45f62e8339740..88f99a20fe330a71f38027171879acd4ffaee494 100644 (file)
@@ -23,8 +23,11 @@ import android.view.ViewGroup;
 import android.widget.LinearLayout;
 
 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;
@@ -43,6 +46,8 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         implements DescriptionSelectedCallback {
     NewTransactionModel model;
     private MobileLedgerProfile mProfile;
+    private ItemTouchHelper touchHelper;
+    private RecyclerView recyclerView;
     NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
         super();
         model = viewModel;
@@ -53,6 +58,70 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
                     String.format(Locale.US, "%d accounts is too little, Calling addRow()", size));
             size = addRow();
         }
+
+        NewTransactionItemsAdapter adapter = this;
+
+        touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
+            @Override
+            public boolean isLongPressDragEnabled() {
+                return true;
+            }
+            @Override
+            public boolean canDropOver(@NonNull RecyclerView recyclerView,
+                                       @NonNull RecyclerView.ViewHolder current,
+                                       @NonNull RecyclerView.ViewHolder target) {
+                final int adapterPosition = target.getAdapterPosition();
+
+                // first and last items are immovable
+                if (adapterPosition == 0)
+                    return false;
+                if (adapterPosition == adapter.getItemCount() - 1)
+                    return false;
+
+                return super.canDropOver(recyclerView, current, target);
+            }
+            @Override
+            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) {
+                    flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
+                            ItemTouchHelper.UP | ItemTouchHelper.DOWN);
+
+                    if (viewModel.getAccountCount() > 2) {
+                        flags |= makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
+                                ItemTouchHelper.START | ItemTouchHelper.END);
+                    }
+                }
+
+                return flags;
+            }
+            @Override
+            public boolean onMove(@NonNull RecyclerView recyclerView,
+                                  @NonNull RecyclerView.ViewHolder viewHolder,
+                                  @NonNull RecyclerView.ViewHolder target) {
+
+                model.swapItems(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+                notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
+                return true;
+            }
+            @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);
+                }
+            }
+        });
     }
     public void setProfile(MobileLedgerProfile profile) {
         mProfile = profile;
@@ -72,6 +141,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
         LinearLayout row = (LinearLayout) LayoutInflater.from(parent.getContext())
                                                         .inflate(R.layout.new_transaction_row,
                                                                 parent, false);
+
         return new NewTransactionItemHolder(row, this);
     }
     @Override
@@ -99,6 +169,18 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
 
         return true;
     }
+    @Override
+    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
+        super.onAttachedToRecyclerView(recyclerView);
+        this.recyclerView = recyclerView;
+        touchHelper.attachToRecyclerView(recyclerView);
+    }
+    @Override
+    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
+        touchHelper.attachToRecyclerView(null);
+        super.onDetachedFromRecyclerView(recyclerView);
+        this.recyclerView = null;
+    }
     public void descriptionSelected(String description) {
         debug("descr selected", description);
         if (!accountListIsEmpty())
index 1f4cec56ab462ee6d903ab42254a5e57e20b7531..0801eb2ecbb4769ac5e740395d823a460ad84d04 100644 (file)
@@ -34,6 +34,7 @@ import org.jetbrains.annotations.NotNull;
 
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Collections;
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.List;
@@ -298,6 +299,9 @@ public class NewTransactionModel extends ViewModel {
     public void noteFocusIsOnAmount(int position) {
         getItem(position).setFocusIsOnAmount(true);
     }
+    public void swapItems(int one, int two) {
+        Collections.swap(items, one-1, two-1);
+    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================