Refresh post in ViewPostDetailActivity.

This commit is contained in:
Alex Ning 2019-06-06 12:05:11 +08:00
parent 7d8c497c77
commit fe4ba7501e
16 changed files with 426 additions and 236 deletions

Binary file not shown.

Binary file not shown.

View File

@ -2,11 +2,12 @@ package ml.docilealligator.infinityforreddit;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import androidx.annotation.NonNull;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
@ -14,13 +15,13 @@ import retrofit2.Retrofit;
class FetchComment { class FetchComment {
interface FetchCommentListener { interface FetchCommentListener {
void onFetchCommentSuccess(List<?> commentData, void onFetchCommentSuccess(List<?> commentsData,
String parentId, ArrayList<String> children); String parentId, ArrayList<String> children);
void onFetchCommentFailed(); void onFetchCommentFailed();
} }
interface FetchMoreCommentListener { interface FetchMoreCommentListener {
void onFetchMoreCommentSuccess(List<?> commentData, int childrenStartingIndex); void onFetchMoreCommentSuccess(List<?> commentsData, int childrenStartingIndex);
void onFetchMoreCommentFailed(); void onFetchMoreCommentFailed();
} }
@ -106,9 +107,6 @@ class FetchComment {
public void onParseCommentSuccess(List<?> commentData, String parentId, public void onParseCommentSuccess(List<?> commentData, String parentId,
ArrayList<String> children) { ArrayList<String> children) {
fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100); fetchMoreCommentListener.onFetchMoreCommentSuccess(commentData, startingIndex + 100);
/*fetchMoreComment(retrofit, subredditNamePrefixed,
mParentId, allChildren, finalStartingIndex,
locale, fetchMoreCommentListener);*/
} }
@Override @Override
@ -157,15 +155,15 @@ class FetchComment {
fetchComment(retrofit, subredditNamePrefixed, article, comment, locale, isPost, parentDepth, fetchComment(retrofit, subredditNamePrefixed, article, comment, locale, isPost, parentDepth,
new FetchCommentListener() { new FetchCommentListener() {
@Override @Override
public void onFetchCommentSuccess(List<?> commentData, String parentId, ArrayList<String> children) { public void onFetchCommentSuccess(List<?> commentsData, String parentId, ArrayList<String> children) {
if(children.size() != 0) { if(children.size() != 0) {
fetchMoreComment(retrofit, subredditNamePrefixed, parentId, children, fetchMoreComment(retrofit, subredditNamePrefixed, parentId, children,
0, locale, new FetchMoreCommentListener() { 0, locale, new FetchMoreCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(List<?> moreCommentData, public void onFetchMoreCommentSuccess(List<?> commentsData,
int childrenStartingIndex) { int childrenStartingIndex) {
((ArrayList<CommentData>)commentData).addAll((ArrayList<CommentData>) moreCommentData); ((ArrayList<CommentData>) commentsData).addAll((ArrayList<CommentData>) commentsData);
fetchAllCommentListener.onFetchAllCommentSuccess(commentData); fetchAllCommentListener.onFetchAllCommentSuccess(commentsData);
} }
@Override @Override
@ -175,7 +173,7 @@ class FetchComment {
} }
}); });
} else { } else {
fetchAllCommentListener.onFetchAllCommentSuccess(commentData); fetchAllCommentListener.onFetchAllCommentSuccess(commentsData);
} }
} }

View File

@ -0,0 +1,52 @@
package ml.docilealligator.infinityforreddit;
import android.util.Log;
import androidx.annotation.NonNull;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
class FetchPost {
interface FetchPostListener {
void fetchPostSuccess(Post post);
void fetchPostFailed();
}
static void fetchPost(Retrofit oauthRetrofit, String id, String accessToken, Locale locale, FetchPostListener fetchPostListener) {
RedditAPI api = oauthRetrofit.create(RedditAPI.class);
Call<String> postCall = api.getPost(id, RedditUtils.getOAuthHeader(accessToken));
postCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(Post post) {
fetchPostListener.fetchPostSuccess(post);
}
@Override
public void onParsePostFail() {
fetchPostListener.fetchPostFailed();
Log.i("error", "fetch post failed");
}
});
} else {
fetchPostListener.fetchPostFailed();
Log.i("call failed", Integer.toString(response.code()));
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
fetchPostListener.fetchPostFailed();
Log.i("call failed", t.getMessage());
}
});
}
}

View File

