Swipe to remove filter criteria

pull/996/head
Alex Baker 5 years ago
parent 410af772f2
commit 17e3750419

@ -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<Int, Int>, private val onClear: Runnable) : ItemTouchHelper.Callback() {
class CustomFilterItemTouchHelper(
private val context: Context,
private val onMove: Callback2<Int, Int>,
private val onDelete: Callback<Int>,
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<Int, Int>, 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<Int, Int>, priva
onClear.run()
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
onDelete.call(viewHolder.adapterPosition)
}
}

@ -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);

@ -1,9 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/window_background"
android:orientation="vertical">
android:background="@color/window_background">
<RelativeLayout
android:id="@+id/row"
@ -50,4 +49,4 @@
</RelativeLayout>
</LinearLayout>
</FrameLayout>

@ -45,9 +45,4 @@
style="@style/TagSettingsRow"
android:text="@string/choose_new_criteria" />
<TextView
android:id="@+id/delete"
style="@style/TagSettingsRow"
android:text="@string/delete" />
</LinearLayout>
Loading…
Cancel
Save