Show low-resolution images in compact layout. Send notifications of messages that are received after the last notification.

This commit is contained in:
Alex Ning 2019-11-12 11:51:08 +08:00
parent 74ae9a8b68
commit 0aaa63b6f6
7 changed files with 83 additions and 48 deletions

View File

@ -1251,7 +1251,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
private void loadImage(final RecyclerView.ViewHolder holder, final Post post) { private void loadImage(final RecyclerView.ViewHolder holder, final Post post) {
if (holder instanceof PostViewHolder) { if (holder instanceof PostViewHolder) {
RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() {
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
((PostViewHolder) holder).progressBar.setVisibility(View.GONE); ((PostViewHolder) holder).progressBar.setVisibility(View.GONE);
@ -1279,9 +1279,24 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
imageRequestBuilder.into(((PostViewHolder) holder).imageView); imageRequestBuilder.into(((PostViewHolder) holder).imageView);
} }
} else if (holder instanceof PostCompactViewHolder) { } else if (holder instanceof PostCompactViewHolder) {
RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).error(R.drawable.ic_error_outline_black_24dp); String previewUrl = post.getThumbnailPreviewUrl().equals("") ? post.getPreviewUrl() : post.getThumbnailPreviewUrl();
RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(previewUrl)
.error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE);
return false;
}
});
if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2))) imageRequestBuilder
.transform(new BlurTransformation(50, 2))
.into(((PostCompactViewHolder) holder).imageView); .into(((PostCompactViewHolder) holder).imageView);
} else { } else {
imageRequestBuilder.into(((PostCompactViewHolder) holder).imageView); imageRequestBuilder.into(((PostCompactViewHolder) holder).imageView);

View File

@ -88,4 +88,5 @@ public class JSONUtils {
public static final String NUM_COMMENTS_KEY = "num_comments"; public static final String NUM_COMMENTS_KEY = "num_comments";
public static final String HIDDEN_KEY = "hidden"; public static final String HIDDEN_KEY = "hidden";
public static final String USER_HAS_FAVORITED_KEY = "user_has_favorited"; public static final String USER_HAS_FAVORITED_KEY = "user_has_favorited";
public static final String RESOLUTIONS_KEY = "resolutions";
} }

View File