@ -17,31 +17,56 @@ import java.util.Locale;
*/ */
class ParsePost { class ParsePost {
interface ParsePostsListingListener {
void onParsePostsListingSuccess(ArrayList<Post> newPostData, String lastItem);
void onParsePostsListingFail();
}
interface ParsePostListener { interface ParsePostListener {
void onParsePostSuccess(ArrayList<Post> newPostData, String lastItem); void onParsePostSuccess(Post post);
void onParsePostFail(); void onParsePostFail();
} }
static void parsePosts(String response, Locale locale, ParsePostsListingListener parsePostsListingListener) {
new ParsePostDataAsyncTask(response, locale, parsePostsListingListener).execute();
}
static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) { static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) {
new ParsePostDataAsyncTask(response, locale, parsePostListener).execute(); new ParsePostDataAsyncTask(response, locale, parsePostListener).execute();
} }
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONObject jsonResponse; private JSONArray allData;
private Locale locale; private Locale locale;
private ParsePostsListingListener parsePostsListingListener;
private ParsePostListener parsePostListener; private ParsePostListener parsePostListener;
private ArrayList<Post> newPosts; private ArrayList<Post> newPosts;
private Post post;
private String lastItem; private String lastItem;
private boolean parseFailed; private boolean parseFailed;
ParsePostDataAsyncTask(String response, Locale locale,
ParsePostsListingListener parsePostsListingListener) {
this.parsePostsListingListener = parsePostsListingListener;
try {
JSONObject jsonResponse = new JSONObject(response);
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
this.locale = locale;
newPosts = new ArrayList<>();
parseFailed = false;
} catch (JSONException e) {
e.printStackTrace();
parseFailed = true;
}
}
ParsePostDataAsyncTask(String response, Locale locale, ParsePostDataAsyncTask(String response, Locale locale,
ParsePostListener parsePostListener) { ParsePostListener parsePostListener) {
this.parsePostListener = parsePostListener; this.parsePostListener = parsePostListener;
try { try {
jsonResponse = new JSONObject(response); allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
this.locale = locale; this.locale = locale;
newPosts = new ArrayList<>();
parseFailed = false; parseFailed = false;
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -56,66 +81,35 @@ class ParsePost {
} }
try { try {
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); if(newPosts == null) {
//Only one post
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); if(allData.length() == 0) {
for(int i = 0; i < allData.length(); i++) { parseFailed = true;
String kind = allData.getJSONObject(i).getString(JSONUtils.KIND_KEY); return null;
if(!kind.equals("t3")) {
//It's a comment
continue;
} }
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String id = data.getString(JSONUtils.ID_KEY);
String fullName = data.getString(JSONUtils.NAME_KEY);
String subredditNamePrefixed = data.getString(JSONUtils.SUBREDDIT_NAME_PREFIX_KEY);
String author = data.getString(JSONUtils.AUTHOR_KEY);
long postTime = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
String title = data.getString(JSONUtils.TITLE_KEY);
int score = data.getInt(JSONUtils.SCORE_KEY);
int voteType;
int gilded = data.getInt(JSONUtils.GILDED_KEY);
boolean nsfw = data.getBoolean(JSONUtils.NSFW_KEY);
boolean stickied = data.getBoolean(JSONUtils.STICKIED_KEY);
if(data.isNull(JSONUtils.LIKES_KEY)) { String kind = allData.getJSONObject(0).getString(JSONUtils.KIND_KEY);
voteType = 0; if(kind.equals("t3")) {
//It's a post
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
post = parseBasicData(data, locale, -1);
} else { } else {
voteType = data.getBoolean(JSONUtils.LIKES_KEY) ? 1 : -1; parseFailed = true;
score -= voteType; return null;
} }
} else {
Calendar postTimeCalendar = Calendar.getInstance(); //Posts listing
postTimeCalendar.setTimeInMillis(postTime); for(int i = 0; i < allData.length(); i++) {
String formattedPostTime = new SimpleDateFormat("MMM d, YYYY, HH:mm", String kind = allData.getJSONObject(i).getString(JSONUtils.KIND_KEY);
locale).format(postTimeCalendar.getTime()); if(kind.equals("t3")) {
String permalink = data.getString(JSONUtils.PERMALINK_KEY); //It's a post
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
String previewUrl = ""; newPosts.add(parseBasicData(data, locale, i));
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);
}
if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) {
//Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
parseData(data, permalink, newPosts, id, fullName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nsfw, stickied, true, i);
} else {
parseData(data, permalink, newPosts, id, fullName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nsfw, stickied, false, i);
} }
} }
} catch (JSONException e) { } catch (JSONException e) {
Log.e("best post parse error", e.getMessage()); Log.e("parsing post error", e.getMessage());
parseFailed = true; parseFailed = true;
} }
return null; return null;
@ -124,18 +118,78 @@ class ParsePost {
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
if(!parseFailed) { if(!parseFailed) {
parsePostListener.onParsePostSuccess(newPosts, lastItem); if(newPosts != null) {
parsePostsListingListener.onParsePostsListingSuccess(newPosts, lastItem);
} else {
parsePostListener.onParsePostSuccess(post);
}
} else { } else {
parsePostListener.onParsePostFail(); if(newPosts != null) {
parsePostsListingListener.onParsePostsListingFail();
} else {
parsePostListener.onParsePostFail();
}
} }
} }
} }
private static void parseData(JSONObject data, String permalink, ArrayList<Post> bestPostData, private static Post parseBasicData(JSONObject data, Locale locale, int i) throws JSONException {
String id, String fullName, String subredditNamePrefixed, String author, String id = data.getString(JSONUtils.ID_KEY);
String formattedPostTime, String title, String previewUrl, int previewWidth, String fullName = data.getString(JSONUtils.NAME_KEY);
int previewHeight ,int score, int voteType, int gilded, String subredditNamePrefixed = data.getString(JSONUtils.SUBREDDIT_NAME_PREFIX_KEY);
boolean nsfw, boolean stickied, boolean isCrosspost, int i) throws JSONException { String author = data.getString(JSONUtils.AUTHOR_KEY);
long postTime = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
String title = data.getString(JSONUtils.TITLE_KEY);
int score = data.getInt(JSONUtils.SCORE_KEY);
int voteType;
int gilded = data.getInt(JSONUtils.GILDED_KEY);
boolean nsfw = data.getBoolean(JSONUtils.NSFW_KEY);
boolean stickied = data.getBoolean(JSONUtils.STICKIED_KEY);
if(data.isNull(JSONUtils.LIKES_KEY)) {
voteType = 0;
} else {
voteType = data.getBoolean(JSONUtils.LIKES_KEY) ? 1 : -1;
score -= voteType;
}
Calendar postTimeCalendar = Calendar.getInstance();
postTimeCalendar.setTimeInMillis(postTime);
String formattedPostTime = new SimpleDateFormat("MMM d, YYYY, HH:mm",
locale).format(postTimeCalendar.getTime());
String permalink = data.getString(JSONUtils.PERMALINK_KEY);
String previewUrl = "";
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);
}
if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) {
//Cross post
data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0);
return parseData(data, permalink, id, fullName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nsfw, stickied, true, i);
} else {
return parseData(data, permalink, id, fullName, subredditNamePrefixed,
author, formattedPostTime, title, previewUrl, previewWidth, previewHeight,
score, voteType, gilded, nsfw, stickied, false, i);
}
}
private static Post parseData(JSONObject data, String permalink, String id, String fullName,
String subredditNamePrefixed, String author, String formattedPostTime,
String title, String previewUrl, int previewWidth, int previewHeight,
int score, int voteType, int gilded, boolean nsfw, boolean stickied,
boolean isCrosspost, int i) throws JSONException {
Post post;
boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY);
String url = data.getString(JSONUtils.URL_KEY); String url = data.getString(JSONUtils.URL_KEY);
@ -144,27 +198,25 @@ class ParsePost {
//Text post //Text post
Log.i("text", Integer.toString(i)); Log.i("text", Integer.toString(i));
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
Post post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost);
if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
post.setSelfText(""); post.setSelfText("");
} else { } else {
post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim());
} }
bestPostData.add(post);
} else { } else {
//No preview link post //No preview link post
Log.i("no preview link", Integer.toString(i)); Log.i("no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
Post linkPost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType, title, previewUrl, url, permalink, score, postType,
voteType, gilded, nsfw, stickied, isCrosspost); voteType, gilded, nsfw, stickied, isCrosspost);
if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
linkPost.setSelfText(""); post.setSelfText("");
} else { } else {
linkPost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim());
} }
bestPostData.add(linkPost);
} }
} else { } else {
if(previewUrl.equals("")) { if(previewUrl.equals("")) {
@ -179,16 +231,14 @@ class ParsePost {
int postType = Post.VIDEO_TYPE; int postType = Post.VIDEO_TYPE;
String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY); String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY);
Post videoPost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, permalink, score, postType, voteType, title, previewUrl, permalink, score, postType, voteType,
gilded, nsfw, stickied, isCrosspost, true); gilded, nsfw, stickied, isCrosspost, true);
videoPost.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
videoPost.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
videoPost.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
videoPost.setDownloadableGifOrVideo(false); post.setDownloadableGifOrVideo(false);
bestPostData.add(videoPost);
} else if(data.has(JSONUtils.PREVIEW_KEY)){ } else if(data.has(JSONUtils.PREVIEW_KEY)){
JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0); JSONObject variations = data.getJSONObject(JSONUtils.PREVIEW_KEY).getJSONArray(JSONUtils.IMAGES_KEY).getJSONObject(0);
if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) { if (variations.has(JSONUtils.VARIANTS_KEY) && variations.getJSONObject(JSONUtils.VARIANTS_KEY).has(JSONUtils.MP4_KEY)) {
@ -197,17 +247,15 @@ class ParsePost {
int postType = Post.GIF_VIDEO_TYPE; int postType = Post.GIF_VIDEO_TYPE;
String videoUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); String videoUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
String gifDownloadUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); String gifDownloadUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY);
Post post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title,
post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title,
previewUrl, permalink, score, postType, voteType, previewUrl, permalink, score, postType, voteType,
gilded, nsfw, stickied, isCrosspost, false); gilded, nsfw, stickied, isCrosspost, false);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setDownloadableGifOrVideo(true); post.setDownloadableGifOrVideo(true);
post.setGifOrVideoDownloadUrl(gifDownloadUrl); post.setGifOrVideoDownloadUrl(gifDownloadUrl);
bestPostData.add(post);
} else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { } else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) {
//Gif video post (Dash) //Gif video post (Dash)
Log.i("gif video dash", Integer.toString(i)); Log.i("gif video dash", Integer.toString(i));
@ -215,64 +263,58 @@ class ParsePost {
String videoUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY) String videoUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY)
.getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY); .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY);
Post post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title,
previewUrl, permalink, score, postType, voteType, previewUrl, permalink, score, postType, voteType,
gilded, nsfw, stickied, isCrosspost, true); gilded, nsfw, stickied, isCrosspost, true);
post.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
post.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
post.setVideoUrl(videoUrl); post.setVideoUrl(videoUrl);
post.setDownloadableGifOrVideo(false); post.setDownloadableGifOrVideo(false);
bestPostData.add(post);
} else { } else {
if (url.endsWith("jpg") || url.endsWith("png")) { if (url.endsWith("jpg") || url.endsWith("png")) {
//Image post //Image post
Log.i("image", Integer.toString(i)); Log.i("image", Integer.toString(i));
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
Post imagePost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, url, url, permalink, score, postType, title, url, url, permalink, score, postType,
voteType, gilded, nsfw, stickied, isCrosspost); voteType, gilded, nsfw, stickied, isCrosspost);
imagePost.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
imagePost.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
bestPostData.add(imagePost);
} else { } else {
if (url.contains(permalink)) { if (url.contains(permalink)) {
//Text post but with a preview //Text post but with a preview
Log.i("text with image", Integer.toString(i)); Log.i("text with image", Integer.toString(i));
int postType = Post.TEXT_TYPE; int postType = Post.TEXT_TYPE;
Post textWithImagePost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost);
textWithImagePost.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
textWithImagePost.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
textWithImagePost.setSelfText(""); post.setSelfText("");
} else { } else {
textWithImagePost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim());
} }
bestPostData.add(textWithImagePost);
} else { } else {
//Link post //Link post
Log.i("link", Integer.toString(i)); Log.i("link", Integer.toString(i));
int postType = Post.LINK_TYPE; int postType = Post.LINK_TYPE;
Post linkPost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, title, previewUrl, url, permalink, score,
postType, voteType, gilded, nsfw, stickied, isCrosspost); postType, voteType, gilded, nsfw, stickied, isCrosspost);
if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) {
linkPost.setSelfText(""); post.setSelfText("");
} else { } else {
linkPost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim());
} }
linkPost.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
linkPost.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
bestPostData.add(linkPost);
} }
} }
} }
@ -281,21 +323,24 @@ class ParsePost {
//Image post //Image post
Log.i("CP image", Integer.toString(i)); Log.i("CP image", Integer.toString(i));
int postType = Post.IMAGE_TYPE; int postType = Post.IMAGE_TYPE;
Post linkPost = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime,
title, previewUrl, url, permalink, score, postType, title, previewUrl, url, permalink, score, postType,
voteType, gilded, nsfw, stickied, isCrosspost); voteType, gilded, nsfw, stickied, isCrosspost);
linkPost.setPreviewWidth(previewWidth); post.setPreviewWidth(previewWidth);
linkPost.setPreviewHeight(previewHeight); post.setPreviewHeight(previewHeight);
bestPostData.add(linkPost);
} else { } else {
//CP No Preview Link post //CP No Preview Link post
Log.i("CP no preview link", Integer.toString(i)); Log.i("CP no preview link", Integer.toString(i));
int postType = Post.NO_PREVIEW_LINK_TYPE; int postType = Post.NO_PREVIEW_LINK_TYPE;
bestPostData.add(new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title,
post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title,
url, url, permalink, score, postType, voteType, url, url, permalink, score, postType, voteType,
gilded, nsfw, stickied, isCrosspost)); gilded, nsfw, stickied, isCrosspost);
} }
} }
} }
return post;
} }
} }

