From cca031399085f433fcbe150af0a1c4a2d46bdfb1 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 18 Aug 2018 09:22:31 +0800 Subject: Load user info after the new access token is retrieved if the old access token expired. Feature added: vote comment --- .../BestPostRecyclerViewAdapter.java | 52 +++---- .../infinityforreddit/CommentData.java | 10 +- .../CommentRecyclerViewAdapter.java | 109 ++++++++++++++- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/MainActivity.java | 150 ++++++++++++--------- .../infinityforreddit/ParseComment.java | 4 +- .../infinityforreddit/ViewPostDetailActivity.java | 5 +- 7 files changed, 229 insertions(+), 102 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java index 38838a00..5978b407 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BestPostRecyclerViewAdapter.java @@ -132,11 +132,11 @@ class BestPostRecyclerViewAdapter extends RecyclerView.Adapter mCommentData; private RequestQueue mVoteThingRequestQueue; + private RequestQueue mAcquireAccessTokenRequestQueue; CommentRecyclerViewAdapter(Context context, ArrayList commentData, - RequestQueue voteThingRequestQueue) { + RequestQueue voteThingRequestQueue, RequestQueue acquireAccessTokenRequestQueue) { mContext = context; mCommentData = commentData; mVoteThingRequestQueue = voteThingRequestQueue; + mAcquireAccessTokenRequestQueue = acquireAccessTokenRequestQueue; } @NonNull @@ -32,7 +37,7 @@ class CommentRecyclerViewAdapter extends RecyclerView.Adapter>() { + @Override + public void onChanged(@Nullable final List subscribedSubredditData) { + if(!mIsInserting) { + if(subscribedSubredditData == null || subscribedSubredditData.size() == 0) { + subscriptionsLabelTextView.setVisibility(View.GONE); + } else { + subscriptionsLabelTextView.setVisibility(View.VISIBLE); + } + + subredditadapter.setSubscribedSubreddits(subscribedSubredditData); + } + } + }); + + final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this); + subscribedUserRecyclerView.setAdapter(userAdapter); + mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); + mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer>() { + @Override + public void onChanged(@Nullable final List subscribedUserData) { + if(!mIsInserting) { + if(subscribedUserData == null || subscribedUserData.size() == 0) { + followingLabelTextView.setVisibility(View.GONE); + } else { + followingLabelTextView.setVisibility(View.VISIBLE); + } + userAdapter.setSubscribedUsers(subscribedUserData); + } + } + }); + } + } + + private void loadUserData(Bundle savedInstanceState) { + if(savedInstanceState == null) { + if(!mFetchUserInfoSuccess) { new FetchUserInfo(this, Volley.newRequestQueue(this)).queryUserInfo(new FetchUserInfo.FetchUserInfoListener() { @Override public void onFetchUserInfoSuccess(String response) { @@ -178,69 +224,37 @@ public class MainActivity extends AppCompatActivity { }, 1); } - final SubscribedSubredditRecyclerViewAdapter subredditadapter = new SubscribedSubredditRecyclerViewAdapter(this); - subscribedSubredditRecyclerView.setAdapter(subredditadapter); - mSubscribedSubredditViewModel = ViewModelProviders.of(this).get(SubscribedSubredditViewModel.class); - mSubscribedSubredditViewModel.getAllSubscribedSubreddits().observe(this, new Observer>() { - @Override - public void onChanged(@Nullable final List subscribedSubredditData) { - if(!mIsInserting) { - if(subscribedSubredditData == null || subscribedSubredditData.size() == 0) { - subscriptionsLabelTextView.setVisibility(View.GONE); - } else { - subscriptionsLabelTextView.setVisibility(View.VISIBLE); - } - - subredditadapter.setSubscribedSubreddits(subscribedSubredditData); - } - } - }); - - final SubscribedUserRecyclerViewAdapter userAdapter = new SubscribedUserRecyclerViewAdapter(this); - subscribedUserRecyclerView.setAdapter(userAdapter); - mSubscribedUserViewModel = ViewModelProviders.of(this).get(SubscribedUserViewModel.class); - mSubscribedUserViewModel.getAllSubscribedUsers().observe(this, new Observer>() { - @Override - public void onChanged(@Nullable final List subscribedUserData) { - if(!mIsInserting) { - if(subscribedUserData == null || subscribedUserData.size() == 0) { - followingLabelTextView.setVisibility(View.GONE); - } else { - followingLabelTextView.setVisibility(View.VISIBLE); - } - userAdapter.setSubscribedUsers(subscribedUserData); - } - } - }); - - new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList(), - new ArrayList(), new ArrayList()) - .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { - @Override - public void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, - ArrayList subscribedUserData, - ArrayList subredditData) { - mIsInserting = true; - new InsertSubscribedThingsAsyncTask( - SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), - SubscribedUserRoomDatabase.getDatabase(MainActivity.this), - SubredditRoomDatabase.getDatabase(MainActivity.this), - subscribedSubredditData, - subscribedUserData, - subredditData, - new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { - @Override - public void insertSuccess() { - mIsInserting = false; - } - }).execute(); - } + if(mInsertSuccess) { + new FetchSubscribedThing(this, Volley.newRequestQueue(this), new ArrayList(), + new ArrayList(), new ArrayList()) + .fetchSubscribedSubreddits(new FetchSubscribedThing.FetchSubscribedSubredditsListener() { + @Override + public void onFetchSubscribedSubredditsSuccess(ArrayList subscribedSubredditData, + ArrayList subscribedUserData, + ArrayList subredditData) { + mIsInserting = true; + new InsertSubscribedThingsAsyncTask( + SubscribedSubredditRoomDatabase.getDatabase(MainActivity.this), + SubscribedUserRoomDatabase.getDatabase(MainActivity.this), + SubredditRoomDatabase.getDatabase(MainActivity.this), + subscribedSubredditData, + subscribedUserData, + subredditData, + new InsertSubscribedThingsAsyncTask.InsertSubscribedThingListener() { + @Override + public void insertSuccess() { + mIsInserting = false; + mInsertSuccess = true; + } + }).execute(); + } - @Override - public void onFetchSubscribedSubredditsFail() { + @Override + public void onFetchSubscribedSubredditsFail() { - } - }, 1); + } + }, 1); + } } } @@ -264,6 +278,8 @@ public class MainActivity extends AppCompatActivity { outState.putString(profileImageUrlState, mProfileImageUrl); outState.putString(bannerImageUrlState, mBannerImageUrl); outState.putString(karmaState, mKarma); + outState.putBoolean(fetchUserInfoState, mFetchUserInfoSuccess); + outState.putBoolean(insertSubscribedSubredditState, mInsertSuccess); } @Override @@ -273,6 +289,8 @@ public class MainActivity extends AppCompatActivity { mProfileImageUrl = savedInstanceState.getString(profileImageUrlState); mBannerImageUrl = savedInstanceState.getString(bannerImageUrlState); mKarma = savedInstanceState.getString(karmaState); + mFetchUserInfoSuccess = savedInstanceState.getBoolean(fetchUserInfoState); + mInsertSuccess = savedInstanceState.getBoolean(insertSubscribedSubredditState); mNameTextView.setText(mName); mKarmaTextView.setText(mKarma); if(!mProfileImageUrl.equals("")) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java index 58b435f1..686a4874 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParseComment.java @@ -69,7 +69,7 @@ class ParseComment { for (int i = 0; i < actualCommentLength; i++) { JSONObject data = allComments.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); - String id = data.getString(JSONUtils.ID_KEY); + String fullName = data.getString(JSONUtils.LINK_ID); String author = data.getString(JSONUtils.AUTHOR_KEY); boolean isSubmitter = data.getBoolean(JSONUtils.IS_SUBMITTER_KEY); String commentContent = data.getString(JSONUtils.BODY_KEY); @@ -87,7 +87,7 @@ class ParseComment { boolean collapsed = data.getBoolean(JSONUtils.COLLAPSED_KEY); boolean hasReply = !(data.get(JSONUtils.REPLIES_KEY) instanceof String); - newcommentData.add(new CommentData(id, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden)); + newcommentData.add(new CommentData(fullName, author, formattedSubmitTime, commentContent, score, isSubmitter, permalink, depth, collapsed, hasReply, scoreHidden)); } } catch (JSONException e) { parseFailed = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 36bed4e1..47a0ef7f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -53,6 +53,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { private ImageView mNoCommentImageView; private RequestQueue mVoteThingQueue; + private RequestQueue mAcquireAccessTokenRequestQueue; private RequestQueue mCommentQueue; @Override @@ -107,6 +108,7 @@ public class ViewPostDetailActivity extends AppCompatActivity { mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); mVoteThingQueue = Volley.newRequestQueue(this); + mAcquireAccessTokenRequestQueue = Volley.newRequestQueue(this); mCommentQueue = Volley.newRequestQueue(this); subredditTextView.setText(mPostData.getSubredditName()); @@ -296,7 +298,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { mCommentProgressbar.setVisibility(View.GONE); mMoreCommentCount = moreCommentCount; if(commentData.size() > 0) { - CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter(ViewPostDetailActivity.this, commentData, mVoteThingQueue); + CommentRecyclerViewAdapter adapter = new CommentRecyclerViewAdapter( + ViewPostDetailActivity.this, commentData, mVoteThingQueue, mAcquireAccessTokenRequestQueue); mRecyclerView.setAdapter(adapter); mCommentCardView.setVisibility(View.VISIBLE); } else { -- cgit v1.2.3