@ -66,39 +66,48 @@ public class ParsePost {
String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString(); String permalink = Html.fromHtml(data.getString(JSONUtils.PERMALINK_KEY)).toString();
String previewUrl = ""; String previewUrl = "";
String thumbnailPreviewUrl = "";
int previewWidth = -1; int previewWidth = -1;
int previewHeight = -1; int previewHeight = -1;
if (data.has(JSONUtils.PREVIEW_KEY)) { if (data.has(JSONUtils.PREVIEW_KEY)) {
previewUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
previewWidth = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY);
.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY); int thumbnailPreviewsLength = thumbnailPreviews.length();
previewHeight = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0) if (thumbnailPreviewsLength > 0) {
.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY); if (thumbnailPreviewsLength >= 3) {
thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(2).getString(JSONUtils.URL_KEY);
} else {
thumbnailPreviewUrl = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY).getJSONObject(0).getString(JSONUtils.URL_KEY);
}
}
previewWidth = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
previewHeight = images.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY);
} }
if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { if (data.has(JSONUtils.CROSSPOST_PARENT_LIST)) {
//Cross post //Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
Post crosspostParent = parseBasicData(data, locale); Post crosspostParent = parseBasicData(data, locale);
Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, author, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, previewWidth,
score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, previewHeight, score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, true); stickied, archived, locked, saved, true);
post.setCrosspostParentId(crosspostParent.getId()); post.setCrosspostParentId(crosspostParent.getId());
return post; return post;
} else { } else {
return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, author, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, previewWidth,
score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, previewHeight, score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, false); stickied, archived, locked, saved, false);
} }
} }
private static Post parseData(JSONObject data, String permalink, String id, String fullName, private static Post parseData(JSONObject data, String permalink, String id, String fullName,
String subredditName, String subredditNamePrefixed, String author, String subredditName, String subredditNamePrefixed, String author,
String formattedPostTime, String title, String previewUrl, int previewWidth, String formattedPostTime, String title, String previewUrl,
int previewHeight, int score, int voteType, int gilded, int nComments, String thumbnailPreviewUrl, int previewWidth, int previewHeight,
String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, int score, int voteType, int gilded, int nComments, String flair,
boolean hidden, boolean spoiler, boolean nsfw, boolean stickied,
boolean archived, boolean locked, boolean saved, boolean isCrosspost) throws JSONException { boolean archived, boolean locked, boolean saved, boolean isCrosspost) throws JSONException {
Post post; Post post;
@ -131,8 +140,8 @@ public class ParsePost {
//No preview link post //No preview link post
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType, voteType, gilded, nComments, title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, gilded,
flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText(""); post.setSelfText("");
} else { } else {
@ -152,8 +161,8 @@ public class ParsePost {
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, permalink, score, postType, voteType, gilded, nComments, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, gilded,
flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
@ -166,9 +175,9 @@ public class ParsePost {
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString(); .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, permalink, score, postType, voteType, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType,
gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
locked, saved, isCrosspost); archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
@ -178,9 +187,9 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, url, url, permalink, score, postType, formattedPostTime, title, url, thumbnailPreviewUrl, url, permalink, score,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, isCrosspost); stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
@ -188,9 +197,9 @@ public class ParsePost {
//Gif post //Gif post
int postType = Post.GIF_TYPE; int postType = Post.GIF_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, isCrosspost); stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
@ -228,9 +237,9 @@ public class ParsePost {
int postType = Post.LINK_TYPE; int postType = Post.LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, isCrosspost); stickied, archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) { if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText(""); post.setSelfText("");
} else { } else {
@ -248,9 +257,9 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
archived, locked, saved, isCrosspost); stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
} else { } else {
@ -258,9 +267,9 @@ public class ParsePost {
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, url, url, permalink, score, postType, voteType, formattedPostTime, title, url, thumbnailPreviewUrl, url, permalink, score, postType,
gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived,
saved, isCrosspost); locked, saved, isCrosspost);
} }
} }
} }

View File