View File

@ -130,10 +130,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, ParsePost.parsePosts(response.body(), locale,
new ParsePost.ParsePostListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if(newPosts.size() == 0) { if(newPosts.size() == 0) {
onPostFetchedCallback.noPost(); onPostFetchedCallback.noPost();
} else { } else {
@ -145,7 +145,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Post fetch error", "Error parsing data"); Log.i("Post fetch error", "Error parsing data");
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -172,15 +172,15 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED); paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Best post", "Error parsing data"); Log.i("Best post", "Error parsing data");
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -206,10 +206,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, ParsePost.parsePosts(response.body(), locale,
new ParsePost.ParsePostListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if(newPosts.size() == 0) { if(newPosts.size() == 0) {
onPostFetchedCallback.noPost(); onPostFetchedCallback.noPost();
} else { } else {
@ -221,7 +221,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Post fetch error", "Error parsing data"); Log.i("Post fetch error", "Error parsing data");
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -247,15 +247,15 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED); paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Best post", "Error parsing data"); Log.i("Best post", "Error parsing data");
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -281,10 +281,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, ParsePost.parsePosts(response.body(), locale,
new ParsePost.ParsePostListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if(newPosts.size() == 0 && lastItem.equals("null")) { if(newPosts.size() == 0 && lastItem.equals("null")) {
callback.onResult(newPosts, null, lastItem); callback.onResult(newPosts, null, lastItem);
initialLoadStateLiveData.postValue(NetworkState.LOADED); initialLoadStateLiveData.postValue(NetworkState.LOADED);
@ -299,7 +299,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Post fetch error", "Error parsing data"); Log.i("Post fetch error", "Error parsing data");
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -325,15 +325,15 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED); paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Best post", "Error parsing data"); Log.i("Best post", "Error parsing data");
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -359,10 +359,10 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, ParsePost.parsePosts(response.body(), locale,
new ParsePost.ParsePostListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
if(newPosts.size() == 0) { if(newPosts.size() == 0) {
onPostFetchedCallback.noPost(); onPostFetchedCallback.noPost();
} else { } else {
@ -374,7 +374,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Post fetch error", "Error parsing data"); Log.i("Post fetch error", "Error parsing data");
initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }
@ -400,15 +400,15 @@ class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(Call<String> call, retrofit2.Response<String> response) { public void onResponse(Call<String> call, retrofit2.Response<String> response) {
if(response.isSuccessful()) { if(response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePosts(response.body(), locale, new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostSuccess(ArrayList<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(ArrayList<Post> newPosts, String lastItem) {
callback.onResult(newPosts, lastItem); callback.onResult(newPosts, lastItem);
paginationNetworkStateLiveData.postValue(NetworkState.LOADED); paginationNetworkStateLiveData.postValue(NetworkState.LOADED);
} }
@Override @Override
public void onParsePostFail() { public void onParsePostsListingFail() {
Log.i("Best post", "Error parsing data"); Log.i("Best post", "Error parsing data");
paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data"));
} }

View File

@ -180,10 +180,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator {
} }
@Subscribe @Subscribe
public void onVoteEvent(VoteEventToPostList event) { public void onPostUpdateEvent(PostUpdateEventToPostList event) {
Post post = mAdapter.getCurrentList().get(event.positionInList); Post post = mAdapter.getCurrentList().get(event.positionInList);
if(post != null) { if(post != null) {
post.setVoteType(event.voteType); post.setTitle(event.post.getTitle());
post.setVoteType(event.post.getVoteType());
post.setScore(event.post.getScore());
mAdapter.notifyItemChanged(event.positionInList); mAdapter.notifyItemChanged(event.positionInList);
} }
} }

View File

@ -414,7 +414,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).downvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType()));
} }
@Override @Override
@ -425,7 +425,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType()));
} }
}, id, newVoteType, holder.getAdapterPosition()); }, id, newVoteType, holder.getAdapterPosition());
}); });
@ -469,7 +469,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).upvoteButton.clearColorFilter();
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType()));
EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType()));
} }
@Override @Override
@ -480,7 +480,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo
((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter); ((DataViewHolder) holder).upvoteButton.setColorFilter(previousUpvoteButtonColorFilter);
((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
EventBus.getDefault().post(new VoteEventToDetailActivity(post.getId(), post.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType()));
} }
}, id, newVoteType, holder.getAdapterPosition()); }, id, newVoteType, holder.getAdapterPosition());
}); });

