From c8a46620566a9027fe620cb16cf1aefa708085a3 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 23 Dec 2019 19:31:06 +0200 Subject: [PATCH] NT: move item touch helper to the adapter --- .../ui/activity/NewTransactionFragment.java | 39 --------- .../activity/NewTransactionItemsAdapter.java | 82 +++++++++++++++++++ .../ui/activity/NewTransactionModel.java | 4 + 3 files changed, 86 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java index f9e8a263..f212bc09 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionFragment.java @@ -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) { diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java index cb03f3d1..88f99a20 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java @@ -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 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