aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2019-11-12 03:51:08 +0000
committerAlex Ning <chineseperson5@gmail.com>2019-11-12 03:51:08 +0000
commit0aaa63b6f67030d57ae2852e02680a4f0e061069 (patch)
treede418b923654af2337d388b392ba7b9cc6ce74a1 /app/src/main/java
parent74ae9a8b683fd1550a01ac6c2e3f1241c80464e9 (diff)
downloadinfinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar.gz
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar.bz2
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar.lz
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar.xz
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.tar.zst
infinity-for-reddit-0aaa63b6f67030d57ae2852e02680a4f0e061069.zip
Show low-resolution images in compact layout. Send notifications of messages that are received after the last notification.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java21
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java1
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java83
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Post.java13
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java10
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SharedPreferencesUtils.java1
6 files changed, 82 insertions, 47 deletions
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<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);
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";
}