From cdcb38db51389c31185601eef6a6efe3be4d71f2 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 9 Nov 2018 12:30:31 +0800 Subject: Use MVVM design pattern to load and display the posts. Minor bugs fixed. --- .../infinityforreddit/PaginationSynchronizer.java | 32 ++- .../infinityforreddit/ParsePost.java | 127 +++++---- .../ml/docilealligator/infinityforreddit/Post.java | 302 +++++++++++++++++++++ .../infinityforreddit/PostData.java | 302 --------------------- .../infinityforreddit/PostFragment.java | 173 ++++++------ .../PostPaginationScrollListener.java | 78 +++--- .../infinityforreddit/PostRecyclerViewAdapter.java | 43 +-- .../infinityforreddit/PostViewModel.java | 22 ++ .../infinityforreddit/ViewPostDetailActivity.java | 178 ++++++------ 9 files changed, 641 insertions(+), 616 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Post.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java index c0e61184..7e53a29d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PaginationSynchronizer.java @@ -1,19 +1,21 @@ package ml.docilealligator.infinityforreddit; +import java.util.ArrayList; + class PaginationSynchronizer { private boolean loadingState; private boolean loadSuccess; private PaginationNotifier paginationNotifier; private PaginationRetryNotifier paginationRetryNotifier; - private LastItemSynchronizer lastItemSynchronizer; + private ArrayList lastItemSynchronizers; - PaginationSynchronizer(LastItemSynchronizer lastItemSynchronizer) { + PaginationSynchronizer() { + lastItemSynchronizers = new ArrayList<>(); loadingState = false; loadSuccess = true; - this. lastItemSynchronizer = lastItemSynchronizer; } - public void setLoadingState(boolean isLoading) { + void setLoadingState(boolean isLoading) { this.loadingState = isLoading; } @@ -21,7 +23,7 @@ class PaginationSynchronizer { return loadingState; } - public void loadSuccess(boolean state) { + void loadSuccess(boolean state) { loadSuccess = state; if(loadSuccess) { paginationNotifier.LoadMorePostSuccess(); @@ -30,28 +32,34 @@ class PaginationSynchronizer { } } - public void setLoadSuccess(boolean loadSuccess) { + void setLoadSuccess(boolean loadSuccess) { this.loadSuccess = loadSuccess; } - public boolean isLoadSuccess() { + boolean isLoadingMorePostsSuccess() { return loadSuccess; } - public void setPaginationNotifier(PaginationNotifier paginationNotifier) { + void setPaginationNotifier(PaginationNotifier paginationNotifier) { this.paginationNotifier = paginationNotifier; } - public void setPaginationRetryNotifier(PaginationRetryNotifier paginationRetryNotifier) { + void setPaginationRetryNotifier(PaginationRetryNotifier paginationRetryNotifier) { this.paginationRetryNotifier = paginationRetryNotifier; } - public PaginationRetryNotifier getPaginationRetryNotifier() { + PaginationRetryNotifier getPaginationRetryNotifier() { return paginationRetryNotifier; } - public LastItemSynchronizer getLastItemSynchronizer() { - return lastItemSynchronizer; + void addLastItemSynchronizer(LastItemSynchronizer lastItemSynchronizer) { + lastItemSynchronizers.add(lastItemSynchronizer); + } + + void notifyLastItemChanged(String lastItem) { + for(LastItemSynchronizer l : lastItemSynchronizers) { + l.lastItemChanged(lastItem); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index a5b9bf30..f5f2120f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -19,32 +19,29 @@ import java.util.Locale; class ParsePost { interface ParsePostListener { - void onParsePostSuccess(ArrayList postData, String lastItem); + void onParsePostSuccess(ArrayList newPostData, String lastItem); void onParsePostFail(); } - static void parsePost(String response, ArrayList postData, Locale locale, - ParsePostListener parsePostListener) { - new ParsePostDataAsyncTask(response, postData, locale, parsePostListener).execute(); + static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) { + new ParsePostDataAsyncTask(response, locale, parsePostListener).execute(); } private static class ParsePostDataAsyncTask extends AsyncTask { private JSONObject jsonResponse; - private ArrayList postData; private Locale locale; private ParsePostListener parsePostListener; - private ArrayList newPostData; + private ArrayList newPosts; private String lastItem; private boolean parseFailed; - ParsePostDataAsyncTask(String response, ArrayList postData, Locale locale, + ParsePostDataAsyncTask(String response, Locale locale, ParsePostListener parsePostListener) { try { jsonResponse = new JSONObject(response); - this.postData = postData; this.locale = locale; this.parsePostListener = parsePostListener; - newPostData = new ArrayList<>(); + newPosts = new ArrayList<>(); parseFailed = false; } catch (JSONException e) { e.printStackTrace(); @@ -85,6 +82,7 @@ class ParsePost { int previewWidth = -1; int previewHeight = -1; if(data.has(JSONUtils.PREVIEW_KEY)) { + Log.i("haspreview", Integer.toString(i)); 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) @@ -96,11 +94,11 @@ class ParsePost { if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { //Cross post data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); - parseData(data, permalink, newPostData, id, fullName, subredditNamePrefixed, + parseData(data, permalink, newPosts, id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, previewWidth, previewHeight, score, voteType, gilded, nsfw, stickied, true, i); } else { - parseData(data, permalink, newPostData, id, fullName, subredditNamePrefixed, + parseData(data, permalink, newPosts, id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, previewWidth, previewHeight, score, voteType, gilded, nsfw, stickied, false, i); } @@ -115,15 +113,14 @@ class ParsePost { @Override protected void onPostExecute(Void aVoid) { if(!parseFailed) { - postData.addAll(newPostData); - parsePostListener.onParsePostSuccess(postData, lastItem); + parsePostListener.onParsePostSuccess(newPosts, lastItem); } else { parsePostListener.onParsePostFail(); } } } - private static void parseData(JSONObject data, String permalink, ArrayList bestPostData, + private static void parseData(JSONObject data, String permalink, ArrayList bestPostData, String id, String fullName, String subredditNamePrefixed, 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 { @@ -134,55 +131,55 @@ class ParsePost { if(url.contains(permalink)) { //Text post Log.i("text", Integer.toString(i)); - int postType = PostData.TEXT_TYPE; - PostData postData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + int postType = Post.TEXT_TYPE; + Post post = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { - postData.setSelfText(""); + post.setSelfText(""); } else { - postData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + post.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); } - bestPostData.add(postData); + bestPostData.add(post); } else { //No preview link post Log.i("no preview link", Integer.toString(i)); - int postType = PostData.NO_PREVIEW_LINK_TYPE; - PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + int postType = Post.NO_PREVIEW_LINK_TYPE; + Post linkPost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { - linkPostData.setSelfText(""); + linkPost.setSelfText(""); } else { - linkPostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + linkPost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); } - bestPostData.add(linkPostData); + bestPostData.add(linkPost); } } else if(isVideo) { //Video post Log.i("video", Integer.toString(i)); JSONObject redditVideoObject = data.getJSONObject(JSONUtils.MEDIA_KEY).getJSONObject(JSONUtils.REDDIT_VIDEO_KEY); - int postType = PostData.VIDEO_TYPE; + int postType = Post.VIDEO_TYPE; String videoUrl = redditVideoObject.getString(JSONUtils.DASH_URL_KEY); - PostData videoPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + Post videoPost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, true); - videoPostData.setPreviewWidth(previewWidth); - videoPostData.setPreviewHeight(previewHeight); - videoPostData.setVideoUrl(videoUrl); - videoPostData.setDownloadableGifOrVideo(false); + videoPost.setPreviewWidth(previewWidth); + videoPost.setPreviewHeight(previewHeight); + videoPost.setVideoUrl(videoUrl); + videoPost.setDownloadableGifOrVideo(false); - bestPostData.add(videoPostData); + bestPostData.add(videoPost); } else if(data.has(JSONUtils.PREVIEW_KEY)){ 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)) { //Gif video post (MP4) Log.i("gif video mp4", Integer.toString(i)); - int postType = PostData.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 gifDownloadUrl = variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY); - PostData post = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, + Post post = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, false); @@ -196,11 +193,11 @@ class ParsePost { } else if(data.getJSONObject(JSONUtils.PREVIEW_KEY).has(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY)) { //Gif video post (Dash) Log.i("gif video dash", Integer.toString(i)); - int postType = PostData.GIF_VIDEO_TYPE; + int postType = Post.GIF_VIDEO_TYPE; String videoUrl = data.getJSONObject(JSONUtils.PREVIEW_KEY) .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY); - PostData post = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, + Post post = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost, true); @@ -214,69 +211,71 @@ class ParsePost { if (url.endsWith("jpg") || url.endsWith("png")) { //Image post Log.i("image", Integer.toString(i)); - int postType = PostData.IMAGE_TYPE; + int postType = Post.IMAGE_TYPE; - PostData imagePostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + Post imagePost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, url, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); - imagePostData.setPreviewWidth(previewWidth); - imagePostData.setPreviewHeight(previewHeight); + imagePost.setPreviewWidth(previewWidth); + imagePost.setPreviewHeight(previewHeight); - bestPostData.add(imagePostData); + bestPostData.add(imagePost); } else { if (url.contains(permalink)) { //Text post but with a preview Log.i("text with image", Integer.toString(i)); - int postType = PostData.TEXT_TYPE; - PostData textWithImagePostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + int postType = Post.TEXT_TYPE; + Post textWithImagePost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); - textWithImagePostData.setPreviewWidth(previewWidth); - textWithImagePostData.setPreviewHeight(previewHeight); + textWithImagePost.setPreviewWidth(previewWidth); + textWithImagePost.setPreviewHeight(previewHeight); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { - textWithImagePostData.setSelfText(""); + textWithImagePost.setSelfText(""); } else { - textWithImagePostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + textWithImagePost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); } - bestPostData.add(textWithImagePostData); + bestPostData.add(textWithImagePost); } else { //Link post Log.i("link", Integer.toString(i)); - int postType = PostData.LINK_TYPE; - PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, + int postType = Post.LINK_TYPE; + Post linkPost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, nsfw, stickied, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_HTML_KEY)) { - linkPostData.setSelfText(""); + linkPost.setSelfText(""); } else { - linkPostData.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); + linkPost.setSelfText(data.getString(JSONUtils.SELFTEXT_HTML_KEY).trim()); } - linkPostData.setPreviewWidth(previewWidth); - linkPostData.setPreviewHeight(previewHeight); + linkPost.setPreviewWidth(previewWidth); + linkPost.setPreviewHeight(previewHeight); - bestPostData.add(linkPostData); + bestPostData.add(linkPost); } } } } else { if (url.endsWith("jpg") || url.endsWith("png")) { //Image post - Log.i("CP no preview image", Integer.toString(i)); - int postType = PostData.IMAGE_TYPE; - bestPostData.add(new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, title, - url, url, permalink, score, postType, voteType, - gilded, nsfw, stickied, isCrosspost)); + Log.i("CP image", Integer.toString(i)); + int postType = Post.IMAGE_TYPE; + Post linkPost = new Post(id, fullName, subredditNamePrefixed, formattedPostTime, + title, previewUrl, url, permalink, score, postType, + voteType, gilded, nsfw, stickied, isCrosspost); + linkPost.setPreviewWidth(previewWidth); + linkPost.setPreviewHeight(previewHeight); + bestPostData.add(linkPost); } else { //CP No Preview Link post Log.i("CP no preview link", Integer.toString(i)); - int postType = PostData.NO_PREVIEW_LINK_TYPE; - PostData linkPostData = new PostData(id, fullName, subredditNamePrefixed, formattedPostTime, - title, previewUrl, url, permalink, score, postType, - voteType, gilded, nsfw, stickied, isCrosspost); - bestPostData.add(linkPostData); + int postType = Post.NO_PREVIEW_LINK_TYPE; + bestPostData.add(new Post(id, fullName, subredditNamePrefixed, formattedPostTime, title, + url, url, permalink, score, postType, voteType, + gilded, nsfw, stickied, isCrosspost)); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java new file mode 100644 index 00000000..ab853001 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -0,0 +1,302 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * Created by alex on 3/1/18. + */ + +class Post implements Parcelable { + static final int TEXT_TYPE = 0; + static final int IMAGE_TYPE = 1; + static final int LINK_TYPE = 2; + static final int VIDEO_TYPE = 3; + static final int GIF_VIDEO_TYPE = 4; + static final int NO_PREVIEW_LINK_TYPE = 5; + + private String id; + private String fullName; + private String subredditNamePrefixed; + private String subredditIconUrl; + private String postTime; + private String title; + private String selfText; + private String previewUrl; + private String url; + private String videoUrl; + private String gifOrVideoDownloadUrl; + private String permalink; + private int score; + private int postType; + private int voteType; + private int gilded; + private int previewWidth; + private int previewHeight; + private boolean nsfw; + private boolean stickied; + private boolean isCrosspost; + private boolean isDashVideo; + private boolean isDownloadableGifOrVideo; + private Post crosspostParentPost; + + Post(String id, String fullName, String subredditNamePrefixed, String postTime, String title, + String previewUrl, String permalink, int score, int postType, int voteType, int gilded, + boolean nsfw, boolean stickied, boolean isCrosspost, boolean isDashVideo) { + this.id = id; + this.fullName = fullName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.postTime = postTime; + this.title = title; + this.previewUrl = previewUrl; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nsfw = nsfw; + this.stickied = stickied; + this.isCrosspost = isCrosspost; + this.isDashVideo = isDashVideo; + } + + Post(String id, String fullName, String subredditNamePrefixed, String postTime, String title, + String previewUrl, String url, String permalink, int score, int postType, int voteType, + int gilded, boolean nsfw, boolean stickied, boolean isCrosspost) { + this.id = id; + this.fullName = fullName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.postTime = postTime; + this.title = title; + this.previewUrl = previewUrl; + this.url = url; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nsfw = nsfw; + this.stickied = stickied; + this.isCrosspost = isCrosspost; + } + + Post(String id, String fullName, String subredditNamePrefixed, String postTime, String title, + String permalink, int score, int postType, int voteType, int gilded, boolean nsfw, + boolean stickied, boolean isCrosspost) { + this.id = id; + this.fullName = fullName; + this.subredditNamePrefixed = subredditNamePrefixed; + this.postTime = postTime; + this.title = title; + this.permalink = RedditUtils.API_BASE_URI + permalink; + this.score = score; + this.postType = postType; + this.voteType = voteType; + this.gilded = gilded; + this.nsfw = nsfw; + this.stickied = stickied; + this.isCrosspost= isCrosspost; + } + + protected Post(Parcel in) { + id = in.readString(); + fullName = in.readString(); + subredditNamePrefixed = in.readString(); + subredditIconUrl = in.readString(); + postTime = in.readString(); + title = in.readString(); + selfText = in.readString(); + previewUrl = in.readString(); + url = in.readString(); + videoUrl = in.readString(); + gifOrVideoDownloadUrl = in.readString(); + permalink = in.readString(); + score = in.readInt(); + postType = in.readInt(); + voteType = in.readInt(); + gilded = in.readInt(); + previewWidth = in.readInt(); + previewHeight = in.readInt(); + nsfw = in.readByte() != 0; + stickied = in.readByte() != 0; + isCrosspost = in.readByte() != 0; + isDashVideo = in.readByte() != 0; + isDownloadableGifOrVideo = in.readByte() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public Post createFromParcel(Parcel in) { + return new Post(in); + } + + @Override + public Post[] newArray(int size) { + return new Post[size]; + } + }; + + public String getId() { + return id; + } + + public String getFullName() { + return fullName; + } + + public String getSubredditNamePrefixed() { + return subredditNamePrefixed; + } + + public String getSubredditIconUrl() { + return subredditIconUrl; + } + + public void setSubredditIconUrl(String subredditIconUrl) { + this.subredditIconUrl = subredditIconUrl; + } + + public String getPostTime() { + return postTime; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setSelfText(String selfText) { + this.selfText = selfText; + } + + public String getSelfText() { + return selfText; + } + + public String getPreviewUrl() { + return previewUrl; + } + + public String getUrl() { + return url; + } + + public void setVideoUrl(String videoUrl) { + this.videoUrl = videoUrl; + } + + public String getVideoUrl() { + return videoUrl; + } + + public String getGifOrVideoDownloadUrl() { + return gifOrVideoDownloadUrl; + } + + public void setGifOrVideoDownloadUrl(String gifOrVideoDownloadUrl) { + this.gifOrVideoDownloadUrl = gifOrVideoDownloadUrl; + } + + public String getPermalink() { + return permalink; + } + + public void setScore(int score) { + this.score = score; + } + + public int getScore() { + return score; + } + + public int getPostType() { + return postType; + } + + public void setVoteType(int voteType) { + this.voteType = voteType; + } + + public int getVoteType() { + return voteType; + } + + public int getGilded() { + return gilded; + } + + void setPreviewWidth(int previewWidth) { + this.previewWidth = previewWidth; + } + + int getPreviewWidth() { + return previewWidth; + } + + void setPreviewHeight(int previewHeight) { + this.previewHeight = previewHeight; + } + + int getPreviewHeight() { + return previewHeight; + } + + public boolean isNSFW() { + return nsfw; + } + + @Override + public int describeContents() { + return 0; + } + + public boolean isDashVideo() { + return isDashVideo; + } + + public void setDownloadableGifOrVideo(boolean isDownloadableGifOrVideo) { + this.isDownloadableGifOrVideo = isDownloadableGifOrVideo; + } + + public boolean isDownloadableGifOrVideo() { + return isDownloadableGifOrVideo; + } + + public boolean isStickied() { + return stickied; + } + + public boolean isCrosspost() { + return isCrosspost; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(id); + parcel.writeString(fullName); + parcel.writeString(subredditNamePrefixed); + parcel.writeString(subredditIconUrl); + parcel.writeString(postTime); + parcel.writeString(title); + parcel.writeString(selfText); + parcel.writeString(previewUrl); + parcel.writeString(url); + parcel.writeString(videoUrl); + parcel.writeString(gifOrVideoDownloadUrl); + parcel.writeString(permalink); + parcel.writeInt(score); + parcel.writeInt(postType); + parcel.writeInt(voteType); + parcel.writeInt(gilded); + parcel.writeInt(previewWidth); + parcel.writeInt(previewHeight); + parcel.writeByte((byte) (nsfw ? 1 : 0)); + parcel.writeByte((byte) (stickied ? 1 : 0)); + parcel.writeByte((byte) (isCrosspost ? 1 : 0)); + parcel.writeByte((byte) (isDashVideo ? 1 : 0)); + parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0)); + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java deleted file mode 100644 index 02d7be4c..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostData.java +++ /dev/null @@ -1,302 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.Parcel; -import android.os.Parcelable; - -/** - * Created by alex on 3/1/18. - */ - -class PostData implements Parcelable { - static final int TEXT_TYPE = 0; - static final int IMAGE_TYPE = 1; - static final int LINK_TYPE = 2; - static final int VIDEO_TYPE = 3; - static final int GIF_VIDEO_TYPE = 4; - static final int NO_PREVIEW_LINK_TYPE = 5; - - private String id; - private String fullName; - private String subredditNamePrefixed; - private String subredditIconUrl; - private String postTime; - private String title; - private String selfText; - private String previewUrl; - private String url; - private String videoUrl; - private String gifOrVideoDownloadUrl; - private String permalink; - private int score; - private int postType; - private int voteType; - private int gilded; - private int previewWidth; - private int previewHeight; - private boolean nsfw; - private boolean stickied; - private boolean isCrosspost; - private boolean isDashVideo; - private boolean isDownloadableGifOrVideo; - private PostData crosspostParentPostData; - - PostData(String id, String fullName, String subredditNamePrefixed, String postTime, String title, - String previewUrl, String permalink, int score, int postType, int voteType, int gilded, - boolean nsfw, boolean stickied, boolean isCrosspost, boolean isDashVideo) { - this.id = id; - this.fullName = fullName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.postTime = postTime; - this.title = title; - this.previewUrl = previewUrl; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nsfw = nsfw; - this.stickied = stickied; - this.isCrosspost = isCrosspost; - this.isDashVideo = isDashVideo; - } - - PostData(String id, String fullName, String subredditNamePrefixed, String postTime, String title, - String previewUrl, String url, String permalink, int score, int postType, int voteType, - int gilded, boolean nsfw, boolean stickied, boolean isCrosspost) { - this.id = id; - this.fullName = fullName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.postTime = postTime; - this.title = title; - this.previewUrl = previewUrl; - this.url = url; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nsfw = nsfw; - this.stickied = stickied; - this.isCrosspost = isCrosspost; - } - - PostData(String id, String fullName, String subredditNamePrefixed, String postTime, String title, - String permalink, int score, int postType, int voteType, int gilded, boolean nsfw, - boolean stickied, boolean isCrosspost) { - this.id = id; - this.fullName = fullName; - this.subredditNamePrefixed = subredditNamePrefixed; - this.postTime = postTime; - this.title = title; - this.permalink = RedditUtils.API_BASE_URI + permalink; - this.score = score; - this.postType = postType; - this.voteType = voteType; - this.gilded = gilded; - this.nsfw = nsfw; - this.stickied = stickied; - this.isCrosspost= isCrosspost; - } - - protected PostData(Parcel in) { - id = in.readString(); - fullName = in.readString(); - subredditNamePrefixed = in.readString(); - subredditIconUrl = in.readString(); - postTime = in.readString(); - title = in.readString(); - selfText = in.readString(); - previewUrl = in.readString(); - url = in.readString(); - videoUrl = in.readString(); - gifOrVideoDownloadUrl = in.readString(); - permalink = in.readString(); - score = in.readInt(); - postType = in.readInt(); - voteType = in.readInt(); - gilded = in.readInt(); - previewWidth = in.readInt(); - previewHeight = in.readInt(); - nsfw = in.readByte() != 0; - stickied = in.readByte() != 0; - isCrosspost = in.readByte() != 0; - isDashVideo = in.readByte() != 0; - isDownloadableGifOrVideo = in.readByte() != 0; - } - - public static final Creator CREATOR = new Creator() { - @Override - public PostData createFromParcel(Parcel in) { - return new PostData(in); - } - - @Override - public PostData[] newArray(int size) { - return new PostData[size]; - } - }; - - public String getId() { - return id; - } - - public String getFullName() { - return fullName; - } - - public String getSubredditNamePrefixed() { - return subredditNamePrefixed; - } - - public String getSubredditIconUrl() { - return subredditIconUrl; - } - - public void setSubredditIconUrl(String subredditIconUrl) { - this.subredditIconUrl = subredditIconUrl; - } - - public String getPostTime() { - return postTime; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getTitle() { - return title; - } - - public void setSelfText(String selfText) { - this.selfText = selfText; - } - - public String getSelfText() { - return selfText; - } - - public String getPreviewUrl() { - return previewUrl; - } - - public String getUrl() { - return url; - } - - public void setVideoUrl(String videoUrl) { - this.videoUrl = videoUrl; - } - - public String getVideoUrl() { - return videoUrl; - } - - public String getGifOrVideoDownloadUrl() { - return gifOrVideoDownloadUrl; - } - - public void setGifOrVideoDownloadUrl(String gifOrVideoDownloadUrl) { - this.gifOrVideoDownloadUrl = gifOrVideoDownloadUrl; - } - - public String getPermalink() { - return permalink; - } - - public void setScore(int score) { - this.score = score; - } - - public int getScore() { - return score; - } - - public int getPostType() { - return postType; - } - - public void setVoteType(int voteType) { - this.voteType = voteType; - } - - public int getVoteType() { - return voteType; - } - - public int getGilded() { - return gilded; - } - - void setPreviewWidth(int previewWidth) { - this.previewWidth = previewWidth; - } - - int getPreviewWidth() { - return previewWidth; - } - - void setPreviewHeight(int previewHeight) { - this.previewHeight = previewHeight; - } - - int getPreviewHeight() { - return previewHeight; - } - - public boolean isNSFW() { - return nsfw; - } - - @Override - public int describeContents() { - return 0; - } - - public boolean isDashVideo() { - return isDashVideo; - } - - public void setDownloadableGifOrVideo(boolean isDownloadableGifOrVideo) { - this.isDownloadableGifOrVideo = isDownloadableGifOrVideo; - } - - public boolean isDownloadableGifOrVideo() { - return isDownloadableGifOrVideo; - } - - public boolean isStickied() { - return stickied; - } - - public boolean isCrosspost() { - return isCrosspost; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(id); - parcel.writeString(fullName); - parcel.writeString(subredditNamePrefixed); - parcel.writeString(subredditIconUrl); - parcel.writeString(postTime); - parcel.writeString(title); - parcel.writeString(selfText); - parcel.writeString(previewUrl); - parcel.writeString(url); - parcel.writeString(videoUrl); - parcel.writeString(gifOrVideoDownloadUrl); - parcel.writeString(permalink); - parcel.writeInt(score); - parcel.writeInt(postType); - parcel.writeInt(voteType); - parcel.writeInt(gilded); - parcel.writeInt(previewWidth); - parcel.writeInt(previewHeight); - parcel.writeByte((byte) (nsfw ? 1 : 0)); - parcel.writeByte((byte) (stickied ? 1 : 0)); - parcel.writeByte((byte) (isCrosspost ? 1 : 0)); - parcel.writeByte((byte) (isDashVideo ? 1 : 0)); - parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0)); - } -} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 3afd6ece..511949b2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -1,12 +1,13 @@ package ml.docilealligator.infinityforreddit; -import android.content.ClipData; -import android.content.ClipboardManager; +import android.arch.lifecycle.Observer; +import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v4.app.Fragment; @@ -52,13 +53,17 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private LinearLayout mFetchPostErrorLinearLayout; private ImageView mFetchPostErrorImageView; - private ArrayList mPostData; + private ArrayList mPostData; private String mLastItem; private PaginationSynchronizer mPaginationSynchronizer; private boolean mIsBestPost; private String mSubredditName; + private PostRecyclerViewAdapter mAdapter; + + private PostViewModel mPostViewModel; + @Inject @Named("no_oauth") Retrofit mRetrofit; @@ -75,12 +80,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); - if(mPostData != null) { - outState.putParcelableArrayList(POST_DATA_PARCELABLE_STATE, mPostData); - outState.putString(LAST_ITEM_STATE, mLastItem); - outState.putBoolean(LOADING_STATE_STATE, mPaginationSynchronizer.isLoading()); - outState.putBoolean(LOAD_SUCCESS_STATE, mPaginationSynchronizer.isLoadSuccess()); - } + outState.putString(LAST_ITEM_STATE, mLastItem); + outState.putBoolean(LOADING_STATE_STATE, mPaginationSynchronizer.isLoading()); + outState.putBoolean(LOAD_SUCCESS_STATE, mPaginationSynchronizer.isLoadingMorePostsSuccess()); } @Override @@ -131,42 +133,57 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } - if(savedInstanceState != null && savedInstanceState.containsKey(POST_DATA_PARCELABLE_STATE)) { - mPostData = savedInstanceState.getParcelableArrayList(POST_DATA_PARCELABLE_STATE); - mLastItem = savedInstanceState.getString(LAST_ITEM_STATE); - mPaginationSynchronizer = new PaginationSynchronizer(new LastItemSynchronizer() { - @Override - public void lastItemChanged(String lastItem) { - mLastItem = lastItem; + mPaginationSynchronizer = new PaginationSynchronizer(); + mPaginationSynchronizer.addLastItemSynchronizer(new LastItemSynchronizer() { + @Override + public void lastItemChanged(String lastItem) { + mLastItem = lastItem; + } + }); + + mPostViewModel = ViewModelProviders.of(this).get(PostViewModel.class); + mPostViewModel.getPosts().observe(this, new Observer>() { + @Override + public void onChanged(@Nullable ArrayList posts) { + mAdapter.changeDataSet(posts); + if(posts == null) { + Log.i("datachange", Integer.toString(0)); + } else { + Log.i("datachange", Integer.toString(posts.size())); } - }); + } + }); + + if(mIsBestPost) { + mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, + mSharedPreferences, mPaginationSynchronizer, mIsBestPost); + + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mOauthRetrofit, mPostViewModel, mLinearLayoutManager, + mLastItem, mPaginationSynchronizer, mSubredditName, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadingMorePostsSuccess(), + getResources().getConfiguration().locale)); + } else { + mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit, + mSharedPreferences, mPaginationSynchronizer, mIsBestPost); + + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mRetrofit, mPostViewModel, mLinearLayoutManager, + mLastItem, mPaginationSynchronizer, mSubredditName, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadingMorePostsSuccess(), + getResources().getConfiguration().locale)); + } + mPostRecyclerView.setAdapter(mAdapter); + + if(savedInstanceState != null && savedInstanceState.containsKey(LAST_ITEM_STATE)) { + mLastItem = savedInstanceState.getString(LAST_ITEM_STATE); + + mPaginationSynchronizer.notifyLastItemChanged(mLastItem); mPaginationSynchronizer.setLoadSuccess(savedInstanceState.getBoolean(LOAD_SUCCESS_STATE)); mPaginationSynchronizer.setLoadingState(savedInstanceState.getBoolean(LOADING_STATE_STATE)); - PostRecyclerViewAdapter adapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, - mSharedPreferences, mPostData, mPaginationSynchronizer, mIsBestPost); - mPostRecyclerView.setAdapter(adapter); - if(mIsBestPost) { - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mOauthRetrofit, mLinearLayoutManager, adapter, mLastItem, mPostData, - mPaginationSynchronizer, mSubredditName, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), - getResources().getConfiguration().locale)); - mProgressBar.setVisibility(View.GONE); - } else { - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mRetrofit, mLinearLayoutManager, adapter, mLastItem, mPostData, - mPaginationSynchronizer, mSubredditName, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), - getResources().getConfiguration().locale)); - mProgressBar.setVisibility(View.GONE); - } + + mProgressBar.setVisibility(View.GONE); } else { - mPaginationSynchronizer = new PaginationSynchronizer(new LastItemSynchronizer() { - @Override - public void lastItemChanged(String lastItem) { - mLastItem = lastItem; - } - }); if(mIsBestPost) { fetchBestPost(); } else { @@ -191,27 +208,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void onResponse(Call call, retrofit2.Response response) { if(getActivity() != null) { if(response.isSuccessful()) { - ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("response", response.body()); - clipboard.setPrimaryClip(clip); - - ParsePost.parsePost(response.body(), new ArrayList(), - getResources().getConfiguration().locale, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response.body(), getResources().getConfiguration().locale, + new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList postData, String lastItem) { + public void onParsePostSuccess(ArrayList newPosts, String lastItem) { if(isAdded() && getActivity() != null) { - mPostData = postData; mLastItem = lastItem; - PostRecyclerViewAdapter adapter = new PostRecyclerViewAdapter( - getActivity(), mOauthRetrofit, mSharedPreferences, - postData, mPaginationSynchronizer, mIsBestPost); - - mPostRecyclerView.setAdapter(adapter); - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mOauthRetrofit, mLinearLayoutManager, adapter, lastItem, postData, - mPaginationSynchronizer, mSubredditName, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), - getResources().getConfiguration().locale)); + mPaginationSynchronizer.notifyLastItemChanged(lastItem); + mPostViewModel.setPosts(newPosts); mProgressBar.setVisibility(View.GONE); } } @@ -247,27 +251,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void onResponse(Call call, retrofit2.Response response) { if(getActivity() != null) { if(response.isSuccessful()) { - ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("response", response.body()); - clipboard.setPrimaryClip(clip); - - ParsePost.parsePost(response.body(), new ArrayList(), - getResources().getConfiguration().locale, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response.body(), getResources().getConfiguration().locale, + new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList postData, String lastItem) { + public void onParsePostSuccess(ArrayList newPosts, String lastItem) { if(isAdded() && getActivity() != null) { - mPostData = postData; mLastItem = lastItem; - PostRecyclerViewAdapter adapter = new PostRecyclerViewAdapter( - getActivity(), mRetrofit, mSharedPreferences, - postData, mPaginationSynchronizer, mIsBestPost); - - mPostRecyclerView.setAdapter(adapter); - mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( - getActivity(), mRetrofit, mLinearLayoutManager, adapter, lastItem, postData, - mPaginationSynchronizer, mSubredditName, mIsBestPost, - mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadSuccess(), - getResources().getConfiguration().locale)); + mPaginationSynchronizer.notifyLastItemChanged(lastItem); + mPostViewModel.setPosts(newPosts); mProgressBar.setVisibility(View.GONE); } } @@ -318,24 +309,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void refresh() { mLastItem = null; - mPaginationSynchronizer = new PaginationSynchronizer(new LastItemSynchronizer() { - @Override - public void lastItemChanged(String lastItem) { - mLastItem = lastItem; - } - }); mPostRecyclerView.clearOnScrollListeners(); mPostRecyclerView.getRecycledViewPool().clear(); - if(mPostData != null) { - mPostData.clear(); - } - mPostData = null; - if(mPostRecyclerView.getAdapter() != null) { - (mPostRecyclerView.getAdapter()).notifyDataSetChanged(); - } + + mPostViewModel.setPosts(null); + if(mIsBestPost) { + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mOauthRetrofit, mPostViewModel, mLinearLayoutManager, + mLastItem, mPaginationSynchronizer, mSubredditName, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadingMorePostsSuccess(), + getResources().getConfiguration().locale)); + fetchBestPost(); } else { + mPostRecyclerView.addOnScrollListener(new PostPaginationScrollListener( + getActivity(), mRetrofit, mPostViewModel, mLinearLayoutManager, + mLastItem, mPaginationSynchronizer, mSubredditName, mIsBestPost, + mPaginationSynchronizer.isLoading(), mPaginationSynchronizer.isLoadingMorePostsSuccess(), + getResources().getConfiguration().locale)); + fetchPost(); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java index 7e1c6027..29b1776e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.content.ClipData; -import android.content.ClipboardManager; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.LinearLayoutManager; @@ -23,9 +21,8 @@ import retrofit2.Retrofit; class PostPaginationScrollListener extends RecyclerView.OnScrollListener { private Context mContext; private Retrofit mRetrofit; + private PostViewModel mPostViewModel; private LinearLayoutManager mLayoutManager; - private PostRecyclerViewAdapter mAdapter; - private ArrayList mPostData; private PaginationSynchronizer mPaginationSynchronizer; private String mSubredditName; @@ -35,19 +32,18 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { private Locale locale; private String mLastItem; - PostPaginationScrollListener(Context context, Retrofit retrofit, LinearLayoutManager layoutManager, PostRecyclerViewAdapter adapter, - String lastItem, ArrayList postData, PaginationSynchronizer paginationSynchronizer, - final String subredditName, final boolean isBestPost, boolean isLoading, - boolean loadSuccess, Locale locale) { + PostPaginationScrollListener(Context context, Retrofit retrofit, PostViewModel postViewModel, + LinearLayoutManager layoutManager, String lastItem, + PaginationSynchronizer paginationSynchronizer, final String subredditName, + final boolean isBestPost, boolean isLoading, boolean loadSuccess, Locale locale) { if(context != null) { - this.mContext = context; - this.mRetrofit = retrofit; - this.mLayoutManager = layoutManager; - this.mAdapter = adapter; - this.mLastItem = lastItem; - this.mPostData = postData; - this.mPaginationSynchronizer = paginationSynchronizer; - this.mSubredditName = subredditName; + mContext = context; + mRetrofit = retrofit; + mPostViewModel = postViewModel; + mLayoutManager = layoutManager; + mLastItem = lastItem; + mPaginationSynchronizer = paginationSynchronizer; + mSubredditName = subredditName; this.isBestPost = isBestPost; this.isLoading = isLoading; this.loadSuccess = loadSuccess; @@ -59,12 +55,17 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { if (isBestPost) { fetchBestPost(); } else { - fetchPost(subredditName, 1); + fetchPost(subredditName); } } }; mPaginationSynchronizer.setPaginationRetryNotifier(paginationRetryNotifier); - //mLastItemSynchronizer = mPaginationSynchronizer.getLastItemSynchronizer(); + mPaginationSynchronizer.addLastItemSynchronizer(new LastItemSynchronizer() { + @Override + public void lastItemChanged(String lastItem) { + mLastItem = lastItem; + } + }); } } @@ -80,7 +81,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { if(isBestPost) { fetchBestPost(); } else { - fetchPost(mSubredditName, 1); + fetchPost(mSubredditName); } } } @@ -101,16 +102,16 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { @Override public void onResponse(Call call, retrofit2.Response response) { if(response.isSuccessful()) { - ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("response", response.body()); - clipboard.setPrimaryClip(clip); - ParsePost.parsePost(response.body(), mPostData, locale, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList postData, String lastItem) { - if(mAdapter != null) { - mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size()); + public void onParsePostSuccess(ArrayList newPostData, String lastItem) { + if(mPostViewModel != null) { + ArrayList posts = mPostViewModel.getPosts().getValue(); + posts.addAll(newPostData); + mPostViewModel.setPosts(posts); + mLastItem = lastItem; - mPaginationSynchronizer.getLastItemSynchronizer().lastItemChanged(lastItem); + mPaginationSynchronizer.notifyLastItemChanged(lastItem); isLoading = false; loadSuccess = true; @@ -140,12 +141,7 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { }); } - private void fetchPost(final String subredditName, final int refreshTime) { - if(refreshTime < 0) { - loadFailed(); - return; - } - + private void fetchPost(final String subredditName) { isLoading = true; loadSuccess = false; mPaginationSynchronizer.setLoadingState(true); @@ -156,16 +152,16 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { @Override public void onResponse(Call call, retrofit2.Response response) { if(response.isSuccessful()) { - ClipboardManager clipboard = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("response", response.body()); - clipboard.setPrimaryClip(clip); - ParsePost.parsePost(response.body(), mPostData, locale, new ParsePost.ParsePostListener() { + ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { @Override - public void onParsePostSuccess(ArrayList postData, String lastItem) { - if(mAdapter != null) { - mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size()); + public void onParsePostSuccess(ArrayList newPostData, String lastItem) { + if(mPostViewModel != null) { + ArrayList posts = mPostViewModel.getPosts().getValue(); + posts.addAll(newPostData); + mPostViewModel.setPosts(posts); + mLastItem = lastItem; - mPaginationSynchronizer.getLastItemSynchronizer().lastItemChanged(lastItem); + mPaginationSynchronizer.notifyLastItemChanged(lastItem); isLoading = false; loadSuccess = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 1bda1e61..9ded7a44 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -47,7 +47,7 @@ import retrofit2.Retrofit; */ class PostRecyclerViewAdapter extends RecyclerView.Adapter { - private ArrayList mPostData; + private ArrayList mPostData; private Context mContext; private Retrofit mOauthRetrofit; private SharedPreferences mSharedPreferences; @@ -62,12 +62,12 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter postData, PaginationSynchronizer paginationSynchronizer, boolean hasMultipleSubreddits) { + PostRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, SharedPreferences sharedPreferences, PaginationSynchronizer paginationSynchronizer, boolean hasMultipleSubreddits) { if(context != null) { mContext = context; mOauthRetrofit = oauthRetrofit; mSharedPreferences = sharedPreferences; - mPostData = postData; + mPostData = new ArrayList<>(); mPaginationSynchronizer = paginationSynchronizer; this.hasMultipleSubreddits = hasMultipleSubreddits; glide = Glide.with(mContext.getApplicationContext()); @@ -113,14 +113,16 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter= 0) { + mPostData.get(holder.getAdapterPosition()).setSubredditIconUrl(iconImageUrl); + } } } }).execute(); @@ -203,7 +205,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter() { + private void loadImage(final RecyclerView.ViewHolder holder, final Post post) { + RequestBuilder imageRequestBuilder = glide.load(post.getPreviewUrl()).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { ((DataViewHolder) holder).progressBar.setVisibility(View.GONE); @@ -488,7 +490,7 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter= mPostData.size() ? VIEW_TYPE_LOADING : VIEW_TYPE_DATA); } + void changeDataSet(ArrayList posts) { + mPostData = posts; + notifyDataSetChanged(); + } + class DataViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.card_view_view_post_detail) CardView cardView; @BindView(R.id.subreddit_icon_circle_image_view_best_post_item) CircleImageView subredditIconCircleImageView; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java new file mode 100644 index 00000000..f665380f --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -0,0 +1,22 @@ +package ml.docilealligator.infinityforreddit; + +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.MutableLiveData; +import android.arch.lifecycle.ViewModel; + +import java.util.ArrayList; + +class PostViewModel extends ViewModel { + private MutableLiveData> posts = new MutableLiveData<>(); + + LiveData> getPosts() { + if(posts == null) { + setPosts(new ArrayList()); + } + return posts; + } + + void setPosts(ArrayList posts) { + this.posts.postValue(posts); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 5f473540..427b12ed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -57,7 +57,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private String orientationState = "OS"; private int mMoreCommentCount; - private PostData mPostData; + private Post mPost; @BindView(R.id.coordinator_layout_view_post_detail) CoordinatorLayout mCoordinatorLayout; @BindView(R.id.subreddit_icon_circle_image_view_view_post_detail) CircleImageView mSubredditIconCircleImageView; @@ -112,14 +112,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { orientation = getResources().getConfiguration().orientation; - mPostData = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); + mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); TextView titleTextView = findViewById(R.id.title_text_view_view_post_detail); - titleTextView.setText(mPostData.getTitle()); + titleTextView.setText(mPost.getTitle()); - if(mPostData.getSubredditIconUrl() == null) { + if(mPost.getSubredditIconUrl() == null) { mLoadSubredditIconAsyncTask = new LoadSubredditIconAsyncTask( - SubredditRoomDatabase.getDatabase(this).subredditDao(), mPostData.getSubredditNamePrefixed(), + SubredditRoomDatabase.getDatabase(this).subredditDao(), mPost.getSubredditNamePrefixed(), new LoadSubredditIconAsyncTask.LoadSubredditIconAsyncTaskListener() { @Override public void loadIconSuccess(String iconImageUrl) { @@ -131,12 +131,12 @@ public class ViewPostDetailActivity extends AppCompatActivity { .into(mSubredditIconCircleImageView); } - mPostData.setSubredditIconUrl(iconImageUrl); + mPost.setSubredditIconUrl(iconImageUrl); } }); mLoadSubredditIconAsyncTask.execute(); - } else if(!mPostData.getSubredditIconUrl().equals("")) { - Glide.with(this).load(mPostData.getSubredditIconUrl()).into(mSubredditIconCircleImageView); + } else if(!mPost.getSubredditIconUrl().equals("")) { + Glide.with(this).load(mPost.getSubredditIconUrl()).into(mSubredditIconCircleImageView); } else { Glide.with(this).load(R.drawable.subreddit_default_icon).into(mSubredditIconCircleImageView); } @@ -146,15 +146,15 @@ public class ViewPostDetailActivity extends AppCompatActivity { public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, - mPostData.getSubredditNamePrefixed().substring(2)); + mPost.getSubredditNamePrefixed().substring(2)); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY, - mPostData.getSubredditNamePrefixed()); + mPost.getSubredditNamePrefixed()); intent.putExtra(ViewSubredditDetailActivity.EXTRA_QUERY_BY_ID_KEY, false); startActivity(intent); } }); - switch (mPostData.getVoteType()) { + switch (mPost.getVoteType()) { case 1: //Upvote mUpvoteButton.setColorFilter(ContextCompat.getColor(this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); @@ -165,14 +165,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { break; } - if(mPostData.getPostType() != PostData.TEXT_TYPE && mPostData.getPostType() != PostData.NO_PREVIEW_LINK_TYPE) { + if(mPost.getPostType() != Post.TEXT_TYPE && mPost.getPostType() != Post.NO_PREVIEW_LINK_TYPE) { mRelativeLayout.setVisibility(View.VISIBLE); mImageView.setVisibility(View.VISIBLE); - mImageView.setRatio((float) mPostData.getPreviewHeight() / (float) mPostData.getPreviewWidth()); + mImageView.setRatio((float) mPost.getPreviewHeight() / (float) mPost.getPreviewWidth()); loadImage(); } - if(mPostData.isCrosspost()) { + if(mPost.isCrosspost()) { mCrosspostImageView.setVisibility(View.VISIBLE); } @@ -180,62 +180,62 @@ public class ViewPostDetailActivity extends AppCompatActivity { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); - mSubredditTextView.setText(mPostData.getSubredditNamePrefixed()); + mSubredditTextView.setText(mPost.getSubredditNamePrefixed()); mSubredditTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewSubredditDetailActivity.class); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, - mPostData.getSubredditNamePrefixed().substring(2)); + mPost.getSubredditNamePrefixed().substring(2)); intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY, - mPostData.getSubredditNamePrefixed()); + mPost.getSubredditNamePrefixed()); intent.putExtra(ViewSubredditDetailActivity.EXTRA_QUERY_BY_ID_KEY, false); startActivity(intent); } }); - mPostTimeTextView.setText(mPostData.getPostTime()); + mPostTimeTextView.setText(mPost.getPostTime()); - if(mPostData.getGilded() > 0) { + if(mPost.getGilded() > 0) { mGildedImageView.setVisibility(View.VISIBLE); Glide.with(this).load(R.drawable.gold).into(mGildedImageView); mGildedNumberTextView.setVisibility(View.VISIBLE); - String gildedNumber = getResources().getString(R.string.gilded, mPostData.getGilded()); + String gildedNumber = getResources().getString(R.string.gilded, mPost.getGilded()); mGildedNumberTextView.setText(gildedNumber); } - if(mPostData.isNSFW()) { + if(mPost.isNSFW()) { mNSFWTextView.setVisibility(View.VISIBLE); } - mScoreTextView.setText(Integer.toString(mPostData.getScore())); + mScoreTextView.setText(Integer.toString(mPost.getScore())); mShareButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("text/plain"); - String extraText = mPostData.getTitle() + "\n" + mPostData.getPermalink(); + String extraText = mPost.getTitle() + "\n" + mPost.getPermalink(); intent.putExtra(Intent.EXTRA_TEXT, extraText); startActivity(Intent.createChooser(intent, "Share")); } }); - switch (mPostData.getPostType()) { - case PostData.IMAGE_TYPE: + switch (mPost.getPostType()) { + case Post.IMAGE_TYPE: mTypeTextView.setText("IMAGE"); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewImageActivity.class); - intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, mPostData.getUrl()); - intent.putExtra(ViewImageActivity.TITLE_KEY, mPostData.getTitle()); - intent.putExtra(ViewImageActivity.FILE_NAME_KEY, mPostData.getSubredditNamePrefixed().substring(2) - + "-" + mPostData.getId().substring(3)); + intent.putExtra(ViewImageActivity.IMAGE_URL_KEY, mPost.getUrl()); + intent.putExtra(ViewImageActivity.TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewImageActivity.FILE_NAME_KEY, mPost.getSubredditNamePrefixed().substring(2) + + "-" + mPost.getId().substring(3)); startActivity(intent); } }); break; - case PostData.LINK_TYPE: + case Post.LINK_TYPE: mTypeTextView.setText("LINK"); mImageView.setOnClickListener(new View.OnClickListener() { @@ -246,57 +246,57 @@ public class ViewPostDetailActivity extends AppCompatActivity { builder.addDefaultShareMenuItem(); builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(mPostData.getUrl())); + customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(mPost.getUrl())); } }); break; - case PostData.GIF_VIDEO_TYPE: + case Post.GIF_VIDEO_TYPE: mTypeTextView.setText("GIF"); - final Uri gifVideoUri = Uri.parse(mPostData.getVideoUrl()); + final Uri gifVideoUri = Uri.parse(mPost.getVideoUrl()); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewVideoActivity.class); intent.setData(gifVideoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, mPostData.getTitle()); - intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPostData.isDashVideo()); - intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPostData.isDownloadableGifOrVideo()); - if(mPostData.isDownloadableGifOrVideo()) { - intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPostData.getGifOrVideoDownloadUrl()); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPostData.getSubredditNamePrefixed()); - intent.putExtra(ViewVideoActivity.ID_KEY, mPostData.getId()); + intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPost.isDashVideo()); + intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo()); + if(mPost.isDownloadableGifOrVideo()) { + intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditNamePrefixed()); + intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId()); } startActivity(intent); } }); break; - case PostData.VIDEO_TYPE: + case Post.VIDEO_TYPE: mTypeTextView.setText("VIDEO"); - final Uri videoUri = Uri.parse(mPostData.getVideoUrl()); + final Uri videoUri = Uri.parse(mPost.getVideoUrl()); mImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(ViewPostDetailActivity.this, ViewVideoActivity.class); intent.setData(videoUri); - intent.putExtra(ViewVideoActivity.TITLE_KEY, mPostData.getTitle()); - intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPostData.isDashVideo()); - intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPostData.isDownloadableGifOrVideo()); - if(mPostData.isDownloadableGifOrVideo()) { - intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPostData.getGifOrVideoDownloadUrl()); - intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPostData.getSubredditNamePrefixed()); - intent.putExtra(ViewVideoActivity.ID_KEY, mPostData.getId()); + intent.putExtra(ViewVideoActivity.TITLE_KEY, mPost.getTitle()); + intent.putExtra(ViewVideoActivity.IS_DASH_VIDEO_KEY, mPost.isDashVideo()); + intent.putExtra(ViewVideoActivity.IS_DOWNLOADABLE_KEY, mPost.isDownloadableGifOrVideo()); + if(mPost.isDownloadableGifOrVideo()) { + intent.putExtra(ViewVideoActivity.DOWNLOAD_URL_KEY, mPost.getGifOrVideoDownloadUrl()); + intent.putExtra(ViewVideoActivity.SUBREDDIT_KEY, mPost.getSubredditNamePrefixed()); + intent.putExtra(ViewVideoActivity.ID_KEY, mPost.getId()); } startActivity(intent); } }); break; - case PostData.NO_PREVIEW_LINK_TYPE: + case Post.NO_PREVIEW_LINK_TYPE: mTypeTextView.setText("LINK"); - if(!mPostData.getSelfText().equals("")) { + if(!mPost.getSelfText().equals("")) { mContentTextView.setVisibility(View.VISIBLE); - mContentTextView.setHtml(mPostData.getSelfText()); + mContentTextView.setHtml(mPost.getSelfText()); } mNoPreviewLinkImageView.setVisibility(View.VISIBLE); mNoPreviewLinkImageView.setOnClickListener(new View.OnClickListener() { @@ -307,15 +307,15 @@ public class ViewPostDetailActivity extends AppCompatActivity { builder.addDefaultShareMenuItem(); builder.setToolbarColor(getResources().getColor(R.color.colorPrimary)); CustomTabsIntent customTabsIntent = builder.build(); - customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(mPostData.getUrl())); + customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(mPost.getUrl())); } }); break; - case PostData.TEXT_TYPE: + case Post.TEXT_TYPE: mTypeTextView.setText("TEXT"); - if(!mPostData.getSelfText().equals("")) { + if(!mPost.getSelfText().equals("")) { mContentTextView.setVisibility(View.VISIBLE); - mContentTextView.setHtml(mPostData.getSelfText()); + mContentTextView.setHtml(mPost.getSelfText()); } break; } @@ -325,7 +325,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { new ObservableOnSubscribe() { @Override public void subscribe(ObservableEmitter emitter) throws Exception { - emitter.onNext(mPostData.getVoteType()); + emitter.onNext(mPost.getVoteType()); emitter.onComplete(); Log.i("asdasdf", "adasdfasdf"); Toast.makeText(ViewPostDetailActivity.this, "observable", Toast.LENGTH_SHORT).show(); @@ -367,19 +367,19 @@ public class ViewPostDetailActivity extends AppCompatActivity { if (mUpvoteButton.getColorFilter() == null) { mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); if(isDownvotedBefore) { - mScoreTextView.setText(Integer.toString(mPostData.getScore() + 2)); + mScoreTextView.setText(Integer.toString(mPost.getScore() + 2)); } else { - mScoreTextView.setText(Integer.toString(mPostData.getScore() + 1)); + mScoreTextView.setText(Integer.toString(mPost.getScore() + 1)); } VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - mPostData.setVoteType(1); + mPost.setVoteType(1); if(isDownvotedBefore) { - mPostData.setScore(mPostData.getScore() + 2); + mPost.setScore(mPost.getScore() + 2); } else { - mPostData.setScore(mPostData.getScore() + 1); + mPost.setScore(mPost.getScore() + 1); } } @@ -387,30 +387,30 @@ public class ViewPostDetailActivity extends AppCompatActivity { public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, "Cannot upvote this post", Toast.LENGTH_SHORT).show(); mUpvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPostData.getScore())); + mScoreTextView.setText(Integer.toString(mPost.getScore())); mDownvoteButton.setColorFilter(downVoteButtonColorFilter); } - }, mPostData.getFullName(), RedditUtils.DIR_UPVOTE); + }, mPost.getFullName(), RedditUtils.DIR_UPVOTE); } else { //Upvoted before mUpvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPostData.getScore() - 1)); + mScoreTextView.setText(Integer.toString(mPost.getScore() - 1)); VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - mPostData.setVoteType(0); - mPostData.setScore(mPostData.getScore() - 1); + mPost.setVoteType(0); + mPost.setScore(mPost.getScore() - 1); } @Override public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); - mScoreTextView.setText(Integer.toString(mPostData.getScore() + 1)); + mScoreTextView.setText(Integer.toString(mPost.getScore() + 1)); mUpvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); - mPostData.setScore(mPostData.getScore() + 1); + mPost.setScore(mPost.getScore() + 1); } - }, mPostData.getFullName(), RedditUtils.DIR_UNVOTE); + }, mPost.getFullName(), RedditUtils.DIR_UNVOTE); } } }); @@ -427,19 +427,19 @@ public class ViewPostDetailActivity extends AppCompatActivity { if (mDownvoteButton.getColorFilter() == null) { mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); if (isUpvotedBefore) { - mScoreTextView.setText(Integer.toString(mPostData.getScore() - 2)); + mScoreTextView.setText(Integer.toString(mPost.getScore() - 2)); } else { - mScoreTextView.setText(Integer.toString(mPostData.getScore() - 1)); + mScoreTextView.setText(Integer.toString(mPost.getScore() - 1)); } VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - mPostData.setVoteType(-1); + mPost.setVoteType(-1); if(isUpvotedBefore) { - mPostData.setScore(mPostData.getScore() - 2); + mPost.setScore(mPost.getScore() - 2); } else { - mPostData.setScore(mPostData.getScore() - 1); + mPost.setScore(mPost.getScore() - 1); } } @@ -447,30 +447,30 @@ public class ViewPostDetailActivity extends AppCompatActivity { public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, "Cannot downvote this post", Toast.LENGTH_SHORT).show(); mDownvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPostData.getScore())); + mScoreTextView.setText(Integer.toString(mPost.getScore())); mUpvoteButton.setColorFilter(upvoteButtonColorFilter); } - }, mPostData.getFullName(), RedditUtils.DIR_DOWNVOTE); + }, mPost.getFullName(), RedditUtils.DIR_DOWNVOTE); } else { //Down voted before mDownvoteButton.clearColorFilter(); - mScoreTextView.setText(Integer.toString(mPostData.getScore() + 1)); + mScoreTextView.setText(Integer.toString(mPost.getScore() + 1)); VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingWithoutPositionListener() { @Override public void onVoteThingSuccess() { - mPostData.setVoteType(0); - mPostData.setScore(mPostData.getScore()); + mPost.setVoteType(0); + mPost.setScore(mPost.getScore()); } @Override public void onVoteThingFail() { Toast.makeText(ViewPostDetailActivity.this, "Cannot unvote this post", Toast.LENGTH_SHORT).show(); mDownvoteButton.setColorFilter(ContextCompat.getColor(ViewPostDetailActivity.this, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); - mScoreTextView.setText(Integer.toString(mPostData.getScore())); - mPostData.setScore(mPostData.getScore()); + mScoreTextView.setText(Integer.toString(mPost.getScore())); + mPost.setScore(mPost.getScore()); } - }, mPostData.getFullName(), RedditUtils.DIR_UNVOTE); + }, mPost.getFullName(), RedditUtils.DIR_UNVOTE); } } }); @@ -479,7 +479,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private void queryComment() { mCommentProgressbar.setVisibility(View.VISIBLE); mNoCommentWrapperLinearLayout.setVisibility(View.GONE); - FetchComment.fetchComment(mRetrofit, mPostData.getSubredditNamePrefixed(), mPostData.getId(), + FetchComment.fetchComment(mRetrofit, mPost.getSubredditNamePrefixed(), mPost.getId(), null, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(String response) { @@ -494,8 +494,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { CommentMultiLevelRecyclerViewAdapter adapter = new CommentMultiLevelRecyclerViewAdapter( ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mSharedPreferences, (ArrayList) commentData, - mRecyclerView, mPostData.getSubredditNamePrefixed(), - mPostData.getId(), getResources().getConfiguration().locale); + mRecyclerView, mPost.getSubredditNamePrefixed(), + mPost.getId(), getResources().getConfiguration().locale); mRecyclerView.removeItemClickListeners(); mRecyclerView.setToggleItemOnClick(false); mRecyclerView.setAccordion(false); @@ -524,8 +524,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { } private void loadImage() { - RequestBuilder imageRequestBuilder = Glide.with(this).load(mPostData.getPreviewUrl()) - .apply(new RequestOptions().override(mPostData.getPreviewWidth(), mPostData.getPreviewHeight())) + RequestBuilder imageRequestBuilder = Glide.with(this).load(mPost.getPreviewUrl()) + .apply(new RequestOptions().override(mPost.getPreviewWidth(), mPost.getPreviewHeight())) .listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { @@ -549,7 +549,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { } }); - if(mPostData.isNSFW()) { + if(mPost.isNSFW()) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 3))) .into(mImageView); } else { -- cgit v1.2.3