From ae81b237372513b0b4b88375514c56d661c7fa61 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 19 Oct 2018 15:29:07 +0800 Subject: [PATCH] Move refreshAccessToken method from RefreshAccessToken class to AccessTokenAuthenticator class. Bind views using Butterknife in PostRecyclerViewAdapter and CommentMultiLevelRecyclerViewAdapter. --- .../AccessTokenAuthenticator.java | 41 +++++++++- .../CommentMultiLevelRecyclerViewAdapter.java | 37 ++++----- .../infinityforreddit/NetworkModule.java | 4 +- .../infinityforreddit/PostFragment.java | 10 --- .../PostPaginationScrollListener.java | 10 --- .../PostRecyclerViewAdapter.java | 76 +++++++------------ .../infinityforreddit/RefreshAccessToken.java | 54 ------------- app/src/main/res/layout/item_post.xml | 1 - 8 files changed, 84 insertions(+), 149 deletions(-) delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 595eb958..b7f8e104 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -3,19 +3,29 @@ package ml.docilealligator.infinityforreddit; import android.content.SharedPreferences; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import okhttp3.Authenticator; import okhttp3.Headers; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; +import retrofit2.Call; +import retrofit2.Retrofit; class AccessTokenAuthenticator implements Authenticator { + private Retrofit mRetrofit; private SharedPreferences mAuthInfoSharedPreferences; - AccessTokenAuthenticator(SharedPreferences authInfoSharedPreferences) { + AccessTokenAuthenticator(Retrofit retrofit, SharedPreferences authInfoSharedPreferences) { + mRetrofit = retrofit; mAuthInfoSharedPreferences = authInfoSharedPreferences; } @@ -27,7 +37,7 @@ class AccessTokenAuthenticator implements Authenticator { synchronized (this) { String accessTokenFromSharedPreferences = mAuthInfoSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); if (accessToken.equals(accessTokenFromSharedPreferences)) { - String newAccessToken = RefreshAccessToken.refreshAccessToken(mAuthInfoSharedPreferences); + String newAccessToken = refreshAccessToken(); if (!newAccessToken.equals("")) { return response.request().newBuilder().headers(Headers.of(RedditUtils.getOAuthHeader(newAccessToken))).build(); } else { @@ -40,4 +50,31 @@ class AccessTokenAuthenticator implements Authenticator { } return null; } + + private String refreshAccessToken() { + String refreshToken = mAuthInfoSharedPreferences.getString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, ""); + + RedditAPI api = mRetrofit.create(RedditAPI.class); + + Map params = new HashMap<>(); + params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); + params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); + + Call accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); + try { + retrofit2.Response response = accessTokenCall.execute(); + JSONObject jsonObject = new JSONObject((String) response.body()); + + String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); + + mAuthInfoSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, newAccessToken).apply(); + + Log.i("access token", newAccessToken); + return newAccessToken; + } catch (IOException | JSONException e) { + e.printStackTrace(); + } + + return ""; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java index 41864d3b..bb95b2f6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentMultiLevelRecyclerViewAdapter.java @@ -24,6 +24,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import butterknife.BindView; +import butterknife.ButterKnife; import retrofit2.Retrofit; class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { @@ -255,30 +257,21 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { expandButton.setImageResource(isExpanded ? R.drawable.ic_expand_less_black_20dp : R.drawable.ic_expand_more_black_20dp); } - private class CommentViewHolder extends RecyclerView.ViewHolder { - private TextView authorTextView; - private TextView commentTimeTextView; - private HtmlTextView commentHtmlTextView; - private ImageView upvoteButton; - private ImageView downvoteButton; - private TextView scoreTextView; - private ImageView expandButton; - private ProgressBar loadMoreCommentsProgressBar; - private ImageView replyButton; - private View verticalBlock; + class CommentViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.author_text_view_item_post_comment) TextView authorTextView; + @BindView(R.id.comment_time_text_view_item_post_comment) TextView commentTimeTextView; + @BindView(R.id.comment_html_text_view_item_post_comment) HtmlTextView commentHtmlTextView; + @BindView(R.id.plus_button_item_post_comment) ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_comment) TextView scoreTextView; + @BindView(R.id.minus_button_item_post_comment) ImageView downvoteButton; + @BindView(R.id.expand_button_item_post_comment) ImageView expandButton; + @BindView(R.id.load_more_comments_progress_bar) ProgressBar loadMoreCommentsProgressBar; + @BindView(R.id.reply_button_item_post_comment) ImageView replyButton; + @BindView(R.id.vertical_block_item_post_comment) View verticalBlock; - public CommentViewHolder(View itemView) { + CommentViewHolder(View itemView) { super(itemView); - authorTextView = itemView.findViewById(R.id.author_text_view_item_post_comment); - commentTimeTextView = itemView.findViewById(R.id.comment_time_text_view_item_post_comment); - commentHtmlTextView = itemView.findViewById(R.id.comment_html_text_view_item_post_comment); - upvoteButton = itemView.findViewById(R.id.plus_button_item_post_comment); - downvoteButton = itemView.findViewById(R.id.minus_button_item_post_comment); - scoreTextView = itemView.findViewById(R.id.score_text_view_item_post_comment); - loadMoreCommentsProgressBar = itemView.findViewById(R.id.load_more_comments_progress_bar); - expandButton = itemView.findViewById(R.id.expand_button_item_post_comment); - replyButton = itemView.findViewById(R.id.reply_button_item_post_comment); - verticalBlock = itemView.findViewById(R.id.vertical_block_item_post_comment); + ButterKnife.bind(this, itemView); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java index f157bf4d..d97929ac 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java @@ -42,9 +42,9 @@ class NetworkModule { @Provides @Singleton - OkHttpClient provideOkHttpClient(@Named("auth_info") SharedPreferences sharedPreferences) { + OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, @Named("auth_info") SharedPreferences sharedPreferences) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); - okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(sharedPreferences)); + okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, sharedPreferences)); return okHttpClientBuilder.build(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index 3d2c6e7b..d6dc175f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -181,11 +181,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mFetchPostErrorLinearLayout.setVisibility(View.GONE); mProgressBar.setVisibility(View.VISIBLE); - /*Retrofit retrofit = new Retrofit.Builder() - .baseUrl(RedditUtils.OAUTH_API_BASE_URI) - .addConverterFactory(ScalarsConverterFactory.create()) - .build();*/ - RedditAPI api = mOauthRetrofit.create(RedditAPI.class); String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) @@ -246,11 +241,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mFetchPostErrorLinearLayout.setVisibility(View.GONE); mProgressBar.setVisibility(View.VISIBLE); - /*Retrofit retrofit = new Retrofit.Builder() - .baseUrl(RedditUtils.API_BASE_URI) - .addConverterFactory(ScalarsConverterFactory.create()) - .build();*/ - RedditAPI api = mRetrofit.create(RedditAPI.class); Call getPost = api.getPost(mSubredditName, mLastItem); getPost.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java index 04db981d..7e1c6027 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java @@ -92,11 +92,6 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { loadSuccess = false; mPaginationSynchronizer.setLoadingState(true); - /*Retrofit retrofit = new Retrofit.Builder() - .baseUrl(RedditUtils.OAUTH_API_BASE_URI) - .addConverterFactory(ScalarsConverterFactory.create()) - .build();*/ - RedditAPI api = mRetrofit.create(RedditAPI.class); String accessToken = mContext.getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) @@ -155,11 +150,6 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener { loadSuccess = false; mPaginationSynchronizer.setLoadingState(true); - /*Retrofit retrofit = new Retrofit.Builder() - .baseUrl(RedditUtils.API_BASE_URI) - .addConverterFactory(ScalarsConverterFactory.create()) - .build();*/ - RedditAPI api = mRetrofit.create(RedditAPI.class); Call getPost = api.getPost(subredditName, mLastItem); getPost.enqueue(new Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index 1a6c093a..8a80821e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -35,6 +35,8 @@ import com.bumptech.glide.request.target.Target; import java.util.ArrayList; +import butterknife.BindView; +import butterknife.ButterKnife; import de.hdodenhof.circleimageview.CircleImageView; import jp.wasabeef.glide.transformations.BlurTransformation; import retrofit2.Retrofit; @@ -517,63 +519,41 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); - params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); - - Call accessTokenCall = api.getAccessToken(RedditUtils.getHttpBasicAuthHeader(), params); - try { - Response response = accessTokenCall.execute(); - JSONObject jsonObject = new JSONObject((String) response.body()); - - String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); - - sharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, newAccessToken).apply(); - - Log.i("access token", newAccessToken); - return newAccessToken; - } catch (IOException | JSONException e) { - e.printStackTrace(); - } - - return ""; - } -} diff --git a/app/src/main/res/layout/item_post.xml b/app/src/main/res/layout/item_post.xml index 900e9817..5ed9444c 100644 --- a/app/src/main/res/layout/item_post.xml +++ b/app/src/main/res/layout/item_post.xml @@ -13,7 +13,6 @@ android:background="#FFFFFF">