diff options
author | Alex Ning <chineseperson5@gmail.com> | 2018-10-19 07:29:07 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2018-10-19 07:29:07 +0000 |
commit | ae81b237372513b0b4b88375514c56d661c7fa61 (patch) | |
tree | 6b5c2defec8b77335c1ca0232a1d851c20392893 /app/src/main/java/ml | |
parent | 4d0a0725c93f84e3282b16554a950fbf83813d43 (diff) | |
download | infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar.gz infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar.bz2 infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar.lz infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar.xz infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.tar.zst infinity-for-reddit-ae81b237372513b0b4b88375514c56d661c7fa61.zip |
Move refreshAccessToken method from RefreshAccessToken class to AccessTokenAuthenticator class. Bind views using Butterknife in PostRecyclerViewAdapter and CommentMultiLevelRecyclerViewAdapter.
Diffstat (limited to 'app/src/main/java/ml')
7 files changed, 85 insertions, 149 deletions
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<String, String> params = new HashMap<>(); + params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); + params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); + + Call<String> 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; - - public CommentViewHolder(View itemView) { + 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; + + 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<String> getPost = api.getPost(mSubredditName, mLastItem); getPost.enqueue(new Callback<String>() { 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<String> getPost = api.getPost(subredditName, mLastItem); getPost.enqueue(new Callback<String>() { 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<RecyclerView.ViewHold } class DataViewHolder extends RecyclerView.ViewHolder { - private CardView cardView; - private CircleImageView subredditIconCircleImageView; - private TextView subredditNameTextView; - private ImageView stickiedPostImageView; - private TextView postTimeTextView; - private TextView titleTextView; - private TextView typeTextView; - private ImageView gildedImageView; - private TextView gildedNumberTextView; - private ImageView crosspostImageView; - private TextView nsfwTextView; - private RelativeLayout relativeLayout; - private ProgressBar progressBar; - private ImageView imageView; - private LinearLayout errorLinearLayout; - private ImageView noPreviewLinkImageView; - private ImageView upvoteButton; - private TextView scoreTextView; - private ImageView downvoteButton; - private ImageView shareButton; + @BindView(R.id.card_view_view_post_detail) CardView cardView; + @BindView(R.id.subreddit_icon_circle_image_view_best_post_item) CircleImageView subredditIconCircleImageView; + @BindView(R.id.subreddit_text_view_best_post_item) TextView subredditNameTextView; + @BindView(R.id.stickied_post_image_view_best_post_item) ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_best_post_item) TextView postTimeTextView; + @BindView(R.id.title_text_view_best_post_item) TextView titleTextView; + @BindView(R.id.type_text_view_item_best_post) TextView typeTextView; + @BindView(R.id.gilded_image_view_item_best_post) ImageView gildedImageView; + @BindView(R.id.gilded_number_text_view_item_best_post) TextView gildedNumberTextView; + @BindView(R.id.crosspost_image_view_item_best_post) ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_best_post) TextView nsfwTextView; + @BindView(R.id.image_view_wrapper_item_best_post) RelativeLayout relativeLayout; + @BindView(R.id.progress_bar_best_post_item) ProgressBar progressBar; + @BindView(R.id.image_view_best_post_item) ImageView imageView; + @BindView(R.id.load_image_error_linear_layout_best_post_item) LinearLayout errorLinearLayout; + @BindView(R.id.image_view_no_preview_link_best_post_item) ImageView noPreviewLinkImageView; + @BindView(R.id.plus_button_item_best_post) ImageView upvoteButton; + @BindView(R.id.score_text_view_item_best_post) TextView scoreTextView; + @BindView(R.id.minus_button_item_best_post) ImageView downvoteButton; + @BindView(R.id.share_button_item_best_post) ImageView shareButton; DataViewHolder(View itemView) { super(itemView); - cardView = itemView.findViewById(R.id.card_view_view_post_detail); - subredditIconCircleImageView = itemView.findViewById(R.id.subreddit_icon_circle_image_view_best_post_item); - subredditNameTextView = itemView.findViewById(R.id.subreddit_text_view_best_post_item); - stickiedPostImageView = itemView.findViewById(R.id.stickied_post_image_view_best_post_item); - postTimeTextView = itemView.findViewById(R.id.post_time_text_view_best_post_item); - titleTextView = itemView.findViewById(R.id.title_text_view_best_post_item); - typeTextView = itemView.findViewById(R.id.type_text_view_item_best_post); - gildedImageView = itemView.findViewById(R.id.gilded_image_view_item_best_post); - gildedNumberTextView = itemView.findViewById(R.id.gilded_number_text_view_item_best_post); - crosspostImageView = itemView.findViewById(R.id.crosspost_image_view_item_best_post); - nsfwTextView = itemView.findViewById(R.id.nsfw_text_view_item_best_post); - relativeLayout = itemView.findViewById(R.id.image_view_wrapper_item_best_post); - progressBar = itemView.findViewById(R.id.progress_bar_best_post_item); - imageView = itemView.findViewById(R.id.image_view_best_post_item); - errorLinearLayout = itemView.findViewById(R.id.load_image_error_linear_layout_best_post_item); - noPreviewLinkImageView = itemView.findViewById(R.id.image_view_no_preview_link_best_post_item); - - upvoteButton = itemView.findViewById(R.id.plus_button_item_best_post); - scoreTextView = itemView.findViewById(R.id.score_text_view_item_best_post); - downvoteButton = itemView.findViewById(R.id.minus_button_item_best_post); - shareButton = itemView.findViewById(R.id.share_button_item_best_post); + ButterKnife.bind(this, itemView); } } class LoadingViewHolder extends RecyclerView.ViewHolder { - private ProgressBar progressBar; - private RelativeLayout relativeLayout; - private Button retryButton; + @BindView(R.id.progress_bar_footer_progress_bar_item) ProgressBar progressBar; + @BindView(R.id.relative_layout_footer_progress_bar_item) RelativeLayout relativeLayout; + @BindView(R.id.retry_button_footer_progress_bar_item) Button retryButton; - LoadingViewHolder(LinearLayout itemView) { + LoadingViewHolder(View itemView) { super(itemView); - progressBar = itemView.findViewById(R.id.progress_bar_footer_progress_bar_item); - relativeLayout = itemView.findViewById(R.id.relative_layout_footer_progress_bar_item); - retryButton = itemView.findViewById(R.id.retry_button_footer_progress_bar_item); + ButterKnife.bind(this, itemView); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java deleted file mode 100644 index 2ef54d29..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RefreshAccessToken.java +++ /dev/null @@ -1,54 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.SharedPreferences; -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 retrofit2.Call; -import retrofit2.Response; -import retrofit2.Retrofit; -import retrofit2.converter.scalars.ScalarsConverterFactory; - -/** - * Created by alex on 3/13/18. - */ - -class RefreshAccessToken { - static String refreshAccessToken(/*Retrofit oauthRetrofit, */SharedPreferences sharedPreferences) { - String refreshToken = sharedPreferences.getString(SharedPreferencesUtils.REFRESH_TOKEN_KEY, ""); - - Retrofit retrofit = new Retrofit.Builder() - .baseUrl(RedditUtils.API_BASE_URI) - .addConverterFactory(ScalarsConverterFactory.create()) - .build(); - - RedditAPI api = retrofit.create(RedditAPI.class); - - Map<String, String> params = new HashMap<>(); - params.put(RedditUtils.GRANT_TYPE_KEY, RedditUtils.GRANT_TYPE_REFRESH_TOKEN); - params.put(RedditUtils.REFRESH_TOKEN_KEY, refreshToken); - - Call<String> 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 ""; - } -} |