From 0aaa63b6f67030d57ae2852e02680a4f0e061069 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 12 Nov 2019 11:51:08 +0800 Subject: Show low-resolution images in compact layout. Send notifications of messages that are received after the last notification. --- .../Adapter/PostRecyclerViewAdapter.java | 21 +++++- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/ParsePost.java | 83 ++++++++++++---------- .../ml/docilealligator/infinityforreddit/Post.java | 13 +++- .../infinityforreddit/PullNotificationWorker.java | 10 +-- .../infinityforreddit/SharedPreferencesUtils.java | 1 + app/src/main/res/layout/item_post_compact.xml | 2 +- 7 files changed, 83 insertions(+), 48 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 6e6649c7..673d2a33 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -1251,7 +1251,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter() { + RequestBuilder imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { ((PostViewHolder) holder).progressBar.setVisibility(View.GONE); @@ -1279,9 +1279,24 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(previewUrl) + .error(R.drawable.ic_error_outline_black_24dp).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((PostCompactViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target 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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index bc966f49..a4edb575 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -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"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 09259c98..c4979fde 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -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); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 47f4a6a3..90cf631d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 9f4138f2..f3eaca39 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -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; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java index f1aa91cd..309db29a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java @@ -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"; } diff --git a/app/src/main/res/layout/item_post_compact.xml b/app/src/main/res/layout/item_post_compact.xml index ecef2387..f8c48962 100644 --- a/app/src/main/res/layout/item_post_compact.xml +++ b/app/src/main/res/layout/item_post_compact.xml @@ -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" /> -- cgit v1.2.3