From 17e3750419d18524668b3e5b262f2bd34ebd5cb3 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 23 Apr 2020 12:14:48 -0500 Subject: [PATCH] Swipe to remove filter criteria --- .../core/CustomFilterItemTouchHelper.kt | 39 +++++++++++++++++-- .../activities/FilterSettingsActivity.java | 13 +++---- app/src/main/res/layout/custom_filter_row.xml | 7 ++-- .../layout/dialog_custom_filter_row_edit.xml | 5 --- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/core/CustomFilterItemTouchHelper.kt b/app/src/main/java/com/todoroo/astrid/core/CustomFilterItemTouchHelper.kt index 57c9aaaf7..9f1938e56 100644 --- a/app/src/main/java/com/todoroo/astrid/core/CustomFilterItemTouchHelper.kt +++ b/app/src/main/java/com/todoroo/astrid/core/CustomFilterItemTouchHelper.kt @@ -1,12 +1,22 @@ package com.todoroo.astrid.core +import android.content.Context +import android.graphics.Canvas +import android.graphics.drawable.ColorDrawable +import androidx.core.content.ContextCompat import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView +import org.tasks.Callback import org.tasks.Callback2 +import org.tasks.R -class CustomFilterItemTouchHelper(private val onMove: Callback2, private val onClear: Runnable) : ItemTouchHelper.Callback() { +class CustomFilterItemTouchHelper( + private val context: Context, + private val onMove: Callback2, + private val onDelete: Callback, + private val onClear: Runnable) : ItemTouchHelper.Callback() { override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int { - return if (viewHolder.adapterPosition > 0) makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) else 0 + return if (viewHolder.adapterPosition > 0) makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) else 0 } override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { @@ -27,6 +37,27 @@ class CustomFilterItemTouchHelper(private val onMove: Callback2, priva return true } + override fun getSwipeEscapeVelocity(defaultValue: Float): Float { + return defaultValue * 3 + } + + override fun getSwipeVelocityThreshold(defaultValue: Float): Float { + return .6f + } + + override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) { + val itemView = viewHolder.itemView + val background = ColorDrawable(ContextCompat.getColor(context, R.color.overdue)) + if (dX > 0) { + background.setBounds(0, itemView.top, itemView.left + dX.toInt(), itemView.bottom) + } else if (dX < 0) { + background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom) + } + background.draw(c) + + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { super.clearView(recyclerView, viewHolder) @@ -35,5 +66,7 @@ class CustomFilterItemTouchHelper(private val onMove: Callback2, priva onClear.run() } - override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {} + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { + onDelete.call(viewHolder.adapterPosition) + } } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/activities/FilterSettingsActivity.java b/app/src/main/java/org/tasks/activities/FilterSettingsActivity.java index e453bb400..ff1982293 100644 --- a/app/src/main/java/org/tasks/activities/FilterSettingsActivity.java +++ b/app/src/main/java/org/tasks/activities/FilterSettingsActivity.java @@ -118,10 +118,9 @@ public class FilterSettingsActivity extends BaseListSettingsActivity { criteria.add(instance); } adapter = new CustomFilterAdapter(criteria, locale, this::onClick); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); - new ItemTouchHelper(new CustomFilterItemTouchHelper(this::onMove, this::updateList)) + new ItemTouchHelper(new CustomFilterItemTouchHelper(this, this::onMove, this::onDelete, this::updateList)) .attachToRecyclerView(recyclerView); fab.setExtended(isNew() || adapter.getItemCount() <= 1); @@ -131,6 +130,11 @@ public class FilterSettingsActivity extends BaseListSettingsActivity { updateTheme(); } + private void onDelete(int index) { + criteria.remove(index); + updateList(); + } + private void onMove(int from, int to) { CriterionInstance criterion = criteria.remove(from); criteria.add(to, criterion); @@ -156,11 +160,6 @@ public class FilterSettingsActivity extends BaseListSettingsActivity { updateList(); }) .show(); - view.findViewById(R.id.delete).setOnClickListener(v -> { - d.dismiss(); - criteria.remove(criterionInstance); - updateList(); - }); view.findViewById(R.id.reconfigure).setOnClickListener(v -> { d.dismiss(); addCriteria(criterionInstance); diff --git a/app/src/main/res/layout/custom_filter_row.xml b/app/src/main/res/layout/custom_filter_row.xml index 756482391..82dcf269d 100644 --- a/app/src/main/res/layout/custom_filter_row.xml +++ b/app/src/main/res/layout/custom_filter_row.xml @@ -1,9 +1,8 @@ - + android:background="@color/window_background"> - + diff --git a/app/src/main/res/layout/dialog_custom_filter_row_edit.xml b/app/src/main/res/layout/dialog_custom_filter_row_edit.xml index 417f1d424..9b38e6e01 100644 --- a/app/src/main/res/layout/dialog_custom_filter_row_edit.xml +++ b/app/src/main/res/layout/dialog_custom_filter_row_edit.xml @@ -45,9 +45,4 @@ style="@style/TagSettingsRow" android:text="@string/choose_new_criteria" /> - - \ No newline at end of file