2017-09-20 18:14:28 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.support.v7.widget.DefaultItemAnimator;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.FrameLayout;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.R;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.CursorRecyclerViewAdapter;
|
|
|
|
import org.thoughtcrime.securesms.database.MediaDatabase;
|
2017-10-16 13:11:42 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.GlideRequests;
|
2017-09-20 18:14:28 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.Slide;
|
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.ViewUtil;
|
|
|
|
|
|
|
|
public class ThreadPhotoRailView extends FrameLayout {
|
|
|
|
|
|
|
|
@NonNull private final RecyclerView recyclerView;
|
|
|
|
@Nullable private OnItemClickedListener listener;
|
|
|
|
|
|
|
|
public ThreadPhotoRailView(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ThreadPhotoRailView(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ThreadPhotoRailView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
|
|
|
|
inflate(context, R.layout.recipient_preference_photo_rail, this);
|
|
|
|
|
|
|
|
this.recyclerView = ViewUtil.findById(this, R.id.photo_list);
|
|
|
|
this.recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
|
|
|
|
this.recyclerView.setItemAnimator(new DefaultItemAnimator());
|
2017-10-08 17:36:35 -07:00
|
|
|
this.recyclerView.setNestedScrollingEnabled(false);
|
2017-09-20 18:14:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setListener(@Nullable OnItemClickedListener listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
|
|
|
|
if (this.recyclerView.getAdapter() != null) {
|
|
|
|
((ThreadPhotoRailAdapter)this.recyclerView.getAdapter()).setListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
public void setCursor(@NonNull MasterSecret masterSecret, @NonNull GlideRequests glideRequests, @Nullable Cursor cursor) {
|
|
|
|
this.recyclerView.setAdapter(new ThreadPhotoRailAdapter(getContext(), masterSecret, glideRequests, cursor, this.listener));
|
2017-09-20 18:14:28 -07:00
|
|
|
}
|
2017-09-21 10:03:05 -07:00
|
|
|
|
2017-09-20 18:14:28 -07:00
|
|
|
private static class ThreadPhotoRailAdapter extends CursorRecyclerViewAdapter<ThreadPhotoRailAdapter.ThreadPhotoViewHolder> {
|
|
|
|
|
|
|
|
private static final String TAG = ThreadPhotoRailAdapter.class.getName();
|
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
@NonNull private final MasterSecret masterSecret;
|
|
|
|
@NonNull private final GlideRequests glideRequests;
|
2017-09-20 18:14:28 -07:00
|
|
|
|
|
|
|
@Nullable private OnItemClickedListener clickedListener;
|
|
|
|
|
|
|
|
private ThreadPhotoRailAdapter(@NonNull Context context,
|
|
|
|
@NonNull MasterSecret masterSecret,
|
2017-10-16 13:11:42 -07:00
|
|
|
@NonNull GlideRequests glideRequests,
|
|
|
|
@Nullable Cursor cursor,
|
2017-09-20 18:14:28 -07:00
|
|
|
@Nullable OnItemClickedListener listener)
|
|
|
|
{
|
|
|
|
super(context, cursor);
|
|
|
|
this.masterSecret = masterSecret;
|
2017-10-16 13:11:42 -07:00
|
|
|
this.glideRequests = glideRequests;
|
2017-09-20 18:14:28 -07:00
|
|
|
this.clickedListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ThreadPhotoViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
|
|
|
|
View itemView = LayoutInflater.from(parent.getContext())
|
|
|
|
.inflate(R.layout.recipient_preference_photo_rail_item, parent, false);
|
|
|
|
|
|
|
|
return new ThreadPhotoViewHolder(itemView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindItemViewHolder(ThreadPhotoViewHolder viewHolder, @NonNull Cursor cursor) {
|
|
|
|
ThumbnailView imageView = viewHolder.imageView;
|
|
|
|
MediaDatabase.MediaRecord mediaRecord = MediaDatabase.MediaRecord.from(getContext(), masterSecret, cursor);
|
|
|
|
Slide slide = MediaUtil.getSlideForAttachment(getContext(), mediaRecord.getAttachment());
|
|
|
|
|
|
|
|
if (slide != null) {
|
2017-10-16 13:11:42 -07:00
|
|
|
imageView.setImageResource(masterSecret, glideRequests, slide, false, false);
|
2017-09-20 18:14:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
imageView.setOnClickListener(v -> {
|
|
|
|
if (clickedListener != null) clickedListener.onItemClicked(mediaRecord);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setListener(@Nullable OnItemClickedListener listener) {
|
|
|
|
this.clickedListener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class ThreadPhotoViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
ThumbnailView imageView;
|
|
|
|
|
|
|
|
ThreadPhotoViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
|
|
|
|
this.imageView = ViewUtil.findById(itemView, R.id.thumbnail);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnItemClickedListener {
|
|
|
|
public void onItemClicked(MediaDatabase.MediaRecord mediaRecord);
|
|
|
|
}
|
|
|
|
}
|