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) {
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
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
((PostViewHolder) holder).progressBar.setVisibility(View.GONE);
@ -1279,9 +1279,24 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView
imageRequestBuilder.into(((PostViewHolder) holder).imageView);
}
} 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) {
imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 2)))
imageRequestBuilder
.transform(new BlurTransformation(50, 2))
.into(((PostCompactViewHolder) holder).imageView);
} else {
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 HIDDEN_KEY = "hidden";
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 previewUrl = "";
String thumbnailPreviewUrl = "";
int previewWidth = -1;
int previewHeight = -1;
if (data.has(JSONUtils.PREVIEW_KEY)) {
previewUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0)
.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
previewWidth = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0)
.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.WIDTH_KEY);
previewHeight = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0)
.getJSONObject(JSONUtils.SOURCE_KEY).getInt(JSONUtils.HEIGHT_KEY);
JSONObject images = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
previewUrl = images.getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
JSONArray thumbnailPreviews = images.getJSONArray(JSONUtils.RESOLUTIONS_KEY);
int thumbnailPreviewsLength = thumbnailPreviews.length();
if (thumbnailPreviewsLength > 0) {
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)) {
//Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
Post crosspostParent = parseBasicData(data, locale);
Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, true);
author, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, previewWidth,
previewHeight, score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, true);
post.setCrosspostParentId(crosspostParent.getId());
return post;
} else {
return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, false);
author, formattedPostTime, title, previewUrl, thumbnailPreviewUrl, previewWidth,
previewHeight, score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, false);
}
}
private static Post parseData(JSONObject data, String permalink, String id, String fullName,
String subredditName, String subredditNamePrefixed, String author,
String formattedPostTime, String title, String previewUrl, int previewWidth,
int previewHeight, int score, int voteType, int gilded, int nComments,
String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied,
String formattedPostTime, String title, String previewUrl,
String thumbnailPreviewUrl, int previewWidth, int previewHeight,
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 {
Post post;
@ -131,8 +140,8 @@ public class ParsePost {
//No preview link post
int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType, voteType, gilded, nComments,
flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
title, previewUrl, thumbnailPreviewUrl, url, permalink, score, postType, voteType, gilded,
nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
@ -152,8 +161,8 @@ public class ParsePost {
String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, permalink, score, postType, voteType, gilded, nComments,
flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
title, previewUrl, thumbnailPreviewUrl, permalink, score, postType, voteType, gilded,
nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
@ -166,9 +175,9 @@ public class ParsePost {
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.HLS_URL_KEY)).toString();
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, permalink, score, postType, voteType,
gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived,
locked, saved, isCrosspost);
formattedPostTime, title, previewUrl, thumbnailPreviewUrl, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl);
@ -178,9 +187,9 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, url, url, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
formattedPostTime, title, url, thumbnailPreviewUrl, url, permalink, score,
postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
@ -188,9 +197,9 @@ public class ParsePost {
//Gif post
int postType = Post.GIF_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
@ -228,9 +237,9 @@ public class ParsePost {
int postType = Post.LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, isCrosspost);
if (data.isNull(JSONUtils.SELFTEXT_KEY)) {
post.setSelfText("");
} else {
@ -248,9 +257,9 @@ public class ParsePost {
int postType = Post.IMAGE_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, previewUrl, url, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied,
archived, locked, saved, isCrosspost);
formattedPostTime, title, previewUrl, thumbnailPreviewUrl, url, permalink, score,
postType, voteType, gilded, nComments, flair, hidden, spoiler, nsfw,
stickied, archived, locked, saved, isCrosspost);
post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight);
} else {
@ -258,9 +267,9 @@ public class ParsePost {
int postType = Post.NO_PREVIEW_LINK_TYPE;
post = new Post(id, fullName, subredditName, subredditNamePrefixed, author,
formattedPostTime, title, url, url, permalink, score, postType, voteType,
gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked,
saved, isCrosspost);
formattedPostTime, title, url, thumbnailPreviewUrl, url, permalink, score, postType,
voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived,
locked, saved, isCrosspost);
}
}
}

View File

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

View File

@ -80,15 +80,15 @@ public class PullNotificationWorker extends Worker {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
int messageSize = messages.size() >= 5 ? 5 : messages.size();
long currentTime = Calendar.getInstance().getTimeInMillis();
long notificationInterval = Long.parseLong(
mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"))
* 1000 * 60 * 60;
long lastNotificationTime = mSharedPreferences.getLong(SharedPreferencesUtils.PULL_NOTIFICATION_TIME, -1L);
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--) {
Message message = messages.get(messageIndex);
if (currentTime - message.getTimeUTC() > notificationInterval) {
if (message.getTimeUTC() <= lastNotificationTime) {
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 int POST_LAYOUT_CARD = 0;
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:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
android:scaleType="center" />
</RelativeLayout>