Vote status is now correctly shown when viewing post details. Fixed following user failed.

This commit is contained in:
Alex Ning 2019-09-09 00:14:52 +08:00
parent 38ed89fc30
commit 313eb77ddc
7 changed files with 47 additions and 20 deletions

View File

@ -1296,7 +1296,8 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie
mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false); mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false);
placeholderTextView.setText(R.string.loading); placeholderTextView.setText(R.string.loading);
FetchComment.fetchMoreComment(mRetrofit, parentComment.getMoreChildrenFullnames(), Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
FetchComment.fetchMoreComment(retrofit, mAccessToken, parentComment.getMoreChildrenFullnames(),
parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale, parentComment.getMoreChildrenStartingIndex(), parentComment.getDepth() + 1, mLocale,
new FetchComment.FetchMoreCommentListener() { new FetchComment.FetchMoreCommentListener() {
@Override @Override

View File

@ -37,7 +37,7 @@ class CommentData implements Parcelable {
private boolean loadMoreChildrenFailed; private boolean loadMoreChildrenFailed;
CommentData(String id, String fullName, String author, String linkAuthor, String commentTime, String commentContent, CommentData(String id, String fullName, String author, String linkAuthor, String commentTime, String commentContent,
String linkId, String subredditName, String parentId, int score, boolean isSubmitter, String permalink, String linkId, String subredditName, String parentId, int score, int voteType, boolean isSubmitter, String permalink,
int depth, boolean collapsed, boolean hasReply, boolean scoreHidden) { int depth, boolean collapsed, boolean hasReply, boolean scoreHidden) {
this.id = id; this.id = id;
this.fullName = fullName; this.fullName = fullName;
@ -49,6 +49,7 @@ class CommentData implements Parcelable {
this.subredditName = subredditName; this.subredditName = subredditName;
this.parentId = parentId; this.parentId = parentId;
this.score = score; this.score = score;
this.voteType = voteType;
this.isSubmitter = isSubmitter; this.isSubmitter = isSubmitter;
this.permalink = RedditUtils.API_BASE_URI + permalink; this.permalink = RedditUtils.API_BASE_URI + permalink;
this.depth = depth; this.depth = depth;

View File

@ -1,6 +1,7 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
@ -21,15 +22,24 @@ class FetchComment {
void onFetchMoreCommentFailed(); void onFetchMoreCommentFailed();
} }
static void fetchComments(Retrofit retrofit, String article, String commentId, static void fetchComments(Retrofit retrofit, @Nullable String accessToken, String article, String commentId,
Locale locale, FetchCommentListener fetchCommentListener) { Locale locale, FetchCommentListener fetchCommentListener) {
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> comments; Call<String> comments;
if(accessToken == null) {
if(commentId == null) { if(commentId == null) {
comments = api.getPostAndCommentsById(article); comments = api.getPostAndCommentsById(article);
} else { } else {
comments = api.getPostAndCommentsSingleThreadById(article, commentId); comments = api.getPostAndCommentsSingleThreadById(article, commentId);
} }
} else {
if(commentId == null) {
comments = api.getPostAndCommentsByIdOauth(article, RedditUtils.getOAuthHeader(accessToken));
} else {
comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, RedditUtils.getOAuthHeader(accessToken));
}
}
comments.enqueue(new Callback<String>() { comments.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
@ -60,7 +70,8 @@ class FetchComment {
}); });
} }
static void fetchMoreComment(Retrofit retrofit, ArrayList<String> allChildren, int startingIndex, static void fetchMoreComment(Retrofit retrofit, @Nullable String accessToken,
ArrayList<String> allChildren, int startingIndex,
int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) { int depth, Locale locale, FetchMoreCommentListener fetchMoreCommentListener) {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < 100; i++) { for(int i = 0; i < 100; i++) {
@ -77,8 +88,13 @@ class FetchComment {
stringBuilder.deleteCharAt(stringBuilder.length() - 1); stringBuilder.deleteCharAt(stringBuilder.length() - 1);
RedditAPI api = retrofit.create(RedditAPI.class); RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> moreComments;
if(accessToken == null) {
moreComments = api.getInfo(stringBuilder.toString());
} else {
moreComments = api.getInfoOauth(stringBuilder.toString(), RedditUtils.getOAuthHeader(accessToken));
}
Call<String> moreComments = api.getInfo(stringBuilder.toString());
moreComments.enqueue(new Callback<String>() { moreComments.enqueue(new Callback<String>() {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {

View File

@ -2,7 +2,6 @@ package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.text.Html; import android.text.Html;
import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -15,6 +14,10 @@ import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; 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 { class ParseComment {
interface ParseCommentListener { interface ParseCommentListener {
void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId, void onParseCommentSuccess(ArrayList<CommentData> expandedComments, String parentId,
@ -49,9 +52,6 @@ class ParseComment {
new ParseCommentAsyncTask(childrenArray, commentData, locale, null, depth, parseCommentListener).execute(); new ParseCommentAsyncTask(childrenArray, commentData, locale, null, depth, parseCommentListener).execute();
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
if(e.getMessage() != null) {
Log.i("comment json error", e.getMessage());
}
parseCommentListener.onParseCommentFailed(); parseCommentListener.onParseCommentFailed();
} }
} }
@ -94,9 +94,6 @@ class ParseComment {
expandChildren(newComments, expandedNewComments); expandChildren(newComments, expandedNewComments);
} catch (JSONException e) { } catch (JSONException e) {
parseFailed = true; parseFailed = true;
if(e.getMessage() != null) {
Log.i("parse comment error", e.getMessage());
}
} }
return null; return null;
} }
@ -226,6 +223,13 @@ class ParseComment {
} }
String permalink = Html.fromHtml(singleCommentData.getString(JSONUtils.PERMALINK_KEY)).toString(); String permalink = Html.fromHtml(singleCommentData.getString(JSONUtils.PERMALINK_KEY)).toString();
int score = singleCommentData.getInt(JSONUtils.SCORE_KEY); 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; long submitTime = singleCommentData.getLong(JSONUtils.CREATED_UTC_KEY) * 1000;
boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY); boolean scoreHidden = singleCommentData.getBoolean(JSONUtils.SCORE_HIDDEN_KEY);
@ -242,7 +246,7 @@ class ParseComment {
boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String); boolean hasReply = !(singleCommentData.get(JSONUtils.REPLIES_KEY) instanceof String);
return new CommentData(id, fullName, author, linkAuthor, formattedSubmitTime, commentContent, 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); hasReply, scoreHidden);
} }

View File

@ -73,6 +73,9 @@ public interface RedditAPI {
@GET("/api/info.json?raw_json=1") @GET("/api/info.json?raw_json=1")
Call<String> getInfo(@Query("id") String id); Call<String> getInfo(@Query("id") String id);
@GET("/api/info.json?raw_json=1")
Call<String> getInfoOauth(@Query("id") String id, @HeaderMap Map<String, String> headers);
@GET("subreddits/search.json?raw_json=1") @GET("subreddits/search.json?raw_json=1")
Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after, Call<String> searchSubreddits(@Query("q") String subredditName, @Query("after") String after,
@Query("sort") String sort); @Query("sort") String sort);

View File

@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -99,7 +98,7 @@ class UserFollowing {
String accountName, boolean isSubscribing) { String accountName, boolean isSubscribing) {
this.subscribedUserDao = subscribedUserDao; this.subscribedUserDao = subscribedUserDao;
this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(), this.subscribedUserData = new SubscribedUserData(userData.getName(), userData.getIconUrl(),
userData.getName()); accountName);
this.accountName = accountName; this.accountName = accountName;
this.isSubscribing = isSubscribing; this.isSubscribing = isSubscribing;
} }

View File

@ -548,7 +548,8 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
commentId = mSingleCommentId; 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 @Override
public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments, public void onFetchCommentSuccess(ArrayList<CommentData> expandedComments,
String parentId, ArrayList<String> children) { String parentId, ArrayList<String> children) {
@ -597,7 +598,9 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo
} }
isLoadingMoreChildren = true; isLoadingMoreChildren = true;
FetchComment.fetchMoreComment(mRetrofit, children, mChildrenStartingIndex,
Retrofit retrofit = mAccessToken == null ? mRetrofit : mOauthRetrofit;
FetchComment.fetchMoreComment(retrofit, mAccessToken, children, mChildrenStartingIndex,
0, mLocale, new FetchComment.FetchMoreCommentListener() { 0, mLocale, new FetchComment.FetchMoreCommentListener() {
@Override @Override
public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) { public void onFetchMoreCommentSuccess(ArrayList<CommentData> expandedComments, int childrenStartingIndex) {