mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2024-12-24 18:08:23 +01:00
Move refreshAccessToken method from RefreshAccessToken class to AccessTokenAuthenticator class. Bind views using Butterknife in PostRecyclerViewAdapter and CommentMultiLevelRecyclerViewAdapter.
This commit is contained in:
parent
4d0a0725c9
commit
ae81b23737
@ -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 "";
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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>() {
|
||||
|
@ -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>() {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 "";
|
||||
}
|
||||
}
|
@ -13,7 +13,6 @@
|
||||
android:background="#FFFFFF">
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/relative_view_item_best_post"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="8dp"
|
||||
|
Loading…
Reference in New Issue
Block a user