diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-01-24 15:24:14 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-01-24 15:24:14 +0000 |
commit | 0e1abee883469a6345485499013c97c252361f33 (patch) | |
tree | b3362d94a5a68eb0df7e90675284508ecb0998a6 /app/src/main/java/ml/docilealligator/infinityforreddit | |
parent | 86570d495996141d3c74eecf7fc1735f9168ae50 (diff) | |
download | infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar.gz infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar.bz2 infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar.lz infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar.xz infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.tar.zst infinity-for-reddit-0e1abee883469a6345485499013c97c252361f33.zip |
Fixed vote status cannot be shown on posts loaded from specific subreddits or users.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
6 files changed, 43 insertions, 41 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 615ceb9f..9ff04e03 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -47,8 +47,9 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { this.onPostFetchedCallback = onPostFetchedCallback; } - PostDataSource(Retrofit retrofit, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) { + PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String name, int postType, OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; + this.accessToken = accessToken; this.locale = locale; this.name = name; paginationNetworkStateLiveData = new MutableLiveData(); @@ -192,7 +193,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { private void loadSubredditPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> getPost = api.getSubredditBestPosts(name, null); + Call<String> getPost = api.getSubredditBestPosts(name, null, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, retrofit2.Response<String> response) { @@ -233,7 +234,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { private void loadSubredditPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> getPost = api.getSubredditBestPosts(name, params.key); + Call<String> getPost = api.getSubredditBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, retrofit2.Response<String> response) { @@ -267,7 +268,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { private void loadUserPostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> getPost = api.getUserBestPosts(name, lastItem); + Call<String> getPost = api.getUserBestPosts(name, lastItem, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, retrofit2.Response<String> response) { @@ -311,7 +312,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { private void loadUserPostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> getPost = api.getUserBestPosts(name, params.key); + Call<String> getPost = api.getUserBestPosts(name, params.key, RedditUtils.getOAuthHeader(accessToken)); getPost.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, retrofit2.Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 848799bf..58f67d42 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -28,9 +28,10 @@ class PostDataSourceFactory extends DataSource.Factory { this.onPostFetchedCallback = onPostFetchedCallback; } - PostDataSourceFactory(Retrofit retrofit, Locale locale, String subredditName, int postType, + PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; + this.accessToken = accessToken; this.locale = locale; this.subredditName = subredditName; postDataSourceLiveData = new MutableLiveData<>(); @@ -43,7 +44,7 @@ class PostDataSourceFactory extends DataSource.Factory { if(postType == PostDataSource.TYPE_FRONT_PAGE) { postDataSource = new PostDataSource(retrofit, accessToken, locale, postType, onPostFetchedCallback); } else { - postDataSource = new PostDataSource(retrofit, locale, subredditName, postType, onPostFetchedCallback); + postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); } postDataSourceLiveData.postValue(postDataSource); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index e4023fd0..28ad4cef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -23,6 +23,8 @@ import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import javax.inject.Inject; import javax.inject.Named; +import butterknife.BindView; +import butterknife.ButterKnife; import retrofit2.Retrofit; @@ -34,13 +36,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { static final String NAME_KEY = "NK"; static final String POST_TYPE_KEY = "PTK"; - private CoordinatorLayout mCoordinatorLayout; - private RecyclerView mPostRecyclerView; + @BindView(R.id.coordinator_layout_post_fragment) CoordinatorLayout mCoordinatorLayout; + @BindView(R.id.recycler_view_post_fragment) RecyclerView mPostRecyclerView; + @BindView(R.id.progress_bar_post_fragment) CircleProgressBar mProgressBar; + @BindView(R.id.fetch_post_info_linear_layout_post_fragment) LinearLayout mFetchPostInfoLinearLayout; + @BindView(R.id.fetch_post_info_image_view_post_fragment) ImageView mFetchPostInfoImageView; + @BindView(R.id.fetch_post_info_text_view_post_fragment) TextView mFetchPostInfoTextView; + private LinearLayoutManager mLinearLayoutManager; - private CircleProgressBar mProgressBar; - private LinearLayout mFetchPostInfoLinearLayout; - private ImageView mFetchPostInfoImageView; - private TextView mFetchPostInfoTextView; private String mName; private int mPostType; @@ -78,14 +81,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ((Infinity) getActivity().getApplication()).getmNetworkComponent().inject(this); - mCoordinatorLayout = rootView.findViewById(R.id.coordinator_layout_post_fragment); - mPostRecyclerView = rootView.findViewById(R.id.recycler_view_post_fragment); + ButterKnife.bind(this, rootView); + mLinearLayoutManager = new LinearLayoutManager(getActivity()); mPostRecyclerView.setLayoutManager(mLinearLayoutManager); - mProgressBar = rootView.findViewById(R.id.progress_bar_post_fragment); - mFetchPostInfoLinearLayout = rootView.findViewById(R.id.fetch_post_info_linear_layout_post_fragment); - mFetchPostInfoImageView = rootView.findViewById(R.id.fetch_post_info_image_view_post_fragment); - mFetchPostInfoTextView = rootView.findViewById(R.id.fetch_post_info_text_view_post_fragment); /*FloatingActionButton fab = rootView.findViewById(R.id.fab_post_fragment); fab.setOnClickListener(new View.OnClickListener() { @Override @@ -97,26 +96,19 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mPostType = getArguments().getInt(POST_TYPE_KEY); + String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) + .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); + + PostViewModel.Factory factory; + if(mPostType != PostDataSource.TYPE_FRONT_PAGE) { mName = getArguments().getString(NAME_KEY); - } - if(mPostType == PostDataSource.TYPE_FRONT_PAGE) { - mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, - mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); - } else { mAdapter = new PostRecyclerViewAdapter(getActivity(), mRetrofit, mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); - } - mPostRecyclerView.setAdapter(mAdapter); - String accessToken = getActivity().getSharedPreferences(SharedPreferencesUtils.AUTH_CODE_FILE_KEY, Context.MODE_PRIVATE) - .getString(SharedPreferencesUtils.ACCESS_TOKEN_KEY, ""); - - PostViewModel.Factory factory; - if(mPostType == PostDataSource.TYPE_FRONT_PAGE) { factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, - getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() { + getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -131,8 +123,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); } else { - factory = new PostViewModel.Factory(mRetrofit, - getResources().getConfiguration().locale, mName, mPostType, new PostDataSource.OnPostFetchedCallback() { + mAdapter = new PostRecyclerViewAdapter(getActivity(), mOauthRetrofit, + mSharedPreferences, mPostType, () -> mPostViewModel.retryLoadingMore()); + + factory = new PostViewModel.Factory(mOauthRetrofit, accessToken, + getResources().getConfiguration().locale, mPostType, new PostDataSource.OnPostFetchedCallback() { @Override public void hasPost() { mFetchPostInfoLinearLayout.setVisibility(View.GONE); @@ -148,6 +143,8 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } + mPostRecyclerView.setAdapter(mAdapter); + mPostViewModel = ViewModelProviders.of(this, factory).get(PostViewModel.class); mPostViewModel.getPosts().observe(this, posts -> mAdapter.submitList(posts)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java index 69df991f..8bfc4560 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostViewModel.java @@ -35,9 +35,9 @@ public class PostViewModel extends ViewModel { posts = (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build(); } - public PostViewModel(Retrofit retrofit, Locale locale, String subredditName, int postType, + public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { - postDataSourceFactory = new PostDataSourceFactory(retrofit, locale, subredditName, postType, onPostFetchedCallback); + postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), dataSource -> dataSource.getInitialLoadStateLiveData()); @@ -94,9 +94,10 @@ public class PostViewModel extends ViewModel { this.onPostFetchedCallback = onPostFetchedCallback; } - public Factory(Retrofit retrofit, Locale locale, String subredditName, int postType, + public Factory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, PostDataSource.OnPostFetchedCallback onPostFetchedCallback) { this.retrofit = retrofit; + this.accessToken = accessToken; this.locale = locale; this.subredditName = subredditName; this.postType = postType; @@ -109,7 +110,7 @@ public class PostViewModel extends ViewModel { if(postType == PostDataSource.TYPE_FRONT_PAGE) { return (T) new PostViewModel(retrofit, accessToken, locale, postType, onPostFetchedCallback); } else { - return (T) new PostViewModel(retrofit, locale, subredditName, postType, onPostFetchedCallback); + return (T) new PostViewModel(retrofit, accessToken, locale, subredditName, postType, onPostFetchedCallback); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 41fe1567..3a697832 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -39,10 +39,12 @@ public interface RedditAPI { Call<String> getBestPosts(@Query("after") String lastItem, @HeaderMap Map<String, String> headers); @GET("r/{subredditName}.json?raw_json=1&limit=25") - Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem); + Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Query("after") String lastItem, + @HeaderMap Map<String, String> headers); @GET("user/{userName}.json?raw_json=1&limit=25") - Call<String> getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem); + Call<String> getUserBestPosts(@Path("userName") String userName, @Query("after") String lastItem, + @HeaderMap Map<String, String> headers); @GET("user/{username}/about.json?raw_json=1") Call<String> getUserData(@Path("username") String username); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index d598f684..aa9f0ab3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -12,7 +12,7 @@ import java.util.Map; public class RedditUtils { static final String OAUTH_URL ="https://www.reddit.com/api/v1/authorize.compact"; static final String OAUTH_API_BASE_URI = "https://oauth.reddit.com"; - static final String API_BASE_URI = "https://www.reddit.com"; + static final String API_BASE_URI = "https://api.reddit.com"; static final String CLIENT_ID_KEY = "client_id"; static final String CLIENT_ID = ""; |