import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
+import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;
import net.ktnx.mobileledger.R;
class TemplateDetailsAdapter extends RecyclerView.Adapter<TemplateDetailsAdapter.ViewHolder> {
private static final String D_TEMPLATE_UI = "template-ui";
private final AsyncListDiffer<TemplateDetailsItem> differ;
- public TemplateDetailsAdapter() {
+ private final TemplateDetailsViewModel mModel;
+ private final ItemTouchHelper itemTouchHelper;
+ public TemplateDetailsAdapter(TemplateDetailsViewModel model) {
super();
+ mModel = model;
setHasStableIds(true);
differ = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<TemplateDetailsItem>() {
@Override
}
}
});
+ itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
+ @Override
+ public float getMoveThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
+ return 0.1f;
+ }
+ @Override
+ public boolean isLongPressDragEnabled() {
+ return false;
+ }
+ @Override
+ public RecyclerView.ViewHolder chooseDropTarget(
+ @NonNull RecyclerView.ViewHolder selected,
+ @NonNull List<RecyclerView.ViewHolder> dropTargets, int curX, int curY) {
+ RecyclerView.ViewHolder best = null;
+ int bestDistance = 0;
+ for (RecyclerView.ViewHolder v : dropTargets) {
+ if (v == selected)
+ continue;
+
+ final int viewTop = v.itemView.getTop();
+ int distance = Math.abs(viewTop - curY);
+ if (best == null) {
+ best = v;
+ bestDistance = distance;
+ }
+ else {
+ if (distance < bestDistance) {
+ bestDistance = distance;
+ best = v;
+ }
+ }
+ }
+
+ Logger.debug("dnd", "Best target is " + best);
+ return best;
+ }
+ @Override
+ public boolean canDropOver(@NonNull RecyclerView recyclerView,
+ @NonNull RecyclerView.ViewHolder current,
+ @NonNull RecyclerView.ViewHolder target) {
+ final int adapterPosition = target.getAdapterPosition();
+
+ // first item is immovable
+ if (adapterPosition == 0)
+ return false;
+
+ return super.canDropOver(recyclerView, current, target);
+ }
+ @Override
+ public int getMovementFlags(@NonNull RecyclerView recyclerView,
+ @NonNull RecyclerView.ViewHolder viewHolder) {
+ int flags = 0;
+ // the top item (transaction params) is always there
+ final int adapterPosition = viewHolder.getAdapterPosition();
+ if (adapterPosition > 0)
+ flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
+ ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
+ 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) {
+
+ final int fromPosition = viewHolder.getAdapterPosition();
+ final int toPosition = target.getAdapterPosition();
+ mModel.moveItem(fromPosition, toPosition);
+
+ return true;
+ }
+ @Override
+ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
+ int pos = viewHolder.getAdapterPosition();
+ mModel.removeItem(pos);
+ }
+ });
+ }
+ @Override
+ public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
+ super.onAttachedToRecyclerView(recyclerView);
+
+ itemTouchHelper.attachToRecyclerView(recyclerView);
+ }
+ @Override
+ public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
+ super.onDetachedFromRecyclerView(recyclerView);
+
+ itemTouchHelper.attachToRecyclerView(null);
}
@Override
public long getItemId(int position) {
};
b.templateDetailsNegateAmountLabel.setOnClickListener(negLabelClickListener);
b.templateDetailsNegateAmountText.setOnClickListener(negLabelClickListener);
+ b.patternAccountLabel.setOnTouchListener((v, event) -> {
+ if (event.getAction() == MotionEvent.ACTION_DOWN) {
+ itemTouchHelper.startDrag(this);
+ }
+ return false;
+ });
}
@Override
void bind(TemplateDetailsItem item) {
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
- android:paddingHorizontal="@dimen/text_margin"
+ android:padding="@dimen/text_margin"
>
<TextView
android:id="@+id/pattern_account_label"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
android:gravity="end"
- android:paddingTop="@dimen/text_margin"
android:text="@string/template_details_account_row_label"
android:textAppearance="?attr/textAppearanceListItem"
app:drawableBottomCompat="@drawable/dashed_border_8dp"
+ app:drawableStartCompat="@drawable/ic_baseline_drag_handle_24"
+ app:layout_constraintBottom_toTopOf="@id/template_account_name_source_label"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintHorizontal_bias="1.0"
+ app:layout_constraintTop_toTopOf="parent"
/>
<TextView
android:id="@+id/template_account_name_source_label"