Fix bug related to gallery selection state.

TreeSets are annoying. contains() is calculated with the comparator,
which can lead to some weird bugs. Made sure the comparator didn't think
two items with the same date were identical.

Also fixed stableId generation to avoid any potential weirdness there.
This commit is contained in:
Greyson Parrelli 2019-01-14 15:40:38 -08:00
parent 5450967d00
commit 4952b4470d
3 changed files with 66 additions and 21 deletions

View File

@ -10,6 +10,7 @@ import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.components.ThumbnailView;
import org.thoughtcrime.securesms.mediasend.Media;
import org.thoughtcrime.securesms.mms.GlideRequests;
import org.thoughtcrime.securesms.util.StableIdGenerator;
import java.util.ArrayList;
import java.util.List;
@ -20,6 +21,7 @@ public class MediaRailAdapter extends RecyclerView.Adapter<MediaRailAdapter.Medi
private final List<Media> media;
private final RailItemListener listener;
private final boolean deleteEnabled;
private final StableIdGenerator<Media> stableIdGenerator;
private int activePosition;
@ -28,6 +30,9 @@ public class MediaRailAdapter extends RecyclerView.Adapter<MediaRailAdapter.Medi
this.media = new ArrayList<>();
this.listener = listener;
this.deleteEnabled = deleteEnabled;
this.stableIdGenerator = new StableIdGenerator<>();
setHasStableIds(true);
}
@NonNull
@ -51,6 +56,11 @@ public class MediaRailAdapter extends RecyclerView.Adapter<MediaRailAdapter.Medi
return media.size();
}
@Override
public long getItemId(int position) {
return stableIdGenerator.getId(media.get(position));
}
public void setMedia(@NonNull List<Media> media) {
setMedia(media, activePosition);
}

View File

@ -13,6 +13,7 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.mms.GlideRequests;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.StableIdGenerator;
import java.util.ArrayList;
import java.util.Collection;
@ -27,6 +28,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
private final List<Media> media;
private final Set<Media> selected;
private final int maxSelection;
private final StableIdGenerator<Media> stableIdGenerator;
private boolean forcedMultiSelect;
@ -35,8 +37,10 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
this.eventListener = eventListener;
this.media = new ArrayList<>();
this.maxSelection = maxSelection;
this.stableIdGenerator = new StableIdGenerator<>();
this.selected = new TreeSet<>((m1, m2) -> {
if (m1.equals(m2)) return 0;
else if (Long.compare(m2.getDate(), m1.getDate()) == 0) return m2.getUri().compareTo(m1.getUri());
else return Long.compare(m2.getDate(), m1.getDate());
});
@ -65,7 +69,7 @@ public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItem
@Override
public long getItemId(int position) {
return media.get(position).getDate();
return stableIdGenerator.getId(media.get(position));
}
void setMedia(@NonNull List<Media> media) {

View File

@ -0,0 +1,31 @@
package org.thoughtcrime.securesms.util;
import android.support.annotation.MainThread;
import android.support.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
/**
* Useful for generate ID's to be used with
* {@link android.support.v7.widget.RecyclerView.Adapter#getItemId(int)} when you otherwise don't
* have a good way to generate an ID.
*/
public class StableIdGenerator<E> {
private final Map<E, Long> keys = new HashMap<>();
private long index = 1;
@MainThread
public long getId(@NonNull E item) {
if (keys.containsKey(item)) {
return keys.get(item);
}
long key = index++;
keys.put(item, key);
return key;
}
}