@ -39,6 +39,7 @@ public class Post implements Parcelable {
private String selfText; private String selfText;
private String selfTextPlainTrimmed; private String selfTextPlainTrimmed;
private String previewUrl; private String previewUrl;
private String thumbnailPreviewUrl;
private String url; private String url;
private String videoUrl; private String videoUrl;
private String permalink; private String permalink;
@ -61,7 +62,7 @@ public class Post implements Parcelable {
private String crosspostParentId; private String crosspostParentId;
public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author,
String postTime, String title, String previewUrl, String permalink, int score, int postType, String postTime, String title, String previewUrl, String thumbnailPreviewUrl, String permalink, int score, int postType,
int voteType, int gilded, int nComments, String flair, boolean hidden, boolean spoiler, int voteType, int gilded, int nComments, String flair, boolean hidden, boolean spoiler,
boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) {
this.id = id; this.id = id;
@ -73,6 +74,7 @@ public class Post implements Parcelable {
this.postTime = postTime; this.postTime = postTime;
this.title = title; this.title = title;
this.previewUrl = previewUrl; this.previewUrl = previewUrl;
this.thumbnailPreviewUrl = thumbnailPreviewUrl;
this.permalink = RedditUtils.API_BASE_URI + permalink; this.permalink = RedditUtils.API_BASE_URI + permalink;
this.score = score; this.score = score;
this.postType = postType; this.postType = postType;
@ -91,7 +93,7 @@ public class Post implements Parcelable {
} }
public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, public Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author,
String postTime, String title, String previewUrl, String url, String permalink, int score, String postTime, String title, String previewUrl, String thumbnailPreviewUrl, String url, String permalink, int score,
int postType, int voteType, int gilded, int nComments, String flair, boolean hidden, int postType, int voteType, int gilded, int nComments, String flair, boolean hidden,
boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked,
boolean saved, boolean isCrosspost) { boolean saved, boolean isCrosspost) {
@ -104,6 +106,7 @@ public class Post implements Parcelable {
this.postTime = postTime; this.postTime = postTime;
this.title = title; this.title = title;
this.previewUrl = previewUrl; this.previewUrl = previewUrl;
this.thumbnailPreviewUrl = thumbnailPreviewUrl;
this.url = url; this.url = url;
this.permalink = RedditUtils.API_BASE_URI + permalink; this.permalink = RedditUtils.API_BASE_URI + permalink;
this.score = score; this.score = score;
@ -165,6 +168,7 @@ public class Post implements Parcelable {
selfText = in.readString(); selfText = in.readString();
selfTextPlainTrimmed = in.readString(); selfTextPlainTrimmed = in.readString();
previewUrl = in.readString(); previewUrl = in.readString();
thumbnailPreviewUrl = in.readString();
url = in.readString(); url = in.readString();
videoUrl = in.readString(); videoUrl = in.readString();
permalink = in.readString(); permalink = in.readString();
@ -259,6 +263,10 @@ public class Post implements Parcelable {
return previewUrl; return previewUrl;
} }
public String getThumbnailPreviewUrl() {
return thumbnailPreviewUrl;
}
public String getUrl() { public String getUrl() {
return url; return url;
} }
@ -407,6 +415,7 @@ public class Post implements Parcelable {
parcel.writeString(selfText); parcel.writeString(selfText);
parcel.writeString(selfTextPlainTrimmed); parcel.writeString(selfTextPlainTrimmed);
parcel.writeString(previewUrl); parcel.writeString(previewUrl);
parcel.writeString(thumbnailPreviewUrl);
parcel.writeString(url); parcel.writeString(url);
parcel.writeString(videoUrl); parcel.writeString(videoUrl);
parcel.writeString(permalink); parcel.writeString(permalink);

View File

@ -80,15 +80,15 @@ public class PullNotificationWorker extends Worker {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
int messageSize = messages.size() >= 5 ? 5 : messages.size(); int messageSize = messages.size() >= 5 ? 5 : messages.size();
long currentTime = Calendar.getInstance().getTimeInMillis(); long lastNotificationTime = mSharedPreferences.getLong(SharedPreferencesUtils.PULL_NOTIFICATION_TIME, -1L);
long notificationInterval = Long.parseLong(
mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"))
* 1000 * 60 * 60;
boolean hasValidMessage = false; boolean hasValidMessage = false;
long currentTime = Calendar.getInstance().getTimeInMillis();
mSharedPreferences.edit().putLong(SharedPreferencesUtils.PULL_NOTIFICATION_TIME, currentTime).apply();
for (int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) { for (int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) {
Message message = messages.get(messageIndex); Message message = messages.get(messageIndex);
if (currentTime - message.getTimeUTC() > notificationInterval) { if (message.getTimeUTC() <= lastNotificationTime) {
continue; continue;
} }

View File

@ -55,4 +55,5 @@ public class SharedPreferencesUtils {
public static final String POST_LAYOUT_SEARCH_POST = "post_layout_search_post"; public static final String POST_LAYOUT_SEARCH_POST = "post_layout_search_post";
public static final int POST_LAYOUT_CARD = 0; public static final int POST_LAYOUT_CARD = 0;
public static final int POST_LAYOUT_COMPACT = 1; public static final int POST_LAYOUT_COMPACT = 1;
public static final String PULL_NOTIFICATION_TIME = "pull_notification_time";
} }

View File

@ -239,7 +239,7 @@
android:id="@+id/image_view_best_post_item" android:id="@+id/image_view_best_post_item"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:scaleType="centerCrop" /> android:scaleType="center" />
</RelativeLayout> </RelativeLayout>