Skip to content

Commit

Permalink
TrackList: Fix crashes in selection mode
Browse files Browse the repository at this point in the history
Fixes: #1711
  • Loading branch information
lmarz authored and dennisguse committed Oct 8, 2023
1 parent 8ca208a commit afbe941
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions src/main/java/de/dennisguse/opentracks/ui/TrackListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
selectionMode = true;
actionModeCallback.onPrepare(menu, getCheckedPositions(), getCheckedIds(), true);

actionModeCallback.onPrepare(menu, null, getCheckedIds(), true);
return true;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (actionModeCallback.onClick(item.getItemId(), getCheckedPositions(), getCheckedIds())) {
if (actionModeCallback.onClick(item.getItemId(), null, getCheckedIds())) {
mode.finish();
}
return true;
Expand All @@ -124,37 +125,34 @@ public void onDestroyActionMode(ActionMode mode) {
selectionMode = false;

setAllSelected(false);
selection.clear();

actionModeCallback.onDestroy();
}

public void setAllSelected(boolean isSelected) {
for (int i = 0; i < getItemCount(); i++) {
ViewHolder holder = (ViewHolder) recyclerView.findViewHolderForLayoutPosition(i);
holder.setSelected(isSelected);
if (isSelected) {
final int idIndex = tracks.getColumnIndexOrThrow(TracksColumns._ID);

tracks.moveToFirst();
do {
selection.put((int) tracks.getLong(idIndex), true);
} while (tracks.moveToNext());
} else {
selection.clear();
}
}

private int[] getCheckedPositions() {
List<Integer> positions = new ArrayList<>();

for (int i = 0; i < selection.size(); i++) {
if (selection.valueAt(i)) {
positions.add(selection.keyAt(i));
}
for (int i = 0; i < recyclerView.getChildCount(); i++) {
ViewHolder holder = (ViewHolder) recyclerView.getChildViewHolder(recyclerView.getChildAt(i));
holder.setSelected(isSelected);
}

return positions.stream().mapToInt(i -> i).toArray();
}

private long[] getCheckedIds() {
List<Long> ids = new ArrayList<>();

for (int i = 0; i < selection.size(); i++) {
if (selection.valueAt(i)) {
ViewHolder holder = (ViewHolder) recyclerView.findViewHolderForLayoutPosition(selection.keyAt(i));
ids.add(holder.getId());
ids.add((long) selection.keyAt(i));
}
}

Expand Down Expand Up @@ -251,7 +249,7 @@ public void bind(Cursor track) {
}

public void setSelected(boolean isSelected) {
selection.put(getLayoutPosition(), isSelected);
selection.put((int) getId(), isSelected);
view.setActivated(isSelected);
}

Expand Down Expand Up @@ -286,8 +284,12 @@ public void onClick(View v) {

@Override
public boolean onLongClick(View v) {
actionMode = context.startSupportActionMode(TrackListAdapter.this);
setSelected(true);
setSelected(!view.isActivated());
if (!selectionMode) {
actionMode = context.startSupportActionMode(TrackListAdapter.this);
} else {
actionMode.invalidate();
}
return true;
}
}
Expand Down

0 comments on commit afbe941

Please sign in to comment.