View File

@ -1,10 +1,10 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
public class VoteEventToDetailActivity { public class PostUpdateEventToDetailActivity {
public final String postId; public final String postId;
public final int voteType; public final int voteType;
public VoteEventToDetailActivity(String postId, int voteType) { public PostUpdateEventToDetailActivity(String postId, int voteType) {
this.postId = postId; this.postId = postId;
this.voteType = voteType; this.voteType = voteType;
} }

View File

@ -0,0 +1,11 @@
package ml.docilealligator.infinityforreddit;
public class PostUpdateEventToPostList {
public final Post post;
public final int positionInList;
public PostUpdateEventToPostList(Post post, int positionInList) {
this.post = post;
this.positionInList = positionInList;
}
}

View File

@ -33,6 +33,9 @@ public interface RedditAPI {
@POST("api/vote") @POST("api/vote")
Call<String> voteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); Call<String> voteThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params);
@GET("comments/{id}.json?raw_json=1")
Call<String> getPost(@Path("id") String id, @HeaderMap Map<String, String> headers);
@GET("best?raw_json=1") @GET("best?raw_json=1")
Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers); Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers);

View File

@ -1,5 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
@ -7,9 +8,9 @@ import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -46,6 +47,7 @@ import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
@ -66,25 +68,33 @@ public class ViewPostDetailActivity extends AppCompatActivity {
static final String EXTRA_POST_DATA = "EPD"; static final String EXTRA_POST_DATA = "EPD";
static final String EXTRA_POST_LIST_POSITION = "EPLI"; static final String EXTRA_POST_LIST_POSITION = "EPLI";
private RequestManager glide; private RequestManager mGlide;
private Locale mLocale;
private int orientation; private int orientation;
private static String ORIENTATION_STATE = "OS"; private static final String ORIENTATION_STATE = "OS";
private static final String POST_STATE = "PS"; private static final String POST_STATE = "PS";
private static final String IS_REFRESHING_STATE = "IRS";
private Post mPost; private Post mPost;
private int postListPosition = -1; private int postListPosition = -1;
private boolean isLoadingMoreChildren = false; private boolean isLoadingMoreChildren = false;
private boolean isRefreshing = false;
private ArrayList<CommentData> mCommentsData;
private ArrayList<String> children; private ArrayList<String> children;
private int mChildrenStartingIndex = 0; private int mChildrenStartingIndex = 0;
private CommentMultiLevelRecyclerViewAdapter mAdapter;
private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask;
@BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout;
@BindView(R.id.nested_scroll_view_view_post_detail_activity) NestedScrollView mNestedScrollView; @BindView(R.id.nested_scroll_view_view_post_detail_activity) NestedScrollView mNestedScrollView;
@BindView(R.id.subreddit_icon_name_linear_layout_view_post_detail) LinearLayout mSubredditIconNameLinearLayout; @BindView(R.id.subreddit_icon_name_linear_layout_view_post_detail) LinearLayout mSubredditIconNameLinearLayout;
@BindView(R.id.subreddit_icon_circle_image_view_view_post_detail) AspectRatioGifImageView mSubredditIconGifImageView; @BindView(R.id.subreddit_icon_circle_image_view_view_post_detail) AspectRatioGifImageView mSubredditIconGifImageView;
@BindView(R.id.subreddit_text_view_view_post_detail) TextView mSubredditTextView; @BindView(R.id.subreddit_text_view_view_post_detail) TextView mSubredditTextView;
@BindView(R.id.post_time_text_view_view_post_detail) TextView mPostTimeTextView; @BindView(R.id.post_time_text_view_view_post_detail) TextView mPostTimeTextView;
@BindView(R.id.title_text_view_view_post_detail) TextView mTitleTextView;
@BindView(R.id.content_markdown_view_view_post_detail) MarkwonView mContentMarkdownView; @BindView(R.id.content_markdown_view_view_post_detail) MarkwonView mContentMarkdownView;
@BindView(R.id.type_text_view_view_post_detail) Chip mTypeChip; @BindView(R.id.type_text_view_view_post_detail) Chip mTypeChip;
@BindView(R.id.gilded_image_view_view_post_detail) ImageView mGildedImageView; @BindView(R.id.gilded_image_view_view_post_detail) ImageView mGildedImageView;
@ -111,10 +121,6 @@ public class ViewPostDetailActivity extends AppCompatActivity {
@BindView(R.id.no_comment_wrapper_linear_layout_view_post_detail) LinearLayout mNoCommentWrapperLinearLayout; @BindView(R.id.no_comment_wrapper_linear_layout_view_post_detail) LinearLayout mNoCommentWrapperLinearLayout;
@BindView(R.id.no_comment_image_view_view_post_detail) ImageView mNoCommentImageView; @BindView(R.id.no_comment_image_view_view_post_detail) ImageView mNoCommentImageView;
private CommentMultiLevelRecyclerViewAdapter mAdapter;
private LinearLayoutManager mLayoutManager;
private LoadSubredditIconAsyncTask mLoadSubredditIconAsyncTask;
@Inject @Named("no_oauth") @Inject @Named("no_oauth")
Retrofit mRetrofit; Retrofit mRetrofit;
@ -137,34 +143,53 @@ public class ViewPostDetailActivity extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mGlide = Glide.with(this);
mLocale = getResources().getConfiguration().locale;
if(savedInstanceState == null) { if(savedInstanceState == null) {
orientation = getResources().getConfiguration().orientation; orientation = getResources().getConfiguration().orientation;
mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA);
} else { } else {
orientation = savedInstanceState.getInt(ORIENTATION_STATE); orientation = savedInstanceState.getInt(ORIENTATION_STATE);
mPost = savedInstanceState.getParcelable(POST_STATE); mPost = savedInstanceState.getParcelable(POST_STATE);
isRefreshing = savedInstanceState.getBoolean(IS_REFRESHING_STATE);
if(isRefreshing) {
isRefreshing = false;
refresh();
}
} }
if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) { if(getIntent().hasExtra(EXTRA_POST_LIST_POSITION)) {
postListPosition = getIntent().getExtras().getInt(EXTRA_POST_LIST_POSITION); postListPosition = getIntent().getExtras().getInt(EXTRA_POST_LIST_POSITION);
} }
TextView titleTextView = findViewById(R.id.title_text_view_view_post_detail); mCommentsData = new ArrayList<>();
titleTextView.setText(mPost.getTitle());
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
fetchComment();
bindView();
initializeButtonOnClickListener();
}
private void bindView() {
mTitleTextView.setText(mPost.getTitle());
glide = Glide.with(this);
if(mPost.getSubredditIconUrl() == null) { if(mPost.getSubredditIconUrl() == null) {
mLoadSubredditIconAsyncTask = new LoadSubredditIconAsyncTask( mLoadSubredditIconAsyncTask = new LoadSubredditIconAsyncTask(
SubredditRoomDatabase.getDatabase(this).subredditDao(), mPost.getSubredditNamePrefixed().substring(2), SubredditRoomDatabase.getDatabase(this).subredditDao(), mPost.getSubredditNamePrefixed().substring(2),
iconImageUrl -> { iconImageUrl -> {
if(!iconImageUrl.equals("")) { if(!iconImageUrl.equals("")) {
glide.load(iconImageUrl) mGlide.load(iconImageUrl)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon) .error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(mSubredditIconGifImageView); .into(mSubredditIconGifImageView);
} else { } else {
glide.load(R.drawable.subreddit_default_icon) mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(mSubredditIconGifImageView); .into(mSubredditIconGifImageView);
} }
@ -173,13 +198,13 @@ public class ViewPostDetailActivity extends AppCompatActivity {
}); });
mLoadSubredditIconAsyncTask.execute(); mLoadSubredditIconAsyncTask.execute();
} else if(!mPost.getSubredditIconUrl().equals("")) { } else if(!mPost.getSubredditIconUrl().equals("")) {
glide.load(mPost.getSubredditIconUrl()) mGlide.load(mPost.getSubredditIconUrl())
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.error(glide.load(R.drawable.subreddit_default_icon) .error(mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
.into(mSubredditIconGifImageView); .into(mSubredditIconGifImageView);
} else { } else {
glide.load(R.drawable.subreddit_default_icon) mGlide.load(R.drawable.subreddit_default_icon)
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))) .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
.into(mSubredditIconGifImageView); .into(mSubredditIconGifImageView);
} }
@ -193,6 +218,9 @@ public class ViewPostDetailActivity extends AppCompatActivity {
//Downvote //Downvote
mDownvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.minusButtonColor), PorterDuff.Mode.SRC_IN); mDownvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.minusButtonColor), PorterDuff.Mode.SRC_IN);
break; break;
case 0:
mUpvoteButton.clearColorFilter();
mDownvoteButton.clearColorFilter();
} }
if(mPost.getPostType() != Post.TEXT_TYPE && mPost.getPostType() != Post.NO_PREVIEW_LINK_TYPE) { if(mPost.getPostType() != Post.TEXT_TYPE && mPost.getPostType() != Post.NO_PREVIEW_LINK_TYPE) {
@ -200,30 +228,22 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mImageView.setVisibility(View.VISIBLE); mImageView.setVisibility(View.VISIBLE);
mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth()); mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth());
loadImage(); loadImage();
} else {
mRelativeLayout.setVisibility(View.GONE);
mImageView.setVisibility(View.GONE);
} }
if(mPost.isCrosspost()) { if(mPost.isCrosspost()) {
mCrosspostImageView.setVisibility(View.VISIBLE); mCrosspostImageView.setVisibility(View.VISIBLE);
} }
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
mSubredditTextView.setText(mPost.getSubredditNamePrefixed()); mSubredditTextView.setText(mPost.getSubredditNamePrefixed());
mSubredditIconNameLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(ViewPostDetailActivity.this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
mPost.getSubredditNamePrefixed().substring(2));
startActivity(intent);
});
mPostTimeTextView.setText(mPost.getPostTime()); mPostTimeTextView.setText(mPost.getPostTime());
if(mPost.getGilded() > 0) { if(mPost.getGilded() > 0) {
mGildedImageView.setVisibility(View.VISIBLE); mGildedImageView.setVisibility(View.VISIBLE);
glide.load(R.drawable.gold).into(mGildedImageView); mGlide.load(R.drawable.gold).into(mGildedImageView);
mGildedNumberTextView.setVisibility(View.VISIBLE); mGildedNumberTextView.setVisibility(View.VISIBLE);
String gildedNumber = getResources().getString(R.string.gilded, mPost.getGilded()); String gildedNumber = getResources().getString(R.string.gilded, mPost.getGilded());
mGildedNumberTextView.setText(gildedNumber); mGildedNumberTextView.setText(gildedNumber);
@ -231,16 +251,12 @@ public class ViewPostDetailActivity extends AppCompatActivity {
if(mPost.isNSFW()) { if(mPost.isNSFW()) {
mNSFWChip.setVisibility(View.VISIBLE); mNSFWChip.setVisibility(View.VISIBLE);
} else {
mNSFWChip.setVisibility(View.GONE);
} }
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
mShareButton.setOnClickListener(view -> { String scoreWithVote = Integer.toString(mPost.getScore() + mPost.getVoteType());
Intent intent = new Intent(Intent.ACTION_SEND); mScoreTextView.setText(scoreWithVote);
intent.setType("text/plain");
String extraText = mPost.getTitle() + "\n" + mPost.getPermalink();
intent.putExtra(Intent.EXTRA_TEXT, extraText);
startActivity(Intent.createChooser(intent, "Share"));
});
switch (mPost.getPostType()) { switch (mPost.getPostType()) {
case Post.IMAGE_TYPE: case Post.IMAGE_TYPE:
@ -329,8 +345,23 @@ public class ViewPostDetailActivity extends AppCompatActivity {
} }
break; break;
} }
}
fetchComment(); private void initializeButtonOnClickListener() {
mSubredditIconNameLinearLayout.setOnClickListener(view -> {
Intent intent = new Intent(ViewPostDetailActivity.this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
mPost.getSubredditNamePrefixed().substring(2));
startActivity(intent);
});
mShareButton.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String extraText = mPost.getTitle() + "\n" + mPost.getPermalink();
intent.putExtra(Intent.EXTRA_TEXT, extraText);
startActivity(Intent.createChooser(intent, "Share"));
});
mUpvoteButton.setOnClickListener(view -> { mUpvoteButton.setOnClickListener(view -> {
ColorFilter previousUpvoteButtonColorFilter = mUpvoteButton.getColorFilter(); ColorFilter previousUpvoteButtonColorFilter = mUpvoteButton.getColorFilter();
@ -355,7 +386,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@ -373,7 +404,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
} }
@ -386,7 +417,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter); mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
} }
}, mPost.getFullName(), newVoteType); }, mPost.getFullName(), newVoteType);
@ -415,7 +446,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() {
@ -433,7 +464,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType())); mScoreTextView.setText(Integer.toString(mPost.getScore() + mPost.getVoteType()));
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
} }
@ -446,7 +477,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter); mDownvoteButton.setColorFilter(previousDownvoteButtonColorFilter);
if(postListPosition != -1) { if(postListPosition != -1) {
EventBus.getDefault().post(new VoteEventToPostList(postListPosition, mPost.getVoteType())); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
} }
} }
}, mPost.getFullName(), newVoteType); }, mPost.getFullName(), newVoteType);
@ -454,30 +485,32 @@ public class ViewPostDetailActivity extends AppCompatActivity {
} }
private void fetchComment() { private void fetchComment() {
mCommentCardView.setVisibility(View.GONE);
mCommentProgressbar.setVisibility(View.VISIBLE); mCommentProgressbar.setVisibility(View.VISIBLE);
mNoCommentWrapperLinearLayout.setVisibility(View.GONE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE);
FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(),
null, getResources().getConfiguration().locale, true, 0, new FetchComment.FetchCommentListener() { null, mLocale, true, 0, new FetchComment.FetchCommentListener() {
@Override @Override
public void onFetchCommentSuccess(List<?> commentData, public void onFetchCommentSuccess(List<?> commentsData,
String parentId, ArrayList<String> children) { String parentId, ArrayList<String> children) {
ViewPostDetailActivity.this.children = children; ViewPostDetailActivity.this.children = children;
mCommentProgressbar.setVisibility(View.GONE); mCommentProgressbar.setVisibility(View.GONE);
if (commentData.size() > 0) { mCommentsData.addAll((ArrayList<CommentData>) commentsData);
mAdapter = new CommentMultiLevelRecyclerViewAdapter(
ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit,
mSharedPreferences, (ArrayList<CommentData>) commentData,
mRecyclerView, mPost.getSubredditNamePrefixed(),
mPost.getId(), getResources().getConfiguration().locale);
mRecyclerView.removeItemClickListeners();
mRecyclerView.setToggleItemOnClick(false);
mRecyclerView.setAccordion(false);
mRecyclerView.setAdapter(mAdapter);
mCommentCardView.setVisibility(View.VISIBLE);
mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() { if (mCommentsData.size() > 0) {
@Override if(mAdapter == null) {
public void onScrollChanged() { mAdapter = new CommentMultiLevelRecyclerViewAdapter(
ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit,
mSharedPreferences, mCommentsData,
mRecyclerView, mPost.getSubredditNamePrefixed(),
mPost.getId(), mLocale);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.removeItemClickListeners();
mRecyclerView.setToggleItemOnClick(false);
mRecyclerView.setAccordion(false);
mNestedScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> {
if(!isLoadingMoreChildren) { if(!isLoadingMoreChildren) {
View view = mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1); View view = mNestedScrollView.getChildAt(mNestedScrollView.getChildCount() - 1);
int diff = view.getBottom() - (mNestedScrollView.getHeight() + int diff = view.getBottom() - (mNestedScrollView.getHeight() +
@ -486,11 +519,15 @@ public class ViewPostDetailActivity extends AppCompatActivity {
fetchMoreComment(mChildrenStartingIndex); fetchMoreComment(mChildrenStartingIndex);
} }
} }
} });
}); } else {
mAdapter.notifyDataSetChanged();
}
mCommentCardView.setVisibility(View.VISIBLE);
} else { } else {
mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE); mNoCommentWrapperLinearLayout.setVisibility(View.VISIBLE);
glide.load(R.drawable.no_comment_indicator).into(mNoCommentImageView); mGlide.load(R.drawable.no_comment_indicator).into(mNoCommentImageView);
} }
} }
@ -505,11 +542,10 @@ public class ViewPostDetailActivity extends AppCompatActivity {
private void fetchMoreComment(int startingIndex) { private void fetchMoreComment(int startingIndex) {
isLoadingMoreChildren = true; isLoadingMoreChildren = true;
FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getFullName(), FetchComment.fetchMoreComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getFullName(),
children, startingIndex, getResources().getConfiguration().locale, children, startingIndex, mLocale, new FetchComment.FetchMoreCommentListener() {
new FetchComment.FetchMoreCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(List<?> commentData, int childrenStartingIndex) { public void onFetchMoreCommentSuccess(List<?> commentsData, int childrenStartingIndex) {
mAdapter.addComments((ArrayList<CommentData>) commentData); mAdapter.addComments((ArrayList<CommentData>) commentsData);
mChildrenStartingIndex = childrenStartingIndex; mChildrenStartingIndex = childrenStartingIndex;
isLoadingMoreChildren = false; isLoadingMoreChildren = false;
} }
@ -525,7 +561,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
} }
private void loadImage() { private void loadImage() {
RequestBuilder imageRequestBuilder = glide.load(mPost.getPreviewUrl()) RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl())
.apply(new RequestOptions().override(mPost.getPreviewWidth(), mPost.getPreviewHeight())) .apply(new RequestOptions().override(mPost.getPreviewWidth(), mPost.getPreviewHeight()))
.listener(new RequestListener<Drawable>() { .listener(new RequestListener<Drawable>() {
@Override @Override
@ -582,17 +618,59 @@ public class ViewPostDetailActivity extends AppCompatActivity {
snackbar.show(); snackbar.show();
} }
private void refresh() {
if(!isRefreshing) {
isRefreshing = true;
if(mAdapter != null && mCommentsData != null) {
int size = mCommentsData.size();
mCommentsData.clear();
mAdapter.notifyItemRangeRemoved(0, size);
}
fetchComment();
String accessToken = getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE)
.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, "");
FetchPost.fetchPost(mOauthRetrofit, mPost.getId(), accessToken, mLocale,
new FetchPost.FetchPostListener() {
@Override
public void fetchPostSuccess(Post post) {
mPost = post;
bindView();
EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition));
isRefreshing = false;
}
@Override
public void fetchPostFailed() {
Snackbar.make(mCoordinatorLayout, R.string.refresh_post_failed, Snackbar.LENGTH_SHORT);
isRefreshing = false;
}
});
}
}
@Subscribe @Subscribe
public void onVoteEvent(VoteEventToDetailActivity event) { public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) {
if(mPost.getId().equals(event.postId)) { if(mPost.getId().equals(event.postId)) {
mPost.setVoteType(event.voteType); mPost.setVoteType(event.voteType);
mScoreTextView.setText(Integer.toString(mPost.getScore() + event.voteType)); mScoreTextView.setText(Integer.toString(mPost.getScore() + event.voteType));
} }
} }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.view_post_detail_activity, menu);
return true;
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_refresh_view_post_detail_activity:
refresh();
return true;
case android.R.id.home: case android.R.id.home:
onBackPressed(); onBackPressed();
return true; return true;
@ -605,6 +683,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putInt(ORIENTATION_STATE, orientation); outState.putInt(ORIENTATION_STATE, orientation);
outState.putParcelable(POST_STATE, mPost); outState.putParcelable(POST_STATE, mPost);
outState.putBoolean(IS_REFRESHING_STATE, isRefreshing);
} }
@Override @Override

View File

@ -1,11 +0,0 @@
package ml.docilealligator.infinityforreddit;
public class VoteEventToPostList {
public final int positionInList;
public final int voteType;
public VoteEventToPostList(int positionInList, int voteType) {
this.positionInList = positionInList;
this.voteType = voteType;
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_refresh_view_post_detail_activity"
android:orderInCategory="2"
android:title="@string/action_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
app:showAsAction="always" />
</menu>

View File

@ -26,6 +26,7 @@
<string name="comments">Comments</string> <string name="comments">Comments</string>
<string name="no_comments_yet">No comments yet. Write a comment?</string> <string name="no_comments_yet">No comments yet. Write a comment?</string>
<string name="vote_failed">Vote failed</string> <string name="vote_failed">Vote failed</string>
<string name="refresh_post_failed">Error refreshing the post</string>
<string name="nsfw">NSFW</string> <string name="nsfw">NSFW</string>
<string name="karma_info">Karma: %1$d</string> <string name="karma_info">Karma: %1$d</string>

View File

@ -7,7 +7,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.4.0' classpath 'com.android.tools.build:gradle:3.4.1'
// NOTE: Do not place your application dependencies here; they belong // NOTE: Do not place your application dependencies here; they belong