From 313eb77ddc97bdd2f96a2a221d397819308f1851 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 9 Sep 2019 00:14:52 +0800 Subject: Vote status is now correctly shown when viewing post details. Fixed following user failed. --- .../CommentAndPostRecyclerViewAdapter.java | 3 ++- .../infinityforreddit/CommentData.java | 3 ++- .../infinityforreddit/FetchComment.java | 28 +++++++++++++++++----- .../infinityforreddit/ParseComment.java | 20 +++++++++------- .../infinityforreddit/RedditAPI.java | 3 +++ .../infinityforreddit/UserFollowing.java | 3 +-- .../infinityforreddit/ViewPostDetailActivity.java | 7 ++++-- 7 files changed, 47 insertions(+), 20 deletions(-) (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index 6ad3638f..273b52ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -1296,7 +1296,8 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter comments; - if(commentId == null) { - comments = api.getPostAndCommentsById(article); + if(accessToken == null) { + if(commentId == null) { + comments = api.getPostAndCommentsById(article); + } else { + comments = api.getPostAndCommentsSingleThreadById(article, commentId); + } } else { - comments = api.getPostAndCommentsSingleThreadById(article, commentId); + if(commentId == null) { + comments = api.getPostAndCommentsByIdOauth(article, RedditUtils.getOAuthHeader(accessToken)); + } else { + comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, RedditUtils.getOAuthHeader(accessToken)); + } } + comments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { @@ -60,7 +70,8 @@ class FetchComment { }); } - static void fetchMoreComment(Retrofit retrofit, ArrayList allChildren, int startingIndex, + static void fetchMoreComment(Retrofit retrofit, @Nullable String accessToken, + ArrayList allChildren, int startingIndex, int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < 100; i++) { @@ -77,8 +88,13 @@ class FetchComment { stringBuilder.deleteCharAt(stringBuilder.length() - 1); RedditAPI api = retrofit.create(RedditAPI.class); + Call moreComments; + if(accessToken == null) { + moreComments = api.getInfo(stringBuilder.toString()); + } else { + moreComments = api.getInfoOauth(stringBuilder.toString(), RedditUtils.getOAuthHeader(accessToken)); + } - Call moreComments = api.getInfo(stringBuilder.toString()); moreComments.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index f9770867..4bcaa29a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; import android.text.Html; -import android.util.Log; import androidx.annotation.Nullable; @@ -15,6 +14,10 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Locale; +import static ml.docilealligator.infinityforreddit.CommentData.VOTE_TYPE_DOWNVOTE; +import static ml.docilealligator.infinityforreddit.CommentData.VOTE_TYPE_NO_VOTE; +import static ml.docilealligator.infinityforreddit.CommentData.VOTE_TYPE_UPVOTE; + class ParseComment { interface ParseCommentListener { void onParseCommentSuccess(ArrayList expandedComments, String parentId, @@ -49,9 +52,6 @@ class ParseComment { new ParseCommentAsyncTask(childrenArray, commentData, locale, null, depth, parseCommentListener).execute(); } catch (JSONException e) { e.printStackTrace(); - if(e.getMessage() != null) { - Log.i("comment json error", e.getMessage()); - } parseCommentListener.onParseCommentFailed(); } } @@ -94,9 +94,6 @@ class ParseComment { expandChildren(newComments, expandedNewComments); } catch (JSONException e) { parseFailed = true; - if(e.getMessage() != null) { - Log.i("parse comment error", e.getMessage()); - } } return null; } @@ -226,6 +223,13 @@ class ParseComment { } String permalink = Html.fromHtml(singleCommentData.getString(JSONUtils.PERMALINK_KEY)).toString(); int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); + int voteType; + if(singleCommentData.isNull(JSONUtils.LIKES_KEY)) { + voteType = VOTE_TYPE_NO_VOTE; + } else { + voteType = singleCommentData.getBoolean(JSONUtils.LIKES_KEY) ? VOTE_TYPE_UPVOTE : VOTE_TYPE_DOWNVOTE; + score -= voteType; + } long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); @@ -242,7 +246,7 @@ class ParseComment { boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); return new CommentData(id, fullName, author, linkAuthor, formattedSubmitTime, commentContent, - linkId, subredditName, parentId, score, isSubmitter, permalink, depth, collapsed, + linkId, subredditName, parentId, score, voteType, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 55904d2b..8a9b8a9e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -73,6 +73,9 @@ public interface RedditAPI { @GET("/api/info.json?raw_json=1") Call getInfo(@Query("id") String id); + @GET("/api/info.json?raw_json=1") + Call getInfoOauth(@Query("id") String id, @HeaderMap Map headers); + @GET("subreddits/search.json?raw_json=1") Call searchSubreddits(@Query("q") String subredditName, @Query("after") String after, @Query("sort") String sort); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java index 99337362..1b25c415 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/UserFollowing.java @@ -1,7 +1,6 @@ package ml.docilealligator.infinityforreddit; import android.os.AsyncTask; -import android.util.Log; import androidx.annotation.NonNull; @@ -99,7 +98,7 @@ class UserFollowing { String accountName, boolean isSubscribing) { this.subscribedUserDao = subscribedUserDao; this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(), - userData.getName()); + accountName); this.accountName = accountName; this.isSubscribing = isSubscribing; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 42a63247..27b31083 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -548,7 +548,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo commentId = mSingleCommentId; } - FetchComment.fetchComments(mRetrofit, mPost.getId(), commentId, mLocale, new FetchComment.FetchCommentListener() { + Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; + FetchComment.fetchComments(retrofit, mAccessToken, mPost.getId(), commentId, mLocale, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList expandedComments, String parentId, ArrayList children) { @@ -597,7 +598,9 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } isLoadingMoreChildren = true; - FetchComment.fetchMoreComment(mRetrofit, children, mChildrenStartingIndex, + + Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit; + FetchComment.fetchMoreComment(retrofit, mAccessToken, children, mChildrenStartingIndex, 0, mLocale, new FetchComment.FetchMoreCommentListener() { @Override public void onFetchMoreCommentSuccess(ArrayList expandedComments, int childrenStartingIndex) { -- cgit v1.2.3