Fix Samsung Oreo sorting issue

pull/513/head
Alex Baker 6 years ago
parent 1be76b9fd3
commit 98f72e94da

@ -66,6 +66,10 @@ public class TaskAdapter {
this.helper = helper; this.helper = helper;
} }
public int getNumSelected() {
return selected.size();
}
public List<Long> getSelected() { public List<Long> getSelected() {
return newArrayList(selected); return newArrayList(selected);
} }

@ -113,8 +113,11 @@ public class ActionModeProvider {
@Override @Override
public void onDestroyActionMode(ActionMode actionMode) { public void onDestroyActionMode(ActionMode actionMode) {
adapter.clearSelections();
taskListRecyclerAdapter.onDestroyActionMode(); taskListRecyclerAdapter.onDestroyActionMode();
if (adapter.getNumSelected() > 0) {
adapter.clearSelections();
taskListRecyclerAdapter.notifyDataSetChanged();
}
} }
private void deleteSelectedItems() { private void deleteSelectedItems() {

@ -1,5 +1,8 @@
package org.tasks.tasklist; package org.tasks.tasklist;
import static android.support.v7.widget.helper.ItemTouchHelper.DOWN;
import static android.support.v7.widget.helper.ItemTouchHelper.UP;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper; import android.support.v7.widget.helper.ItemTouchHelper;
@ -13,7 +16,6 @@ public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final TaskListFragment taskList; private final TaskListFragment taskList;
private int from = -1; private int from = -1;
private int to = -1; private int to = -1;
private boolean dragging;
ItemTouchHelperCallback( ItemTouchHelperCallback(
TaskAdapter adapter, TaskListRecyclerAdapter recyclerAdapter, TaskListFragment taskList) { TaskAdapter adapter, TaskListRecyclerAdapter recyclerAdapter, TaskListFragment taskList) {
@ -22,43 +24,32 @@ public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
this.taskList = taskList; this.taskList = taskList;
} }
public boolean isDragging() {
return dragging;
}
public void setDragging(boolean dragging) {
this.dragging = dragging;
}
@Override @Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState); super.onSelectedChanged(viewHolder, actionState);
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
recyclerAdapter.startActionMode();
((ViewHolder) viewHolder).setMoving(true); ((ViewHolder) viewHolder).setMoving(true);
} }
} }
@Override @Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
if (!adapter.isManuallySorted()) { return adapter.isManuallySorted() && adapter.getNumSelected() == 0
return makeMovementFlags(0, 0); ? makeMovementFlags(UP | DOWN, getSwipeFlags((ViewHolder) viewHolder))
} : makeMovementFlags(0, 0);
ViewHolder vh = (ViewHolder) viewHolder; }
if (!recyclerAdapter.isActionModeActive()) {
int indentFlags = 0; private int getSwipeFlags(ViewHolder vh) {
if (vh.isIndented()) { int indentFlags = 0;
indentFlags |= ItemTouchHelper.LEFT; if (vh.isIndented()) {
} indentFlags |= ItemTouchHelper.LEFT;
int position = vh.getAdapterPosition();
if (position > 0 && adapter.canIndent(position, vh.task)) {
indentFlags |= ItemTouchHelper.RIGHT;
}
return makeMovementFlags(0, indentFlags);
} }
if (dragging) { int position = vh.getAdapterPosition();
return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0); if (position > 0 && adapter.canIndent(position, vh.task)) {
indentFlags |= ItemTouchHelper.RIGHT;
} }
return makeMovementFlags(0, 0); return indentFlags;
} }
@Override @Override
@ -101,17 +92,16 @@ public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder); super.clearView(recyclerView, viewHolder);
ViewHolder vh = (ViewHolder) viewHolder; ViewHolder vh = (ViewHolder) viewHolder;
if (dragging) { vh.setMoving(false);
vh.setMoving(false); if (recyclerAdapter.isActionModeActive()) {
dragging = false; recyclerAdapter.toggle(vh);
if (from != -1) { } else {
if (from >= 0 && from != to) { if (from >= 0 && from != to) {
if (from < to) { if (from < to) {
to++; to++;
}
adapter.moved(from, to);
taskList.loadTaskListContent(false);
} }
adapter.moved(from, to);
taskList.loadTaskListContent(false);
} }
} }
from = -1; from = -1;

@ -110,35 +110,39 @@ public class TaskListRecyclerAdapter extends RecyclerView.Adapter<ViewHolder>
@Override @Override
public boolean onLongPress(ViewHolder viewHolder) { public boolean onLongPress(ViewHolder viewHolder) {
toggle(viewHolder); if (!adapter.isManuallySorted()) {
startActionMode();
}
if (mode != null) {
toggle(viewHolder);
}
return true; return true;
} }
private void toggle(ViewHolder viewHolder) { void startActionMode() {
if (mode == null) {
mode = actionModeProvider.startActionMode(adapter, taskList, this);
updateModeTitle();
if (adapter.isManuallySorted()) {
Flags.set(Flags.TLFP_NO_INTERCEPT_TOUCH);
}
}
}
void toggle(ViewHolder viewHolder) {
adapter.toggleSelection(viewHolder.task); adapter.toggleSelection(viewHolder.task);
notifyItemChanged(viewHolder.getAdapterPosition()); notifyItemChanged(viewHolder.getAdapterPosition());
if (adapter.getSelected().isEmpty()) { if (adapter.getSelected().isEmpty()) {
itemTouchHelperCallback.setDragging(false);
finishActionMode(); finishActionMode();
} else { } else {
if (mode == null) {
mode = actionModeProvider.startActionMode(adapter, taskList, this);
if (adapter.isManuallySorted()) {
itemTouchHelperCallback.setDragging(true);
Flags.set(Flags.TLFP_NO_INTERCEPT_TOUCH);
} else {
itemTouchHelperCallback.setDragging(false);
}
} else {
itemTouchHelperCallback.setDragging(false);
}
updateModeTitle(); updateModeTitle();
} }
} }
private void updateModeTitle() { private void updateModeTitle() {
if (mode != null) { if (mode != null) {
mode.setTitle(Integer.toString(adapter.getSelected().size())); int count = Math.max(1, adapter.getNumSelected());
mode.setTitle(Integer.toString(count));
} }
} }
@ -197,14 +201,11 @@ public class TaskListRecyclerAdapter extends RecyclerView.Adapter<ViewHolder>
return asyncPagedListDiffer; return asyncPagedListDiffer;
} }
public boolean isActionModeActive() { boolean isActionModeActive() {
return mode != null; return mode != null;
} }
void onDestroyActionMode() { void onDestroyActionMode() {
mode = null; mode = null;
if (!itemTouchHelperCallback.isDragging()) {
notifyDataSetChanged();
}
} }
} }

Loading…
Cancel
Save