session-android/src/org/thoughtcrime/securesms/mediasend/MediaPickerItemAdapter.java

173 lines
5.9 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.mediasend;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
2019-03-19 11:10:43 -07:00
import android.widget.TextView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
2019-07-24 12:30:23 +10:00
import network.loki.messenger.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;
2019-03-19 11:10:43 -07:00
import java.util.LinkedList;
import java.util.List;
public class MediaPickerItemAdapter extends RecyclerView.Adapter<MediaPickerItemAdapter.ItemViewHolder> {
private final GlideRequests glideRequests;
private final EventListener eventListener;
private final List<Media> media;
2019-03-19 11:10:43 -07:00
private final List<Media> selected;
private final int maxSelection;
private final StableIdGenerator<Media> stableIdGenerator;
private boolean forcedMultiSelect;
public MediaPickerItemAdapter(@NonNull GlideRequests glideRequests, @NonNull EventListener eventListener, int maxSelection) {
this.glideRequests = glideRequests;
this.eventListener = eventListener;
this.media = new ArrayList<>();
this.maxSelection = maxSelection;
this.stableIdGenerator = new StableIdGenerator<>();
2019-03-19 11:10:43 -07:00
this.selected = new LinkedList<>();
setHasStableIds(true);
}
@Override
public @NonNull ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ItemViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.mediapicker_media_item, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int i) {
holder.bind(media.get(i), forcedMultiSelect, selected, maxSelection, glideRequests, eventListener);
}
@Override
public void onViewRecycled(@NonNull ItemViewHolder holder) {
holder.recycle();
}
@Override
public int getItemCount() {
return media.size();
}
@Override
public long getItemId(int position) {
return stableIdGenerator.getId(media.get(position));
}
void setMedia(@NonNull List<Media> media) {
this.media.clear();
this.media.addAll(media);
notifyDataSetChanged();
}
void setSelected(@NonNull Collection<Media> selected) {
this.selected.clear();
this.selected.addAll(selected);
notifyDataSetChanged();
}
2019-03-19 11:10:43 -07:00
List<Media> getSelected() {
return selected;
}
void setForcedMultiSelect(boolean forcedMultiSelect) {
this.forcedMultiSelect = forcedMultiSelect;
notifyDataSetChanged();
}
static class ItemViewHolder extends RecyclerView.ViewHolder {
private final ImageView thumbnail;
private final View playOverlay;
2019-03-01 10:50:48 -08:00
private final View selectOn;
private final View selectOff;
private final View selectOverlay;
2019-03-19 11:10:43 -07:00
private final TextView selectOrder;
ItemViewHolder(@NonNull View itemView) {
super(itemView);
2019-03-01 10:50:48 -08:00
thumbnail = itemView.findViewById(R.id.mediapicker_image_item_thumbnail);
playOverlay = itemView.findViewById(R.id.mediapicker_play_overlay);
selectOn = itemView.findViewById(R.id.mediapicker_select_on);
selectOff = itemView.findViewById(R.id.mediapicker_select_off);
selectOverlay = itemView.findViewById(R.id.mediapicker_select_overlay);
2019-03-19 11:10:43 -07:00
selectOrder = itemView.findViewById(R.id.mediapicker_select_order);
}
2019-03-19 11:10:43 -07:00
void bind(@NonNull Media media, boolean multiSelect, List<Media> selected, int maxSelection, @NonNull GlideRequests glideRequests, @NonNull EventListener eventListener) {
glideRequests.load(media.getUri())
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transition(DrawableTransitionOptions.withCrossFade())
.into(thumbnail);
playOverlay.setVisibility(MediaUtil.isVideoType(media.getMimeType()) ? View.VISIBLE : View.GONE);
if (selected.isEmpty() && !multiSelect) {
itemView.setOnClickListener(v -> eventListener.onMediaChosen(media));
2019-03-01 10:50:48 -08:00
selectOn.setVisibility(View.GONE);
selectOff.setVisibility(View.GONE);
selectOverlay.setVisibility(View.GONE);
if (maxSelection > 1) {
itemView.setOnLongClickListener(v -> {
selected.add(media);
eventListener.onMediaSelectionStarted();
eventListener.onMediaSelectionChanged(new ArrayList<>(selected));
return true;
});
}
} else if (selected.contains(media)) {
2019-03-01 10:50:48 -08:00
selectOff.setVisibility(View.VISIBLE);
selectOn.setVisibility(View.VISIBLE);
selectOverlay.setVisibility(View.VISIBLE);
2019-03-19 11:10:43 -07:00
selectOrder.setText(String.valueOf(selected.indexOf(media) + 1));
itemView.setOnLongClickListener(null);
itemView.setOnClickListener(v -> {
selected.remove(media);
eventListener.onMediaSelectionChanged(new ArrayList<>(selected));
});
} else {
2019-03-01 10:50:48 -08:00
selectOff.setVisibility(View.VISIBLE);
selectOn.setVisibility(View.GONE);
selectOverlay.setVisibility(View.GONE);
itemView.setOnLongClickListener(null);
itemView.setOnClickListener(v -> {
if (selected.size() < maxSelection) {
selected.add(media);
eventListener.onMediaSelectionChanged(new ArrayList<>(selected));
} else {
eventListener.onMediaSelectionOverflow(maxSelection);
}
});
}
}
void recycle() {
itemView.setOnClickListener(null);
}
2019-03-19 11:10:43 -07:00
}
interface EventListener {
void onMediaChosen(@NonNull Media media);
void onMediaSelectionStarted();
void onMediaSelectionChanged(@NonNull List<Media> media);
void onMediaSelectionOverflow(int maxSelection);
}
}