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.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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 19:22:48 -08:00
|
|
|
public void setCursor(@NonNull GlideRequests glideRequests, @Nullable Cursor cursor) {
|
|
|
|
this.recyclerView.setAdapter(new ThreadPhotoRailAdapter(getContext(), 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> {
|
|
|
|
|
2018-02-01 19:22:48 -08:00
|
|
|
@SuppressWarnings("unused")
|
2018-12-06 12:14:20 -08:00
|
|
|
private static final String TAG = ThreadPhotoRailAdapter.class.getSimpleName();
|
2017-09-20 18:14:28 -07:00
|
|
|
|
2017-10-16 13:11:42 -07:00
|
|
|
@NonNull private final GlideRequests glideRequests;
|
2017-09-20 18:14:28 -07:00
|
|
|
|
|
|
|
@Nullable private OnItemClickedListener clickedListener;
|
|
|
|
|
|
|
|
private ThreadPhotoRailAdapter(@NonNull Context context,
|
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);
|
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;
|
2018-01-24 19:17:44 -08:00
|
|
|
MediaDatabase.MediaRecord mediaRecord = MediaDatabase.MediaRecord.from(getContext(), cursor);
|
2017-09-20 18:14:28 -07:00
|
|
|
Slide slide = MediaUtil.getSlideForAttachment(getContext(), mediaRecord.getAttachment());
|
|
|
|
|
|
|
|
if (slide != null) {
|
2018-01-24 19:17:44 -08:00
|
|
|
imageView.setImageResource(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 {
|
2018-02-01 19:22:48 -08:00
|
|
|
void onItemClicked(MediaDatabase.MediaRecord mediaRecord);
|
2017-09-20 18:14:28 -07:00
|
|
|
}
|
|
|
|
}
|