2017-01-03 10:30:27 +11:00
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2017-01-04 11:23:57 +11:00
|
|
|
import android.support.annotation.Nullable;
|
2017-01-10 17:14:27 +11:00
|
|
|
import android.support.v7.widget.PagerSnapHelper;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.text.Spanned;
|
2017-01-10 17:14:27 +11:00
|
|
|
import android.text.style.ImageSpan;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2017-01-08 09:24:02 +11:00
|
|
|
import android.widget.ImageButton;
|
2017-01-03 10:30:27 +11:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.android.volley.toolbox.ImageLoader;
|
|
|
|
import com.android.volley.toolbox.NetworkImageView;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class TimelineAdapter extends RecyclerView.Adapter {
|
|
|
|
private List<Status> statuses = new ArrayList<>();
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
StatusActionListener listener;
|
2017-01-03 10:30:27 +11:00
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public TimelineAdapter(StatusActionListener listener) {
|
2017-01-03 10:30:27 +11:00
|
|
|
super();
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
|
|
|
|
View v = LayoutInflater.from(viewGroup.getContext())
|
|
|
|
.inflate(R.layout.item_status, viewGroup, false);
|
|
|
|
return new ViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
|
|
|
|
ViewHolder holder = (ViewHolder) viewHolder;
|
|
|
|
Status status = statuses.get(position);
|
|
|
|
holder.setDisplayName(status.getDisplayName());
|
|
|
|
holder.setUsername(status.getUsername());
|
|
|
|
holder.setCreatedAt(status.getCreatedAt());
|
|
|
|
holder.setContent(status.getContent());
|
|
|
|
holder.setAvatar(status.getAvatar());
|
|
|
|
holder.setContent(status.getContent());
|
2017-01-08 09:24:02 +11:00
|
|
|
holder.setReblogged(status.getReblogged());
|
|
|
|
holder.setFavourited(status.getFavourited());
|
2017-01-03 10:30:27 +11:00
|
|
|
String rebloggedByUsername = status.getRebloggedByUsername();
|
|
|
|
if (rebloggedByUsername == null) {
|
2017-01-08 09:24:02 +11:00
|
|
|
holder.hideRebloggedByUsername();
|
2017-01-03 10:30:27 +11:00
|
|
|
} else {
|
|
|
|
holder.setRebloggedByUsername(rebloggedByUsername);
|
|
|
|
}
|
2017-01-11 07:41:50 +11:00
|
|
|
boolean sensitive = status.getSensitive();
|
|
|
|
holder.setMediaPreviews(status.getAttachments(), sensitive, listener);
|
|
|
|
if (!sensitive) {
|
|
|
|
holder.hideSensitiveMediaWarning();
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
holder.setupButtons(listener, position);
|
|
|
|
if (status.getVisibility() == Status.Visibility.PRIVATE) {
|
|
|
|
holder.disableReblogging();
|
|
|
|
}
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return statuses.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public int update(List<Status> new_statuses) {
|
|
|
|
int scrollToPosition;
|
|
|
|
if (statuses == null || statuses.isEmpty()) {
|
|
|
|
statuses = new_statuses;
|
|
|
|
scrollToPosition = 0;
|
|
|
|
} else {
|
|
|
|
int index = new_statuses.indexOf(statuses.get(0));
|
|
|
|
if (index == -1) {
|
|
|
|
statuses.addAll(0, new_statuses);
|
|
|
|
scrollToPosition = 0;
|
|
|
|
} else {
|
|
|
|
statuses.addAll(0, new_statuses.subList(0, index));
|
|
|
|
scrollToPosition = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyDataSetChanged();
|
|
|
|
return scrollToPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addItems(List<Status> new_statuses) {
|
|
|
|
int end = statuses.size();
|
|
|
|
statuses.addAll(new_statuses);
|
|
|
|
notifyItemRangeInserted(end, new_statuses.size());
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void removeItem(int position) {
|
|
|
|
statuses.remove(position);
|
|
|
|
notifyItemRemoved(position);
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:30:27 +11:00
|
|
|
public Status getItem(int position) {
|
|
|
|
return statuses.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
private TextView displayName;
|
|
|
|
private TextView username;
|
|
|
|
private TextView sinceCreated;
|
|
|
|
private TextView content;
|
|
|
|
private NetworkImageView avatar;
|
|
|
|
private ImageView boostedIcon;
|
|
|
|
private TextView boostedByUsername;
|
2017-01-08 09:24:02 +11:00
|
|
|
private ImageButton replyButton;
|
|
|
|
private ImageButton reblogButton;
|
|
|
|
private ImageButton favouriteButton;
|
|
|
|
private ImageButton moreButton;
|
|
|
|
private boolean favourited;
|
|
|
|
private boolean reblogged;
|
2017-01-10 17:14:27 +11:00
|
|
|
private NetworkImageView mediaPreview0;
|
|
|
|
private NetworkImageView mediaPreview1;
|
|
|
|
private NetworkImageView mediaPreview2;
|
|
|
|
private NetworkImageView mediaPreview3;
|
2017-01-11 07:41:50 +11:00
|
|
|
private View sensitiveMediaWarning;
|
2017-01-03 10:30:27 +11:00
|
|
|
|
|
|
|
public ViewHolder(View itemView) {
|
|
|
|
super(itemView);
|
|
|
|
displayName = (TextView) itemView.findViewById(R.id.status_display_name);
|
|
|
|
username = (TextView) itemView.findViewById(R.id.status_username);
|
|
|
|
sinceCreated = (TextView) itemView.findViewById(R.id.status_since_created);
|
|
|
|
content = (TextView) itemView.findViewById(R.id.status_content);
|
|
|
|
avatar = (NetworkImageView) itemView.findViewById(R.id.status_avatar);
|
|
|
|
boostedIcon = (ImageView) itemView.findViewById(R.id.status_boosted_icon);
|
|
|
|
boostedByUsername = (TextView) itemView.findViewById(R.id.status_boosted);
|
2017-01-08 09:24:02 +11:00
|
|
|
replyButton = (ImageButton) itemView.findViewById(R.id.status_reply);
|
|
|
|
reblogButton = (ImageButton) itemView.findViewById(R.id.status_reblog);
|
|
|
|
favouriteButton = (ImageButton) itemView.findViewById(R.id.status_favourite);
|
|
|
|
moreButton = (ImageButton) itemView.findViewById(R.id.status_more);
|
|
|
|
reblogged = false;
|
|
|
|
favourited = false;
|
2017-01-10 17:14:27 +11:00
|
|
|
mediaPreview0 = (NetworkImageView) itemView.findViewById(R.id.status_media_preview_0);
|
|
|
|
mediaPreview1 = (NetworkImageView) itemView.findViewById(R.id.status_media_preview_1);
|
|
|
|
mediaPreview2 = (NetworkImageView) itemView.findViewById(R.id.status_media_preview_2);
|
|
|
|
mediaPreview3 = (NetworkImageView) itemView.findViewById(R.id.status_media_preview_3);
|
2017-01-11 07:41:50 +11:00
|
|
|
sensitiveMediaWarning = itemView.findViewById(R.id.status_sensitive_media_warning);
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setDisplayName(String name) {
|
|
|
|
displayName.setText(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUsername(String name) {
|
|
|
|
Context context = username.getContext();
|
|
|
|
String format = context.getString(R.string.status_username_format);
|
|
|
|
String usernameText = String.format(format, name);
|
|
|
|
username.setText(usernameText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setContent(Spanned content) {
|
|
|
|
this.content.setText(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAvatar(String url) {
|
|
|
|
Context context = avatar.getContext();
|
|
|
|
ImageLoader imageLoader = VolleySingleton.getInstance(context).getImageLoader();
|
|
|
|
avatar.setImageUrl(url, imageLoader);
|
|
|
|
avatar.setDefaultImageResId(R.drawable.avatar_default);
|
|
|
|
avatar.setErrorImageResId(R.drawable.avatar_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is a rough duplicate of android.text.format.DateUtils.getRelativeTimeSpanString,
|
|
|
|
* but even with the FORMAT_ABBREV_RELATIVE flag it wasn't abbreviating enough. */
|
|
|
|
private String getRelativeTimeSpanString(long then, long now) {
|
|
|
|
final long MINUTE = 60;
|
|
|
|
final long HOUR = 60 * MINUTE;
|
|
|
|
final long DAY = 24 * HOUR;
|
|
|
|
final long YEAR = 365 * DAY;
|
|
|
|
long span = (now - then) / 1000;
|
|
|
|
String prefix = "";
|
|
|
|
if (span < 0) {
|
|
|
|
prefix = "in ";
|
|
|
|
span = -span;
|
|
|
|
}
|
|
|
|
String unit;
|
|
|
|
if (span < MINUTE) {
|
|
|
|
unit = "s";
|
|
|
|
} else if (span < HOUR) {
|
|
|
|
span /= MINUTE;
|
|
|
|
unit = "m";
|
|
|
|
} else if (span < DAY) {
|
|
|
|
span /= HOUR;
|
|
|
|
unit = "h";
|
|
|
|
} else if (span < YEAR) {
|
|
|
|
span /= DAY;
|
|
|
|
unit = "d";
|
|
|
|
} else {
|
|
|
|
span /= YEAR;
|
|
|
|
unit = "y";
|
|
|
|
}
|
|
|
|
return prefix + span + unit;
|
|
|
|
}
|
|
|
|
|
2017-01-04 11:23:57 +11:00
|
|
|
public void setCreatedAt(@Nullable Date createdAt) {
|
|
|
|
String readout;
|
|
|
|
if (createdAt != null) {
|
|
|
|
long then = createdAt.getTime();
|
|
|
|
long now = new Date().getTime();
|
|
|
|
readout = getRelativeTimeSpanString(then, now);
|
|
|
|
} else {
|
2017-01-08 09:24:02 +11:00
|
|
|
readout = "?m"; // unknown minutes~
|
2017-01-04 11:23:57 +11:00
|
|
|
}
|
|
|
|
sinceCreated.setText(readout);
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setRebloggedByUsername(String name) {
|
|
|
|
Context context = boostedByUsername.getContext();
|
|
|
|
String format = context.getString(R.string.status_boosted_format);
|
|
|
|
String boostedText = String.format(format, name);
|
|
|
|
boostedByUsername.setText(boostedText);
|
|
|
|
boostedIcon.setVisibility(View.VISIBLE);
|
|
|
|
boostedByUsername.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void hideRebloggedByUsername() {
|
2017-01-03 10:30:27 +11:00
|
|
|
boostedIcon.setVisibility(View.GONE);
|
|
|
|
boostedByUsername.setVisibility(View.GONE);
|
|
|
|
}
|
2017-01-08 09:24:02 +11:00
|
|
|
|
|
|
|
public void setReblogged(boolean reblogged) {
|
|
|
|
this.reblogged = reblogged;
|
|
|
|
if (!reblogged) {
|
|
|
|
reblogButton.setImageResource(R.drawable.ic_reblog_off);
|
|
|
|
} else {
|
|
|
|
reblogButton.setImageResource(R.drawable.ic_reblog_on);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void disableReblogging() {
|
|
|
|
reblogButton.setEnabled(false);
|
|
|
|
reblogButton.setImageResource(R.drawable.ic_reblog_disabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFavourited(boolean favourited) {
|
|
|
|
this.favourited = favourited;
|
|
|
|
if (!favourited) {
|
|
|
|
favouriteButton.setImageResource(R.drawable.ic_favourite_off);
|
|
|
|
} else {
|
|
|
|
favouriteButton.setImageResource(R.drawable.ic_favourite_on);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 17:14:27 +11:00
|
|
|
public void setMediaPreviews(final Status.MediaAttachment[] attachments,
|
2017-01-11 07:41:50 +11:00
|
|
|
boolean sensitive, final StatusActionListener listener) {
|
2017-01-10 17:14:27 +11:00
|
|
|
final NetworkImageView[] previews = {
|
|
|
|
mediaPreview0,
|
|
|
|
mediaPreview1,
|
|
|
|
mediaPreview2,
|
|
|
|
mediaPreview3
|
|
|
|
};
|
|
|
|
Context context = mediaPreview0.getContext();
|
|
|
|
ImageLoader imageLoader = VolleySingleton.getInstance(context).getImageLoader();
|
2017-01-11 07:41:50 +11:00
|
|
|
final int n = Math.min(attachments.length, Status.MAX_MEDIA_ATTACHMENTS);
|
2017-01-10 17:14:27 +11:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
String previewUrl = attachments[i].getPreviewUrl();
|
|
|
|
previews[i].setImageUrl(previewUrl, imageLoader);
|
2017-01-11 07:41:50 +11:00
|
|
|
if (!sensitive) {
|
|
|
|
previews[i].setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
previews[i].setVisibility(View.GONE);
|
|
|
|
}
|
2017-01-10 17:14:27 +11:00
|
|
|
final String url = attachments[i].getUrl();
|
|
|
|
final Status.MediaAttachment.Type type = attachments[i].getType();
|
|
|
|
previews[i].setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onViewMedia(url, type);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-11 07:41:50 +11:00
|
|
|
if (sensitive) {
|
|
|
|
sensitiveMediaWarning.setVisibility(View.VISIBLE);
|
|
|
|
sensitiveMediaWarning.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
v.setVisibility(View.GONE);
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
previews[i].setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
v.setOnClickListener(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-10 17:14:27 +11:00
|
|
|
// Hide any of the placeholder previews beyond the ones set.
|
|
|
|
for (int i = n; i < Status.MAX_MEDIA_ATTACHMENTS; i++) {
|
|
|
|
previews[i].setImageUrl(null, imageLoader);
|
|
|
|
previews[i].setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-11 07:41:50 +11:00
|
|
|
public void hideSensitiveMediaWarning() {
|
|
|
|
sensitiveMediaWarning.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2017-01-08 09:24:02 +11:00
|
|
|
public void setupButtons(final StatusActionListener listener, final int position) {
|
|
|
|
reblogButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onReblog(!reblogged, position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
favouriteButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onFavourite(!favourited, position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
moreButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
listener.onMore(v, position);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-01-03 10:30:27 +11:00
|
|
|
}
|
|
|
|
}
|