Load sensitive media previews only when shown (#1439)

main
Ivan Kupalov 5 years ago committed by Konrad Pozniak
parent 3c7010e671
commit 01646fd8bf
  1. 83
      app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java

@ -29,11 +29,11 @@ import com.keylesspalace.tusky.entity.Emoji;
import com.keylesspalace.tusky.entity.Status; import com.keylesspalace.tusky.entity.Status;
import com.keylesspalace.tusky.interfaces.StatusActionListener; import com.keylesspalace.tusky.interfaces.StatusActionListener;
import com.keylesspalace.tusky.util.CustomEmojiHelper; import com.keylesspalace.tusky.util.CustomEmojiHelper;
import com.keylesspalace.tusky.util.TimestampUtils;
import com.keylesspalace.tusky.util.HtmlUtils; import com.keylesspalace.tusky.util.HtmlUtils;
import com.keylesspalace.tusky.util.ImageLoadingHelper; import com.keylesspalace.tusky.util.ImageLoadingHelper;
import com.keylesspalace.tusky.util.LinkHelper; import com.keylesspalace.tusky.util.LinkHelper;
import com.keylesspalace.tusky.util.ThemeUtils; import com.keylesspalace.tusky.util.ThemeUtils;
import com.keylesspalace.tusky.util.TimestampUtils;
import com.keylesspalace.tusky.view.MediaPreviewImageView; import com.keylesspalace.tusky.view.MediaPreviewImageView;
import com.keylesspalace.tusky.viewdata.PollOptionViewData; import com.keylesspalace.tusky.viewdata.PollOptionViewData;
import com.keylesspalace.tusky.viewdata.PollViewData; import com.keylesspalace.tusky.viewdata.PollViewData;
@ -92,6 +92,8 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
private int avatarRadius36dp; private int avatarRadius36dp;
private int avatarRadius24dp; private int avatarRadius24dp;
private final int mediaPreviewUnloadedId;
protected StatusBaseViewHolder(View itemView, protected StatusBaseViewHolder(View itemView,
boolean useAbsoluteTime) { boolean useAbsoluteTime) {
super(itemView); super(itemView);
@ -145,6 +147,9 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
this.avatarRadius48dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_48dp); this.avatarRadius48dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_48dp);
this.avatarRadius36dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_36dp); this.avatarRadius36dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_36dp);
this.avatarRadius24dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_24dp); this.avatarRadius24dp = itemView.getContext().getResources().getDimensionPixelSize(R.dimen.avatar_radius_24dp);
mediaPreviewUnloadedId = ThemeUtils.getDrawableId(itemView.getContext(),
R.attr.media_preview_unloaded_drawable, android.R.color.black);
} }
protected abstract int getMediaPreviewHeight(Context context); protected abstract int getMediaPreviewHeight(Context context);
@ -329,66 +334,71 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
favouriteButton.setChecked(favourited); favouriteButton.setChecked(favourited);
} }
protected void setMediaPreviews(final List<Attachment> attachments, boolean sensitive, private void loadImage(MediaPreviewImageView imageView, String previewUrl, String description,
final StatusActionListener listener, boolean showingContent) { MetaData meta) {
Context context = itemView.getContext();
int mediaPreviewUnloadedId =
ThemeUtils.getDrawableId(itemView.getContext(), R.attr.media_preview_unloaded_drawable,
android.R.color.black);
final int n = Math.min(attachments.size(), Status.MAX_MEDIA_ATTACHMENTS);
for (int i = 0; i < n; i++) {
String previewUrl = attachments.get(i).getPreviewUrl();
String description = attachments.get(i).getDescription();
if (TextUtils.isEmpty(description)) {
mediaPreviews[i].setContentDescription(context.getString(R.string.action_view_media));
} else {
mediaPreviews[i].setContentDescription(description);
}
mediaPreviews[i].setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(previewUrl)) { if (TextUtils.isEmpty(previewUrl)) {
Glide.with(mediaPreviews[i]) Glide.with(imageView)
.load(mediaPreviewUnloadedId) .load(mediaPreviewUnloadedId)
.centerInside() .centerInside()
.into(mediaPreviews[i]); .into(imageView);
} else { } else {
MetaData meta = attachments.get(i).getMeta();
Focus focus = meta != null ? meta.getFocus() : null; Focus focus = meta != null ? meta.getFocus() : null;
if (focus != null) { // If there is a focal point for this attachment: if (focus != null) { // If there is a focal point for this attachment:
mediaPreviews[i].setFocalPoint(focus); imageView.setFocalPoint(focus);
Glide.with(mediaPreviews[i]) Glide.with(imageView)
.load(previewUrl) .load(previewUrl)
.placeholder(mediaPreviewUnloadedId) .placeholder(mediaPreviewUnloadedId)
.centerInside() .centerInside()
.addListener(mediaPreviews[i]) .addListener(imageView)
.into(mediaPreviews[i]); .into(imageView);
} else { } else {
mediaPreviews[i].removeFocalPoint(); imageView.removeFocalPoint();
Glide.with(mediaPreviews[i]) Glide.with(imageView)
.load(previewUrl) .load(previewUrl)
.placeholder(mediaPreviewUnloadedId) .placeholder(mediaPreviewUnloadedId)
.centerInside() .centerInside()
.into(mediaPreviews[i]); .into(imageView);
} }
} }
}
protected void setMediaPreviews(final List<Attachment> attachments, boolean sensitive,
final StatusActionListener listener, boolean showingContent) {
Context context = itemView.getContext();
final int n = Math.min(attachments.size(), Status.MAX_MEDIA_ATTACHMENTS);
for (int i = 0; i < n; i++) {
String previewUrl = attachments.get(i).getPreviewUrl();
String description = attachments.get(i).getDescription();
MediaPreviewImageView imageView = mediaPreviews[i];
imageView.setVisibility(View.VISIBLE);
if (TextUtils.isEmpty(description)) {
imageView.setContentDescription(imageView.getContext()
.getString(R.string.action_view_media));
} else {
imageView.setContentDescription(description);
}
if (!sensitive || showingContent) {
loadImage(imageView, previewUrl, description, attachments.get(i).getMeta());
} else {
imageView.setImageResource(mediaPreviewUnloadedId);
}
final Attachment.Type type = attachments.get(i).getType(); final Attachment.Type type = attachments.get(i).getType();
if (type == Attachment.Type.VIDEO | type == Attachment.Type.GIFV) { if (type == Attachment.Type.VIDEO || type == Attachment.Type.GIFV) {
mediaOverlays[i].setVisibility(View.VISIBLE); mediaOverlays[i].setVisibility(View.VISIBLE);
} else { } else {
mediaOverlays[i].setVisibility(View.GONE); mediaOverlays[i].setVisibility(View.GONE);
} }
setAttachmentClickListener(mediaPreviews[i], listener, i, attachments.get(i), true); setAttachmentClickListener(imageView, listener, i, attachments.get(i), true);
}
final int mediaPreviewHeight = getMediaPreviewHeight(context); final int mediaPreviewHeight = getMediaPreviewHeight(context);
@ -401,7 +411,6 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
mediaPreviews[2].getLayoutParams().height = mediaPreviewHeight; mediaPreviews[2].getLayoutParams().height = mediaPreviewHeight;
mediaPreviews[3].getLayoutParams().height = mediaPreviewHeight; mediaPreviews[3].getLayoutParams().height = mediaPreviewHeight;
} }
}
final String hiddenContentText; final String hiddenContentText;
if (sensitive) { if (sensitive) {

Loading…
Cancel
Save